I’m very new to Applescript, and I’m looking for a short education on OmniOutliner and Applescript, so if you have any tips to clean up the below Applescript then I greatly appreciate it. I plan on doing this regardless, but if a more experienced person could give me some pointers, that would be fantastic.
This script is based on The Hackademic’s Skim to Evernote Alfred Workflow. It takes a pdf annotated with Skim’s favorite colors, and exports to OmniOutliner. Depending on the color used it creates a parent or child, and in the notes section it places a hyperlink back to the page of the annotation (hyperlinking requires Hackademic’s Skimmer URL Handler). The gist description contains all relevant links to Hackademic’s awesome work (Alfred Workflow, Skimmer, Website). The applescript is also provided below for users landing here.
set _OO_ to false
repeat until _OO_ = true
tell application "System Events"
if not (exists process "OmniOutliner") then
tell application id "com.omnigroup.OmniOutliner4" to activate
delay 1
tell application id "com.omnigroup.OmniOutliner4" to activate
end if
if (exists process "OmniOutliner") then set _OO_ to true
end tell
end repeat
--tell application id "com.omnigroup.OmniOutliner4"
-- set template to ((path to application support from user domain as string) & "The Omni Group:OmniOutliner:Templates:" & "Default.oo3template:")
-- open template
-- set newOutline to front document -- "open" doesn't return a reference to the new document like "make" does
--end tell
tell application "Skim"
set all_notes to every note of front document
set pdf_name to (name of front document)
set _file to (path of front document)
set file_url to my encode_text(_file, false, false)
set skimmer_url to "skimmer://" & file_url & "?page="
tell application id "com.omnigroup.OmniOutliner4" to make new row with properties {topic:pdf_name} at end of rows of front document
repeat with i from 1 to count of all_notes
set _note to item i of all_notes
set _page to index of page of _note
set real_page to _page as string
set this_url to skimmer_url & _page
if type of _note is highlight note then
set note_text to text of _note
set rgba to color of _note
--set rgb to items 1 thru 3 of rgba
--set fave_colors to favorite colors
set {fav1, fav2, fav3, fav4, fav5, fav6} to favorite colors
--tell front document of application "OmniOutliner"
tell application id "com.omnigroup.OmniOutliner4"
set doc to front document
--set StyleOne to (first named style of front document whose name is "Heading 1")
--set StyleTwo to (first named style of front document whose name is "Heading 2")
--set StyleThree to (first named style of front document whose name is "Heading 3")
--set StyleRed to (first named style of front document whose name is "Red")
if rgba is fav1 then
set FirstHead to make new row with properties {topic:note_text, note:this_url} at end of rows of doc
--set style of FirstHead to StyleOne
else if rgba is fav2 then
set SecondHead to make new row with properties {topic:note_text} at end of last child of doc
--set style of SecondHead to StyleTwo
else if rgba is fav3 then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (2)) times -- if needed, work our way back to the right parent
set parentRow to parent of parentRow
end repeat
set ThirdHead to make new row with properties {topic:note_text, state:checked} at end of parentRow
--set style of ThirdHead to StyleThree
else if rgba is fav4 then
if state of last row of doc is checked then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times -- if needed, work our way back to the right parent
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
else if level of last row of doc is 4 then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
else
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (2)) times
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
end if
else if rgba is fav5 then
if state of last row of doc is checked then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times -- if needed, work our way back to the right parent
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
else if level of last row of doc is 4 then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
else
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (2)) times
set parentRow to parent of parentRow
end repeat
make new row with properties {topic:note_text} at end of parentRow
end if
else if rgba is fav6 then
if state of last row of doc is checked then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times -- if needed, work our way back to the right parent
set parentRow to parent of parentRow
end repeat
set LectureTip to make new row with properties {topic:note_text} at end of parentRow
--set style of LectureTip to StyleRed
else if level of last row of doc is 4 then
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (3)) times
set parentRow to parent of parentRow
end repeat
set LectureTip to make new row with properties {topic:note_text} at end of parentRow
--set style of LectureTip to StyleRed
else
set parentRow to my FetchLastRow(doc)
set parentLevel to level of parentRow
repeat (parentLevel - (2)) times
set parentRow to parent of parentRow
end repeat
set LectureTip to make new row with properties {topic:note_text} at end of parentRow
--set style of LectureTip to StyleRed
end if
end if
end tell
end if
end repeat
tell application id "com.omnigroup.OmniOutliner4" to expandAll rows of front document
tell application "System Events" to set frontmost of process "OmniOutliner" to true
end tell
--tell application id "com.omnigroup.OmniOutliner4" to activate
-- activate
-- expandAll rows of front document
--end tell
on FetchLastRow(doc) -- return the last row in an OmniOutliner document. If no rows, return the document itself.
tell application "OmniOutliner"
set doc to front document
if rows of doc is equal to {} then
return doc
end if
return last row of doc
end tell
end FetchLastRow
--URL encode text
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