Skip to content

Instantly share code, notes, and snippets.

@shawngraham
Created March 11, 2020 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawngraham/5be1e16b9dc6d685a76757ffc834d480 to your computer and use it in GitHub Desktop.
Save shawngraham/5be1e16b9dc6d685a76757ffc834d480 to your computer and use it in GitHub Desktop.
extract notes from skim to clipboard
(* Inspired and modified based on http://drosophiliac.com/2012/09/an-academic-notetaking-workflow.html and https://gist.github.com/smargh/6068104 *)
(* PROPERTIES *)
property LF : (ASCII character 10)
property tid : AppleScript's text item delimiters
(* THE SCRIPT *)
tell application "Skim"
set the clipboard to ""
activate
set numberOfPages to count pages of document 1
tell document 1
set theFile to path
end tell
set selectedColors to {"Summary", "Technique", "Result", "Reference", "Hypothesis", "Question or connection"}
set myColorCodes to my chooseColor(selectedColors)
set documentName to name of document 1
set documentName to text 1 thru -5 of documentName
set pdfType to the button returned of (display dialog "Is this an academic article or book?" buttons {"Article", "Book"} default button 1)
if pdfType is equal to "Article" then
set firstPage to "1" as number
set lastPage to numberOfPages
set useColoredHighlight to the button returned of (display dialog "Do you want to distinguish between highlight colors?" buttons {"Yes", "No"} default button 1)
set AppleScript's text item delimiters to "-"
set tmp to text items of documentName
if (count tmp) = 4 then
set AppleScript's text item delimiters to tid
set articleTitle to item 4 of tmp
set journalName to item 1 of tmp
set authorName to item 3 of tmp
set pubYear to item 2 of tmp
set the clipboard to "# Notes for " & articleTitle & LF & LF & authorName & " et al., " & journalName & ", " & pubYear & LF
else
set the clipboard to "# Notes for " & documentName & LF
end if
else
set extractAll to the button returned of (display dialog "Do you want to export all the notes or some of them?" buttons {"All", "Some"} default button 1)
if extractAll is "All" then
set firstPage to "1" as number
set lastPage to numberOfPages
set the clipboard to "# Notes for " & documentName & LF
else
display dialog "Give the number of the first page." default answer ""
set firstPage to text returned of the result as number
display dialog "Give the number of the last page" default answer ""
set lastPage to text returned of the result as number
set the clipboard to "# Notes for " & documentName & ", Pages " & firstPage & " - " & lastPage & LF
end if
set useColoredHighlight to "No"
end if
set the clipboard to (the clipboard) & "Generated at " & (current date) & LF & LF & "## Automatically extracted notes" & LF & LF
set allNotes to every note of document 1
set allNotes_types to type of every note of document 1
set allNotes_text to text of every note of document 1
set allNotes_pages to index of every page of every note of document 1
set finalNotes to ""
set allLinks to {}
set encodedFilePath to do shell script "python -c \"import sys;print(sys.argv[1].replace(' ','%20'))\" \"" & theFile & "\""
repeat with i from 1 to count of allNotes_types
set notePage to item i of allNotes_pages
set encodedURL to "skimmer:/" & encodedFilePath & "#page=" & notePage
if item i of allNotes_types is highlight note then
set noteText to item i of allNotes_text
if useColoredHighlight is equal to "No" then
set finalNotes to finalNotes & "* Highlight ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & noteText & LF & LF
else
repeat with j from 1 to the count of myColorCodes
--compare color whereas allowing slight differences
set hasSimilarColor to true
repeat with k from 1 to 3
set tmp to color of item i of allNotes
set thiscolor to item k of tmp as integer
if (thiscolor - (item k of item j of myColorCodes) < -5000) or (thiscolor - (item k of item j of myColorCodes) > 5000) then
set hasSimilarColor to false
end if
end repeat
if hasSimilarColor then
set categoryName to item j of selectedColors as text
set finalNotes to finalNotes & "* " & categoryName & " ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & noteText & LF & LF
end if
end repeat
end if
else if item i of allNotes_types is text note then
set noteText to item i of allNotes_text
set finalNotes to finalNotes & "* **Text comment** ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & noteText & LF & LF
else if item i of allNotes_types is anchored note then
set titleText to item i of allNotes_text
set noteText to extended text of item i of allNotes
set finalNotes to finalNotes & "* **Quoted Comment** ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & titleText & LF & tab & LF & tab & noteText & LF & LF
else if item i of allNotes_types is underline note then
set noteText to item i of allNotes_text
set finalNotes to finalNotes & "* Underline ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & noteText & LF & LF
else if item i of selNotes_types is strike out note then
set noteText to item i of allNotes_text
set finalNotes to finalNotes & "* Strike-through ([p." & notePage & "](" & encodedURL & ")): " & LF & tab & "> " & noteText & LF & LF
end if
end repeat
set the clipboard to (the clipboard) & finalNotes
set AppleScript's text item delimiters to tid
end tell
on chooseColor(selectedColors)
set colorCodes to {}
set noteColor to ""
repeat with noteCol in selectedColors
set noteColor to noteCol as text
if noteColor is "Summary" then
set end of colorCodes to {64868, 30911, 30826, 65535}
else if noteColor is "Technique" then
set end of colorCodes to {65159, 47141, 2365, 65535}
else if noteColor is "Result" then
set end of colorCodes to {65535, 65531, 2689, 65535}
else if noteColor is "Reference" then
set end of colorCodes to {8608, 65514, 1753, 65535}
else if noteColor is "Hypothesis" then
set end of colorCodes to {8372, 65519, 65472, 65535}
else if noteColor is "Question or connection" then
set end of colorCodes to {52428, 26219, 65472, 65535}
end if
end repeat
return colorCodes
end chooseColor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment