Jump to content

Scripting on a Mac (Applescript)


DTLow
Go to solution Solved by DTLow,

Recommended Posts

  • Level 5*

Instead of hijacking other discussions, I am using this discussion to discuss general scripting on a Mac5aabe5fad2569_ScreenShot2018-03-16at08_41_01.png.afceefd741509c8e191f29cd6c41f1f8.png

Evernote/Mac has a scripting layer built in to the app.  
This feature is accessed using AppleScript with the app Script Editor

 

There's documentation at https://dev.evernote.com/doc/articles/applescript.php

and the Script Editor displays a dictionary for each app5aabe7bdc6837_ScreenShot2018-03-16at08_48_58.png.60683c848be872fe1bdff563e4ad7c95.png

 

Resources

  1. Storage and Menu for accessing scripts
  2. Import Folder
  3. Script: Identify the Note OS File Folder
  4. Script - Append Metadata To Note rContents
  5. Script - Email to Evernote
  6. Script - Extract Selection to Note
  7. Script - Search Titles and Insert Link
  • Like 1
Link to post
  • Level 5*
  • Solution

Two points about the storage and access of Applescripts.5aab78258ccd5_ScreenShot2018-03-16at00_49_45.thumb.png.10509ddb69ff81b4f1c00638cb209b0f.png.a905a79d06a4547e34494e8cbae34573.png.0472805c3f3cae1f81e8619ef13f184a.png

I show a menu of scripts in my top menu bar.

  1. I store the Evernote scripts in folder /Users/<user>/Library/Scripts/Applications/Evernote
  2. I turned on the menu in Script Editor > Preferences.
    5aab780d1e86c_ScreenShot2018-03-16at00_47_42.png.38e7e0e95982a05d77ac2229cd3d0dfc.png.3a056a613a3efab020cb23482db15ca4.png
Link to post
  • Level 5*

Script - Identify OS folder the note contents are stored in.5aad69753c843_ScreenShot2018-03-17at12_11_55.png.b2983c9c684c5301b436bc5f11150fbf.png

On a Mac, each note's content is stored in a separate OS folder.
The top level folder can be identified using Option Key Help > Troubleshooting

 

The metadata is stored in database LocalNoteStore.sqlite
The contents of each note are stored in <some folder> and includes:

  • file content.enml
  • attachments5aabeaa332e1f_ScreenShot2018-03-16at09_02_16.png.335a74946ca446621042c56c5760dd8d.png
  • other files (snippet, card image, ...)

 

The script I use to identify <some folder> is below
Evernote_Note_Folder.scpt
Note, the database folder is hardcoded; update this for your own installation

Note: For external edits, the data accessed is a copy of the master version maintained on the Evernote servers.  After verifying my update, l make a dummy change (like adding a space in the note).  This ensures the update gets registered and sync'd to the server

5ab3034a91d7f_ScreenShot2018-03-21at18_13_00.png.cf917e358f20cffe3ba61689ffab7108.png

 

Link to post
  • Level 5*
5 hours ago, michaelbierman said:

Yup. Another option is to go to System Preferences > Keyboard > Services and you can set a keyboard shortcut of your own choosing. I made some minor improvements yesterday and uploaded them to github. 

I actually bought a program Fastscripts that assigns a keyboard shortcut to any script.

It works great, but I discovered I can't remember all these shortcuts; so most of  the time I refer to a menu

  • Like 1
Link to post
  • Level 5*

For automatic scheduling, I use the Mac LaunchAgents feature to launch scripts at a scheduled time.
This includes my morning alarm script, and daily backup script.

There's documentat at https://en.wikipedia.org/wiki/Launchd 
but basically it's a .plist file in folder /Users/david/Library/LaunchAgents
5ab3fb13262bc_ScreenShot2018-03-22at11_50_37.png.be96a8cd16de5ab91f2007b022a979b1.png

 

I use app Lingon to simplify the setup

5ab3fa6381469_ScreenShot2018-03-22at11_45_58.png.764d89b65aa7b4fd24b2373831515397.png

 

Link to post
  • Level 5*

Script: Boilerplate code for processing notes5abd4cd86635c_ScreenShot2018-03-29at13_27_24.png.8f6a5f6885fb2994f2432d4e81310140.png

The example shows procesing for

  1. Selected Notes; 
  2. Notes meeting a search query
Link to post
  • 1 month later...
On 3/16/2018 at 9:20 AM, DTLow said:

Two points about the storage and access of Applescripts.5aab78258ccd5_ScreenShot2018-03-16at00_49_45.thumb.png.10509ddb69ff81b4f1c00638cb209b0f.png.a905a79d06a4547e34494e8cbae34573.png.0472805c3f3cae1f81e8619ef13f184a.png

I show a menu of scripts in my top menu bar.

  1. I store the Evernote scripts in folder /Users/<user>/Library/Scripts/Applications/Evernote
  2. I turned on the menu in Script Editor > Preferences.
    5aab780d1e86c_ScreenShot2018-03-16at00_47_42.png.38e7e0e95982a05d77ac2229cd3d0dfc.png.3a056a613a3efab020cb23482db15ca4.png

This is awesome! How did you get the Evernote Scripts section to appear on the dropdown of the scripts tool in the top toolbar? Asking for a friend. 

Link to post
  • Level 5*
10 minutes ago, Sprintup said:

How did you get the Evernote Scripts section to appear on the dropdown of the scripts tool in the top toolbar?

The scripts are filed in a separate folder for each application and displayed for whatever application is open.5aedd4999fc1d_ScreenShot2018-05-05at08_57_08.png.9506c6527541ea0df6f5f4592ffc28fc.png
If you're working in Evernote, you see the Evernote scripts.

Link to post
  • 3 months later...
  • Level 5*

Script - Backup Evernote Data

The backup uses the export feature with the code

try
with timeout of (10 * 60) seconds
     
export theNotes to fileBackup format HTML
end timeout
end try

For selection criteria, my daily incremental backups use    set theNotes to find notes ("updated:day-1 -updated:day")
My weekly full backups are a more complicated; a separate export is required for each notebook

set allNotebooks to every notebook
repeat with currentNotebook in allNotebooks
     set notebookName to (the name of currentNotebook)
     set theNotes to every note in notebook notebookName
end repeat

Link to post
  • Shane D. featured, unfeatured and featured this topic
  • 2 weeks later...
  • Level 5*

Script - Append Metadata To Note Contents

Currently the metadata is not stored with the note contents.  1448312769_ScreenShot2018-09-04at12_05_05.png.93754c44da4636378ff4760d597e5108.png
On a Mac, the metadata is stored separately in an sqlite database
My objective is to have this data always available, so I append a copy to the bottom of each note

The script runs nightly for all new/changed notes, or on a one-of basis

 

tell application "Evernote"
     set
theNotes to get selection
     
repeat with theNote in theNotes

set thedate to modification date of theNote

set newText to "<hr/>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">Title: " & title of theNote & "</span></span></div>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">Notebook: " & name of notebook of theNote & "</span></span></div>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "Updated:  ud" & my FormatDate(modification date of theNote)
set newText to newText & "   Created: cd" & my FormatDate(creation date of theNote)
set newText to newText & "   Subject: sd" & my FormatDate(subject date of theNote)
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "Reminder: rd" & my FormatDate(reminder time of theNote)
set newText to newText & "   Completed:  xd" & my FormatDate(reminder done time of theNote)|
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set theTags to tags of theNote
repeat with theTag in theTags
     
set theTagName to name of theTag
     
set newText to newText & "Tag: " & theTagName & "   "
end repeat

 append theNote html newText
set modification date of theNote to thedate

end repeat
end tell

Link to post
  • 2 weeks later...

Hi! I was wondering if there is a way to have the tag of a note change based on it's reminder time. For example if I create a note with the tag "scheduled" and set the reminder to April 5. I want the tag to change from "scheduled" to "open" on April 5th. Can this easily be done? 

 

Link to post
  • 3 weeks later...
  • Level 5*
On 9/14/2018 at 12:26 PM, Ben68982 said:

Hi! I was wondering if there is a way to have the tag of a note change based on it's reminder time. For example if I create a note with the tag "scheduled" and set the reminder to April 5. I want the tag to change from "scheduled" to "open" on April 5th. Can this easily be done? 

Sorry to have overlooked the post.

Yes, it's easy to use scriting to retrieve the reminder time and assign tags
The actual commands are 

set theReminderTime to reminder time of theNote
assign tag theTag to theNote
unassign tag theTag from theNote

>>I want the tag to change from "scheduled" to "open"

Instead of tag "scheduled", we can use search  reminderOrder:* or ReminderTime:*
                   tag "Open" , we can use search  -reminderOrder:* or -ReminderTime:* (negation)

I'm a tagger, but I don't want to duplicate the trigger; I use Reminders to identify my tasks, open or scheduled

 

 
  •  
Link to post
  • 3 months later...
On 9/5/2018 at 5:07 AM, DTLow said:

set thedate to modification date of theNote

set newText to "<hr/>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">Title: " & title of theNote & "</span></span></div>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">Notebook: " & name of notebook of theNote & "</span></span></div>"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "Updated:  ud" & my FormatDate(modification date of theNote)
set newText to newText & "   Created: cd" & my FormatDate(creation date of theNote)
set newText to newText & "   Subject: sd" & my FormatDate(subject date of theNote)
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "Reminder: rd" & my FormatDate(reminder time of theNote)
set newText to newText & "   Completed:  xd" & my FormatDate(reminder done time of theNote)|
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set newText to newText & "<div><span style=\"color: rgb(4, 51, 255);\"><span style=\"font-size: 12px;\">"
set theTags to tags of theNote
repeat with theTag in theTags
     
set theTagName to name of theTag
     
set newText to newText & "Tag: " & theTagName & "   "
end repeat

 append theNote html newText
set modification date of theNote to thedate

I'm really new to this; I was only able to set up the Evernote import folder thanks to your script (which is so helpful). So I want to know how do I use this script to set append metadata? When I pasted this into Script Editor it says, "Expected “,” or “)” but found class name." Thank you for your time.

Screen Shot 2020-10-07 at 7.19.55 am.png

Link to post
  • Level 5*
On 10/6/2020 at 2:21 PM, esala said:

I'm really new to this; I was only able to set up the Evernote import folder thanks to your script (which is so helpful). So I want to know how do I use this script to set append metadata? When I pasted this into Script Editor it says, "Expected “,” or “)” but found class name."

That's a partial script
I see "subject date of theNote" so this code should be within a        tell application "Evernote"    .....    end tell
I also see a call to subroutine FormatDate, so that needs to be defined

Link to post
  • 4 months later...

The reason this is happening is that - at least as of version 10.8.5 - the Mac EN is not supporting Applescript - if you try to use Script Editor to open the app's dictionary, you'll see it's unavailable (ie greyed out).

Don't know if this is on purpose, or someone just forgot to include the appropriate AS Library resource in a recent build.

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...