Last active
June 21, 2019 17:19
-
-
Save derickfay/25b3de3fb18e0d7fa8ca157e3c0bfeb5 to your computer and use it in GitHub Desktop.
export skim notes to markdown on clipboard sorting by color.applescript
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
(* EXPORT ALL SKIM NOTES TO THE CLIPBOARD | |
(no longer) requires hackademic URL handler from github user smargh | |
entirely rewritten to take advantage of Skim's built-in templating | |
2016-06-26 by derickfay | |
2019-02-09 updated to group notes by color | |
*) | |
property LF : (ASCII character 10) | |
(* Skim Export Templates *) | |
set tNote to "<$contents?><$contents/></$contents?>" | |
set tPage to "<$page.label?><$page.label/><?$page.label?><$pageIndex.numberByAddingOne/></$page.label?>" | |
set tType to "<$type.typeName/>" | |
set tHeader to "<$fileURL/>" | |
set excludedTypes to {"Line", "Circle", "Square", "Ink"} | |
(* add custom colors with titles here | |
hex colors must be an exact match but you can have multiple color values for the same category | |
highlights in colors that don't appear here will export without a category | |
leave the coloredNotes field blank - it is used internally | |
the two sample colors here are based on the hex values for the default favorite colors in Skim, with smargh's categories | |
*) | |
set theColors to {{hex:"#fefb00", category:"Summary", coloredNotes:""}, ¬ | |
{hex:"#ff2600", category:"Disagree", coloredNotes:""}} | |
set hexColor to "<$color.hexString/>" | |
tell application "Skim" | |
try | |
convert notes front document | |
save front document | |
on error msg | |
return msg & " in Pre-Processing." | |
end try | |
-- get info from top 3 notes | |
try | |
set Markdownlink to (get text of note 2 of page 1 of document 1) as string | |
set pdfTitle to (get extended text of note 1 of page 1 of document 1) as string | |
set PDFLink to (get text of note 1 of page 1 of document 1) as string | |
set number_of_Note3 to (get text of note 3 of page 1 of document 1) as string | |
on error msg | |
set fail to button returned of (display dialog "Can't read top 3 notes" buttons {"Cancel", "Add Top 3 Notes"}) | |
if fail is "Cancel" then | |
return | |
else | |
tell application "Alfred 3" to search "sk3" | |
end if | |
end try | |
set noteString to "" | |
set theHeader to "# Skim Notes: [" & pdfTitle & "](" & (format document 1 using template tHeader & ")" & LF & LF) | |
-- set refListText to LF & LF & "## Reference URLS:" & LF & LF | |
set refListText to "" | |
set allNotes to notes of document 1 | |
-- adding sort to fix error introduced in recent versions of Skim | |
repeat with i from 1 to (count of allNotes) - 1 | |
repeat with j from i + 1 to count of allNotes | |
if index of page of item j of allNotes < index of page of item i of allNotes then | |
set temp to item i of allNotes | |
set item i of allNotes to item j of allNotes | |
set item j of allNotes to temp | |
end if | |
-- log allNotes | |
end repeat | |
end repeat | |
set activeNotes to items 4 through -1 of allNotes | |
repeat with n in activeNotes | |
set noteType to format n using template tType | |
if noteType is not in excludedTypes then | |
set formattedNote to format n using template tNote | |
set correctedPage to ((format n using template tPage) as integer) + number_of_Note3 | |
-- set linkID to my randomize("0") | |
-- from original MMD scripts - commented out for use with Ulysses | |
-- set linkIDString to "[" & linkID & "]" | |
-- set tnString to "[Text Note]" | |
-- set linkIDString to "" | |
set tnString to "(Text Note)" | |
if noteType is "Underline" then | |
set noteString to noteString & "### " & formattedNote & " (" & correctedPage & ")" & LF & LF | |
else if noteType is "Text Note" then | |
set noteString to noteString & "#### " & formattedNote & " (" & correctedPage & ") " & tnString & LF & LF | |
else if noteType is "Highlight" then | |
set colorTitleString to "" | |
set c to format n using template hexColor | |
repeat with aColor in theColors | |
if c is (hex of aColor) then | |
set colorTitleString to (category of aColor) | |
set coloredNotes of aColor to coloredNotes of aColor & "> " & colorTitleString & ": " & formattedNote & " (" & correctedPage & ") " & LF & LF | |
end if | |
end repeat | |
if colorTitleString is "" then | |
set noteString to noteString & "> " & formattedNote & " (" & correctedPage & ") " & LF & LF | |
end if | |
else | |
set noteString to noteString & "\"" & formattedNote & "\" (" & correctedPage & ") [" & noteType & "]" & LF & LF | |
end if | |
-- set refListText to refListText & "[" & linkID & "]: " & PDFLink & correctedPage & LF & LF | |
end if | |
end repeat | |
-- add the accumulated colored notes | |
repeat with aColor in theColors | |
set noteString to noteString & "## " & (category of aColor) & LF & LF & (coloredNotes of aColor) | |
end repeat | |
set finalText to theHeader & noteString & refListText | |
set the clipboard to finalText | |
end tell | |
display notification "Skim Notes copied to clipboard" with title "sk2c" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment