Skip to content

Instantly share code, notes, and snippets.

@shamun
Forked from fractaledmind/Custom URL Handler
Last active August 29, 2015 14:07
Show Gist options
  • Save shamun/bc89841e7fb7b2e9374e to your computer and use it in GitHub Desktop.
Save shamun/bc89841e7fb7b2e9374e to your computer and use it in GitHub Desktop.
on run
display dialog "This script applet will respond to webpage links beginning with:" & return & return & "customurl://com.hackademic.AppleScript.URL_Handler?action=1$Path/to/PDF.pdf#" & return & return & "It will open your custom URLs in Skim" buttons {"OK"} default button 1 with title "Custom URL Helper" with icon 1
tell application "Finder" to update (path to me)
end run
on open location this_URL
-- EXTRACT ARGUMENTS
set X to the offset of "?" in this_URL
set y to the offset of "$" in this_URL
set z to the offset of "#" in this_URL
set the argument_string to text from (X + 1) to (y - 1) of this_URL
set the pdfPath to text from (y + 1) to (z - 1) of this_URL
set pdfPath to my decode_text(pdfPath)
set the pdfPage_string to text from (z + 1) to -1 of this_URL
set pdfPage to pdfPage_string as number
set AppleScript's text item delimiters to "&"
set these_arguments to every text item of the argument_string
set AppleScript's text item delimiters to ""
-- PROCESS ACTIONS
repeat with i from 1 to the count of these_arguments
set this_pair to item i of these_arguments
set AppleScript's text item delimiters to "="
copy every text item of this_pair to {this_key, this_value}
set AppleScript's text item delimiters to ""
if this_key is "action" then
if this_value is "1" then
tell application "Skim"
open pdfPath
go document 1 to page pdfPage of document 1
activate
end tell
end if
end if
end repeat
end open location
(* SUB-ROUTINES *)
on decode_chars(these_chars)
copy these_chars to {indentifying_char, multiplier_char, remainder_char}
set the hex_list to "123456789ABCDEF"
if the multiplier_char is in "ABCDEF" then
set the multiplier_amt to the offset of the multiplier_char in the hex_list
else
set the multiplier_amt to the multiplier_char as integer
end if
if the remainder_char is in "ABCDEF" then
set the remainder_amt to the offset of the remainder_char in the hex_list
else
set the remainder_amt to the remainder_char as integer
end if
set the ASCII_num to (multiplier_amt * 16) + remainder_amt
return (ASCII character ASCII_num)
end decode_chars
on decode_text(this_text)
set flag_A to false
set flag_B to false
set temp_char to ""
set the character_list to {}
repeat with this_char in this_text
set this_char to the contents of this_char
if this_char is "%" then
set flag_A to true
else if flag_A is true then
set the temp_char to this_char
set flag_A to false
set flag_B to true
else if flag_B is true then
set the end of the character_list to my decode_chars(("%" & temp_char & this_char) as string)
set the temp_char to ""
set flag_A to false
set flag_B to false
else
set the end of the character_list to this_char
end if
end repeat
return the character_list as string
end decode_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment