Last active
February 23, 2016 09:06
-
-
Save JMichaelTX/ff60010ecd318591f636 to your computer and use it in GitHub Desktop.
Evernote Mac (EN Mac) Get and Sort Large List of Notes by Note Property using AppleScript and ASObjC.
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
(* | |
=============================================================================== | |
[EN] Get and Sort Notes by Note Property Using "every note" Method & ASObJC [AS] | |
=============================================================================== | |
VER: 2.0 LAST UPDATE: 2016-02-22 | |
PURPOSE: | |
• Get and Sort Notes by Note Property Using "every note" Method & ASObJC | |
• This script can handle very large list of Notes | |
• Can get and sort 4,000 notes in less than 0.9 seconds. | |
AUTHOR: JMichaelTX | |
with a GIANT assist from @Shane Stanley and @hhas | |
All errors are mine. Credit for the core approach and code goes | |
to the above. | |
Find any bugs/issues or have suggestions for improvement? | |
Contact me via PM or at blog.jmichaeltx.com/contact/ | |
REQUIRED: | |
1. Mac OS X Yosemite 10.10.5+ | |
2. Mac Applications | |
• Evernote Mac 6.4+ | |
3. EXTERNAL OSAX Additions/LIBRARIES/FUNCTIONS | |
• BridgePlus 1.3.1 Script Library by Shane Stanley | |
https://www.macosxautomation.com/applescript/apps/BridgePlus.html | |
4. INTERNAL FUNCTIONS: | |
• timer() Calculate and Log Execution Time (non-essential) | |
• continueScript() Prompt User to Continue or Not | |
REF: The following were essential in the writing of this script. | |
1. Using the "every note" method pointed out by @hhas at MacScripter.net | |
http://macscripter.net/viewtopic.php?pid=184925#p184925 | |
2. Using the ASOBjC sort facility in BridgePlus by Shane Stanley | |
http://macscripter.net/viewtopic.php?pid=184936#p184936 | |
=============================================================================== | |
*) | |
use AppleScript version "2.4" -- Yosemite 10.10.5+ | |
use scripting additions | |
use framework "Foundation" | |
use BPLib : script "BridgePlus" | |
load framework | |
timer("start") | |
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
-- GET LISTS OF Evernote NOTE PROPERTIES TO SEARCH ON -- | |
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
tell application "Evernote" | |
set notebookStr to "MyBigNotebook" -- change to your Notebook | |
tell every note in notebook notebookStr | |
set titleList to its title | |
set modDateList to its modification date -- will be main sort key in this example | |
set noteLinkList to its note link -- will be used to get actual Note after sort | |
end tell -- note | |
set numNotes to count of noteLinkList | |
log numNotes | |
(* --- UNCOMMENT IF YOU'D LIKE A COMPARISION | |
--- LOG RESULTS BEFORE SORT FOR COMPARISION --- | |
log "~~~~~ BEFORE SORT ~~~~~~~" | |
set indexList to 1 | |
set modDate to item indexList of modDateList | |
set noteLink to item indexList of noteLinkList | |
set oNote to find note noteLink -- GET REF TO ACTUAL NOTE OBJECT | |
set titleStr to title of oNote | |
log "Mod Date: " & modDate & " | |
Title: " & titleStr | |
*) | |
end tell -- Evernote | |
timer("AFTER GET EN LISTS") | |
--- CONFIRM CONTINUE WITH SCRIPT --- | |
set msgStr to "Number of Notes to Process: " & numNotes | |
if not (my continueScript(msgStr)) then error number -128 -- user canceled | |
timer("start") -- restart timer after dialog | |
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
-- SORT EVERNOTE NOTE LISTS -- | |
-- (requires BridgePlus Script Lib) | |
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
--- Convert Date List To Cocoa Format --- | |
-- (Needed ONLY for Yosemite 10.10) | |
set modDateList to Cocoaify modDateList -- needed for Yosemite 10.10 | |
--- Setup the Sort --- | |
set listPairs to BPLib's colsToRowsIn:{modDateList, noteLinkList} | |
--- Do the Sort | |
set listPairs to BPLib's sublistsIn:listPairs sortedByIndexes:{1, 2} ascending:{true} sortTypes:{} | |
--- Get Sort Results --- | |
set {modDateList, noteLinkList} to BPLib's colsToRowsIn:listPairs | |
--- De-Cocoaify Date List For Yosemite 10.10 --- | |
set modDateList to ASify from modDateList | |
tell application "Evernote" | |
log "~~~~~ AFTER SORT ~~~~~~~" | |
set indexList to 1 -- should be oldest Note | |
set modDate to item indexList of modDateList | |
set noteLink to item indexList of noteLinkList | |
set oNote to find note noteLink -- GET REF TO ACTUAL NOTE OBJECT | |
set titleStr to title of oNote | |
log "Mod Date: " & modDate & " | |
Title: " & titleStr | |
end tell -- Evernote | |
timer("STOP") | |
--~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
###—————————————————————————————————————————————— | |
# continueScript() Prompt User to Continue or Not | |
# | |
# Ver 2.1 2016-02-21 | |
###—————————————————————————————————————————————— | |
on continueScript(pMsgStr) | |
beep | |
set titleStr to (name of me) & " | |
" & pMsgStr & " | |
" & "Continue or Cancel?" | |
set recAns to (display alert titleStr as critical ¬ | |
buttons {"Cancel", "Continue"}) -- last button is default | |
set strAns to button returned of recAns | |
if (strAns = "Continue") then | |
set continueBol to true | |
else | |
set continueBol to false | |
end if | |
return continueBol | |
end continueScript | |
###—————————————————————————————————————————————— | |
###—————————————————————————————————————————————— | |
# timer() Calculate and Log Execution Time | |
# | |
# Ver 1.0 2016-02-21 | |
# | |
# REF: The base ASObjC code was provided by Shane Stanley | |
###—————————————————————————————————————————————— | |
on timer(pAction) | |
(* | |
### Requires these two statements at top of main script: ### | |
use scripting additions | |
use framework "Foundation" | |
*) | |
global gTimerStartDate | |
if (pAction = "start") then | |
set gTimerStartDate to current application's NSDate's |date|() | |
log "START: " & ((current date) as text) | |
else | |
log pAction & ": | |
• " & ((current date) as text) & " | |
• EXECUTION TIME: " & (round (-(gTimerStartDate's timeIntervalSinceNow())) * 1000) / 1000.0 & " sec" | |
end if | |
end timer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script Library Required
BridgePlus Script Library v1.3.1
This is a FREE library written and maintained by well-known AppleScript and ASObjC guru Shane Stanley.