Skip to content

Instantly share code, notes, and snippets.

@hectormethod
Last active September 12, 2017 14:03
Show Gist options
  • Select an option

  • Save hectormethod/065cd84824533a9595c548e3a05a6f1f to your computer and use it in GitHub Desktop.

Select an option

Save hectormethod/065cd84824533a9595c548e3a05a6f1f to your computer and use it in GitHub Desktop.
[Evernote to DevonThink] #applescript
(*
http://veritrope.com
EVERNOTE TO DEVONTHINK EXPORTER
VERSION 1.72
March 10, 2013
// TERMS OF USE:
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// LIKE THIS SCRIPT?
If this AppleScript is helpful to you, please show your support here:
http://veritrope.com/support
// SCRIPT INFORMATION AND UPDATE PAGE
-- Project Page: http://veritrope.com/code/evernote-to-devonthink-exporter/
-- FastScripts (Optional): http://bit.ly/FastScripts
// INSTALLATION:
-- You can save this script to /Library/Scripts/ and launch it using the system-wide script menu from the Mac OS X menu bar. (The script menu can be activated using the AppleScript Utility application).
-- To use, highlight the note(s) you want to move from Evernote and run this script file;
-- The "User Switches" below allow you to customize the way this script works.
(Optional but recommended)
Easier Keyboard Shortcut with FastScripts
-- Download and Install FastScripts here:
-- http://bit.ly/FastScripts
// CHANGELOG:
* 1.72 BUGFIXES
* 1.71 DETECTION OF APP STORE/ DIRECTLY DOWNLOADED EVERNOTE VERSION
* 1.70 BUGFIXES (FILE EXPORT / SANDBOX)
* 1.63 FIX TO MAKE FILE NAME FIX WORK WITH VERITROPE "OPEN IN SCRIPT EDITOR" BUTTON
* 1.62 FIXED PDF/TAG ISSUE
* 1.61 ADDED NOTE LINKS
* 1.60 TEMPORARY REMOVAL OF GROWL
* 1.51 ADD CREATION DATE / MODIFICATION DATE TO PDF SUBROUTINE
* 1.50 MAINTENANCE RELEASE: BUGFIXES, CHANGES TO USE EVERNOTE'S NATIVE SELECTION, NEW APPROACH TO SLASHES AND COLONS IN NOTE NAMES
* 1.40 (Internal Test)
* 1.30 FIX TO ALLOW SCRIPT TO RUN FROM DEVONTHINK'S INTERNAL SCRIPT MENU, ADD SOURCE URL, CREATION DATE, MODIFICATION DATE DATA FROM EVERNOTE TO IMPORTED NOTE, TAGGING SUPPORT
* 1.23 BUGFIX FOR QUOTATION IN TITLE ISSUE
* 1.22 BUGFIX FOR ADDITIONAL CASE OF "Can’t get item 1 of {}" ERROR ("find notes"/quotation issue)
* 1.21 BUG FIXES ("Can’t get item 1 of {}" ERROR, isRunning PROPERTY)
* 1.20 BUG FIXES / BEGINNING WORK ON SNOW LEOPARD COMPATIBILITY. ADDED CREATION DATE. MODIFIED TO USE NEW GROUP SELECTOR.
* 1.10 REWRITTEN TO USE NATIVE HTML EXPORT, DETECT PDF FILES, RUN IN BACKGROUND
* 1.00 INITIAL RELEASE
*)
(*
======================================
// USER SWITCHES (YOU CAN CHANGE THESE!)
======================================
*)
property transferTags : "ON"
(*
======================================
// PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)
property theNum : "0"
property itemexists : ""
property theFile : ""
property SaveLoc : ""
property ArchiveInput : ""
property EVNote : ""
property isRunning : "false"
property theString : ""
property noteTags : {}
property noteSource : {}
property ResourceFolder : ""
property noteName : ""
property thePDF : ""
property appType : ""
(*
======================================
// MAIN PROGRAM
======================================
*)
--SETUP
set myPath to (path to home folder)
tell application "Evernote"
try
--GET THE NOTES
set SelNotes to selection
if (SelNotes is not false) then
--TEST HOME FOLDER TO DETECT APP STORE OR DIRECTLY-DOWNLOADED VERSION
set homeFolder to (path to home folder) as text
if homeFolder contains "Containers" then
set appType to "AS"
else
set appType to "DD"
end if
--TEMP FILES PROCESSED IN EVERNOTE CONTAINER
set ExportFolder to ((path to home folder) & "Documents:Temp Export From Evernote:" as string) as string
set SaveLoc to my f_exists(ExportFolder)
--DO THE CONVERSION
my ENExport(SelNotes, ExportFolder)
end if
end try
end tell
(*
======================================
// EXPORT SUBROUTINES
======================================
*)
on ENExport(SelNotes, ExportFolder)
--SET FOLDER FOR NOTE
set theGroup to my GroupList()
tell application "Evernote"
set theNum to 0
--PROCESS THE NOTES
repeat with SelNote in SelNotes
set ArchiveInput to (ExportFolder & (theNum + 1))
set noteSource to missing value
set evernoteName to (title of item 1 of SelNote)
--TEMP CHANGE TITLE IF SLASH OR COLON IN NOTE NAME
set html_Slash to "&#" & "47;" as string
set html_Colon to "&#" & "58;" as string
set noteName to my replaceString(evernoteName, "/", html_Slash)
set noteName to my replaceString(noteName, ":", html_Colon)
set title of SelNote to noteName
--HTML EXPORT
set theNote to (SelNote) as list
export theNote ¬
to ArchiveInput ¬
format HTML
--GET NOTE INFO
set noteID to (local id of item 1 of SelNote)
set noteSource to (source URL of item 1 of SelNote)
set noteCreated to (creation date of item 1 of SelNote)
set noteModified to (modification date of item 1 of SelNote)
set noteTags to (tags of item 1 of SelNote)
set noteLink to (note link of item 1 of SelNote)
--LOOK FOR RESOURCE FOLDER
tell application "Finder"
try
set EVNote to (every document file of folder ArchiveInput whose name extension is "html")
set theHTML to POSIX path of (EVNote as alias)
set ArchiveInput to POSIX path of (ArchiveInput as alias)
set ResourceFolder to POSIX path of ((ArchiveInput & noteName & ".resources") as string)
set hfs_Resource to POSIX file ResourceFolder as text
--LOOK FOR PDF
set theFile to (every document file of folder hfs_Resource whose name extension is "PDF")
set thePDF to POSIX path of (theFile as alias)
set itemexists to my PDFDetect(theFile)
end try
end tell
--IMPORT PDF IF PRESENT…
tell application id "com.devon-technologies.thinkpro2"
if itemexists is true then
set resultRecord to import thePDF ¬
name noteName ¬
to theGroup ¬
type pdf and postscript
--ADD BACK EVERNOTE DATA
set theRecord to resultRecord
if noteSource is not missing value then
set the URL of resultRecord to noteSource
else
set the URL of resultRecord to noteLink
end if
set the creation date of resultRecord to noteCreated
set the modification date of resultRecord to noteModified
if transferTags is "ON" then
--PROCESS TAGS
-- Combine the tags into a comma-delimited list
set allTags to {}
if noteTags is not missing value then
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
repeat with eachTag in noteTags
copy (name of eachTag) to the end of allTags
end repeat
end if
set the tags of resultRecord to allTags
set AppleScript's text item delimiters to oldDelim
end if
--… OR CONVERT TO WEBARCHIVE FILE!
else
set outputTitle to (POSIX path of (ArchiveInput & noteName & ".webarchive"))
do shell script "/usr/bin/textutil -convert webarchive " & (quoted form of theHTML) & " -output " & (quoted form of outputTitle)
--IMPORT INTO DEVONTHINK
set resultRecord to import outputTitle ¬
name noteName ¬
to theGroup ¬
type html
--ADD BACK EVERNOTE DATA
set theRecord to resultRecord
if noteSource is not missing value then
set the URL of resultRecord to noteSource
else
set the URL of resultRecord to noteLink
end if
set the creation date of resultRecord to noteCreated
set the modification date of resultRecord to noteModified
--FIX NAMES IF NEEDED
set html_Slash to "&#" & "47;" as string
set html_Colon to "&#" & "58;" as string
set (name of resultRecord) to evernoteName
set devonSource to source of resultRecord
set devonSource to my replaceString(devonSource, html_Slash, "/")
set devonSource to my replaceString(devonSource, html_Colon, "/")
set source of resultRecord to devonSource
tell application "Evernote" to set (title of item 1 of SelNote) to evernoteName
if transferTags is "ON" then
--PROCESS TAGS
-- Combine the tags into a comma-delimited list
set allTags to {}
if noteTags is not missing value then
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
repeat with eachTag in noteTags
copy (name of eachTag) to the end of allTags
end repeat
end if
set the tags of resultRecord to allTags
set AppleScript's text item delimiters to oldDelim
end if
end if
end tell
set theNum to theNum + 1
set itemexists to ""
end repeat --SelNotes
--DELETE THE TEMP FILE/FOLDER
tell application "Finder" to delete SaveLoc
end tell --EV
end ENExport
--GET LIST OF GROUPS FROM DEVONTHINK
on GroupList()
tell application id "com.devon-technologies.thinkpro2"
activate
set SelGroup to display group selector "Select DEVONthink Group" buttons {"Cancel", "OK"} (*USER SELECTION FROM GROUP LIST *)
set PreGroup to SelGroup
end tell
end GroupList
(*
======================================
// UTILITY SUBROUTINES
======================================
*)
--APP RUNNING?
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
--FOLDER CHECK
on f_exists(the_path)
try
get the_path as alias
set SaveLoc to the_path
on error
-- MAKE NEW FOLDER IN APPROPRIATE LOCATION
-- APP STORE VERSION
if appType is "AS" then
set theLocation to (path to home folder) & "Library:Containers:com.evernote.Evernote:Data:Documents" as string
-- DIRECTLY-DOWNLOADED VERSION
else if appType is "DD" then
set theLocation to (path to documents folder)
end if
-- MAKE IT!
tell application "Finder" to make new folder at theLocation with properties {name:"Temp Export From Evernote"}
end try
end f_exists
--PDF CHECK
on PDFDetect(theFile)
try
set PDFalias to theFile as alias
return true
on error
return false
end try
end PDFDetect
--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
set theStringParts to text items of theString
if (count of theStringParts) is greater than 1 then
set theString to text item 1 of theStringParts as string
repeat with eachPart in items 2 thru -1 of theStringParts
set theString to theString & theNewString & eachPart as string
end repeat
end if
set AppleScript's text item delimiters to od
return theString
end replaceString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment