Created
July 24, 2013 04:39
-
-
Save fractaledmind/6068109 to your computer and use it in GitHub Desktop.
Script inserts Reference Link to selected PDF into either TextEdit or TextMate document. The link uses my custom URL scheme.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* INSERT MARKDOWN LINK TO CHOSEN PDF INTO TEXTEDIT DOC | |
-- Stephen Margheim | |
-- 14 July 2013 | |
-- open source | |
This script assumes you are writing in an open TextEdit document. It will prompt you to choose a PDF file when run. It will then insert the name of the chosen PDF (without the .pdf extension) as a Markdown reference link. That means the linked text is inserted at the cursor point along with a randomly generated 2-digit number and a letter of the alphabet as the link ID. Then, at the end of the document that same link ID and the custom URL are inserted. The cursor is placed one space after the title link. So, for example, you would have something like this: | |
"Some text [title][ID] (then type some more text) | |
[ID]: custom URL | |
" | |
Usage notes: | |
-- You must have at least two extra lines at the end of your text document, or else the [ID]: URL will be appended right after your [title][ID] and you will need to cut and paste. I typically begin every document by hitting Return 3 times then returning to the beginning to actually start typing. | |
-- You must select a PDF document. If you don't you will hear a beep (if your computer isn't muted) and nothing will happen | |
*) | |
--if you use TextMate, change this property to "TextMate" and be sure you have an open, empty TextMate file | |
property theEditor : "TextMate" | |
--prompt to choose the PDF file | |
set thePDF to choose file | |
set PDFInfo to info for thePDF | |
--if you choose a file that isn't a PDF, the script fails | |
if (not (name extension of PDFInfo is "pdf")) then | |
(beep) | |
--if you choose a PDF, the script runs | |
else if name extension of PDFInfo is "pdf" then | |
set pdfPath to POSIX path of thePDF | |
set pdfPath to POSIX file pdfPath as string | |
--get the name of the PDF without the extension | |
tell application "System Events" to tell disk item (pdfPath) to set {theName, theExtension} to {name, name extension} | |
if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName | |
--generate the custom URL for that PDF | |
set pdfPath to my encode_text(pdfPath, true, false) | |
set customURL to "customurl://com.hackademic.AppleScript.URL_Handler?action=1$" & pdfPath & "#1" | |
--Prepatory Material for the Markdown reference link | |
set LF to (ASCII character 10) | |
set LF1 to (ASCII character 32) & (ASCII character 32) & (ASCII character 10) | |
--prepare the inline anchor text and the reference link | |
set InsertText to "[" & theName & "](" & customURL & ")" | |
set the clipboard to InsertText | |
if theEditor is "TextEdit" then | |
--insert the anchor at the cursor point and put the reference link at the end of the document | |
tell application "TextEdit" | |
activate | |
tell application "System Events" | |
keystroke "v" using {command down} | |
end tell | |
delay 0.1 | |
end tell | |
else if theEditor is "TextMate" then | |
tell application "TextMate" | |
insert InsertText | |
end tell | |
end if | |
end if | |
(* SUB-ROUTINES *) | |
on encode_text(this_text, encode_URL_A, encode_URL_B) | |
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789" | |
set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*" | |
set the URL_B_chars to ".-_:" | |
set the acceptable_characters to the standard_characters | |
if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars | |
if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars | |
set the encoded_text to "" | |
repeat with this_char in this_text | |
if this_char is in the acceptable_characters then | |
set the encoded_text to (the encoded_text & this_char) | |
else | |
set the encoded_text to (the encoded_text & encode_char(this_char)) as string | |
end if | |
end repeat | |
return the encoded_text | |
end encode_text | |
on encode_char(this_char) | |
set the ASCII_num to (the ASCII number this_char) | |
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"} | |
set x to item ((ASCII_num div 16) + 1) of the hex_list | |
set y to item ((ASCII_num mod 16) + 1) of the hex_list | |
return ("%" & x & y) as string | |
end encode_char |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment