Last active
August 29, 2015 14:05
-
-
Save daigotanaka/1acc7822eeb522bc1f7d to your computer and use it in GitHub Desktop.
AppleScript to import instapaper (or any list of URLs) to Evernote on Mac
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
| -- See http://www.daigolab.org/instapaper-to-evernote-mac/ for instruction | |
| -- Basic settings | |
| set myNotebook to "Instapaper" | |
| set delayInSeconds to 1 | |
| set giveUpAfter to 30 | |
| set oldToNewOrder to true | |
| -- Function to make sure Evernote is up | |
| to makeSureEvernoteIsUp(giveUpAfter) | |
| tell application "Evernote" to activate | |
| tell application "System Events" to set processCount to the count of (processes whose name is "Evernote") | |
| set trial to 0 | |
| repeat until processCount > 0 or trial > giveUpAfter | |
| delay 1 | |
| tell application "System Events" to set processCount to the count of (processes whose name is "Evernote") | |
| set trial to trial + 1 | |
| end repeat | |
| if processCount = 0 then | |
| return false | |
| end if | |
| return true | |
| end makeSureEvernoteIsUp | |
| -- Main | |
| set prevDelimiter to AppleScript's text item delimiters | |
| set AppleScript's text item delimiters to {","} | |
| log "Making sure Evernote is up..." | |
| if makeSureEvernoteIsUp(giveUpAfter) = false then | |
| display alert "Evernote did not start :(" | |
| error number -128 | |
| end if | |
| tell application "AppleScript Editor" to activate | |
| set theFile to (choose file with prompt "Select a text file containing URLs") | |
| open for access theFile | |
| set FileContents to paragraphs of (read theFile) | |
| if oldToNewOrder then | |
| set FileContents to reverse of FileContents | |
| end if | |
| close access theFile | |
| -- Loop through the URLs | |
| set i to 1 | |
| repeat with nextLine in FileContents | |
| if length of nextLine is greater than 0 then | |
| try | |
| set myData to every text item of nextLine | |
| set {theUrl} to {item 1 of myData} | |
| log {i, theUrl} | |
| tell application "Evernote" | |
| set countMatches to count (find notes ("sourceURL:" & theUrl)) | |
| end tell | |
| if countMatches = 0 then | |
| log " Adding this article…" | |
| tell application "Evernote" | |
| create note from url theUrl notebook myNotebook | |
| end tell | |
| set trial to 0 | |
| repeat until countMatches = 1 or trial > giveUpAfter | |
| if makeSureEvernoteIsUp(giveUpAfter) = false then | |
| display alert "Evernote crashed? Please try running the script again." | |
| error number -128 | |
| end if | |
| set trial to trial + 1 | |
| delay delayInSeconds | |
| tell application "Evernote" | |
| set countMatches to count (find notes ("sourceURL:" & theUrl)) | |
| end tell | |
| end repeat | |
| if trial > giveUpAfter then | |
| log " …error adding this artile." | |
| else | |
| log " …done" | |
| end if | |
| else | |
| log " Already exists. Skipped." | |
| end if | |
| end try | |
| end if | |
| set i to i + 1 | |
| end repeat | |
| -- Finishing up | |
| set AppleScript's text item delimiters to prevDelimiter | |
| tell application "Evernote" | |
| synchronize | |
| end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment