For research, I want to be able to highlight text and have it automatically copied as a note with a link to the exact location in the PDF document. Sente and Skim PDF copy snippets to a notes section when text is highlighted in a PDF. However, the snippets have no links once pasted into a different program. Papers2 also allows extracting notes, but again without links. All programs also add unnecessary titles and additional metadata to each note.

Automator doesn't even extract notes properly from Preview.

The most important condition is for my pasted/extracted notes to have links to within the PDF. What program/script will allow me to do this?


Open AppleScript Editor and save this script as /Applications/skimnoteopener.app:

on open location u
    set text item delimiters to {"=", "&"}
    do shell script "x=" & quoted form of text item 2 of u & ";printf \"${x//\\%/\\x}\""
    set f to POSIX file result
    set p to (text item 4 of u as integer)
    set s to (text item 6 of u as integer)
    set e to (text item 8 of u as integer)
    tell application "Skim"
        open f
        tell document 1
            set current page to page p
            set selection to characters s thru e of text of page p
        end tell
    end tell
end open location

Then run defaults write /Applications/skimnoteopener.app/Contents/Info.plist CFBundleURLTypes '({CFBundleURLName=skimnoteopener;CFBundleURLSchemes=(skimnoteopener);})'. The application should be registered as the default handler for the URL scheme immediately.

You can then use this script to export highlight notes:

do shell script "osascript -e 'tell application \"Skim\"
selection of (notes of document 1 where (its type is highlight note))
end'|tr , \\\\n|awk '{print $2,$4}'"
set ranges to paragraphs of result

set out to ""
tell application "Skim"
    set f to do shell script "ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9]/){\"%%%02X\"%$&.ord}' " & quoted form of POSIX path of (get file of document 1)
    set i to 1
    repeat with n in (notes of document 1 where (its type is highlight note))
        set {s, e} to words of item i of ranges
        set p to index of page of n
        set out to out & "<a href=skimnoteopener://?file=" & f & "&amp;page=" & p & "&amp;start=" & s & "&amp;end=" & e
        set out to out & ">" & p & "</a> " & my escapexml(text of n) & "<br>" & linefeed
        set i to i + 1
    end repeat
end tell

do shell script "printf %s " & quoted form of out & "|textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"

on replace(input, search, replace)
    set text item delimiters to search
    set ti to text items of input
    set text item delimiters to replace
    ti as text
end replace

on escapexml(input)
    replace(replace(replace(input, "&", "&amp;"), "<", "&lt;"), ">", "&gt;")
end escapexml

The script copies the notes as rich text. You can save the notes to an rtf file by replacing -stdout|LC_CTYPE=UTF-8 pbcopy with -output /path/to/file.rtf.

Here is another script that copies the text selected in Skim as a link:

tell application "Skim"
    set f to POSIX path of (get file of document 1)
    set p to index of current page of document 1
    set t to selection of document 1 as text
end tell
tell (do shell script "osascript -e 'tell app \"Skim\" to selection of document 1'")
    set s to word 2
    set e to word 4
end tell
do shell script "printf %s \"<a href=skimnoteopener://?file=$(ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9]/){\"%%%02X\"%$&.ord}' " & quoted form of f & ")&page=" & p & "&start=" & s & "&end=" & e & ">$(printf %s " & quoted form of t & "|sed 's/&/\\&amp;/g;s/</\\&lt;/g;s/>/\\&gt;/g')</a>\"|textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"
  • I've created the app and run the Terminal command, but get no output with the second script. How is the second script supposed to be implemented and run? Thank you. Nov 23 '13 at 15:47
  • @user1891836 You can use the second script by opening a PDF in Skim, running the script in AppleScript Editor, and then pasting the clipboard to a rich text file.
    – Lri
    Nov 24 '13 at 9:48
  • OK. I think the second program works as it should. However, the first script produces equal links that lead nowhere. The link address is always: "skimnoteopener://?file=/Volumes/Macintosh" Ideas? Nov 24 '13 at 11:49
  • @user1891836 I edited the script. It didn't escape the filename part of the URL.
    – Lri
    Nov 24 '13 at 12:38
  • perfect! Do you think a similar solution is possible for web pages? Nov 24 '13 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.