Select date string and replace with alternate date format

I am trying to standardise my naming conventions in certain Evernote notes. Some are prefaced with a date in the format yyyy-MM-dd but due to a limitation in one of my apps, I need to switch to yyyy/MM/dd format. I reckon KM is the best approach and would like to basically:

  1. position cursor anywhere in a string e.g. 2014-10-15
  2. hit hot key trigger
  3. KM selects the string e.g. 2014-10-15
  4. KM replaces the string with yyyy/MM/dd e.g. 2014/10/15

The “position cursor anywhere” makes it somewhat difficult. If you can restrict yourself to clicking within the year it will dramatically simplify this. In that case, you would do:

  • Option-Shift-Left Arrow
  • Option-Shift-Right Arrow
  • Option-Shift-Right Arrow
  • Option-Shift-Right Arrow
  • Command-C
  • Pause may be needed here
  • Search and Replace Clipboard ‘-’ with ‘/’
  • Command-V <-- depending on the app, you may need a pause, or may get in to clipboard caching issues here

Hey Sherman,

You imply but don’t specifically say that you’re working with the Title field in Evernote. Are you?

If so, then this task is simple.

------------------------------------------------------------------
# Get the title of the selected note in Evernote.
------------------------------------------------------------------
tell application "Evernote"
  set _selection to selection
  if length of _selection = 1 then
    set _note to first item of _selection
  else
    error "Problem with number of notes selected!"
  end if
  tell _note
    set _title to title
  end tell
end tell
tell application "Keyboard Maestro Engine"
  try
    set value of variable "eNoteTitle" to _title
  on error
    make new variable with properties {name:"eNoteTitle", value:_title}
  end try
end tell
------------------------------------------------------------------
# Set the title.
------------------------------------------------------------------
tell application "Evernote"
  set _selection to selection
  if length of _selection = 1 then
    set _note to first item of _selection
  else
    error "Problem with number of notes selected!"
  end if
  tell _note
    set title to newTitle
  end tell
end tell
------------------------------------------------------------------

The first script gets the title of the selected note into a KM variable.

From there you find/replace in the variable using your correct date format.

The second script sets the title of the selected note.

Done!


Best Regards,
Chris

Thank you Peter. Clicking within the year is an easy compromise. This should do the trick nicely. Cheers!
…sherm

Thank you Chris! I gather this an Apple script which I haven’t delved into yet. I take it this resolves the “click anywhere in the title” use case. I’m keen to have a play with this but think I need to learn a bit more about scripting and KM. Thanks!
…sherm

Hey Sherman,

Yes. My scripts let you directly get and set the title without having to muck about with brute-forcing the UI. Not every application is scriptable of course, but when it’s possible scripting is the better option for speed and reliability.

So. You can get the title with AppleScript and place it in a KM variable - massage it with KM - and put it back into Evernote with AppleScript - all from one KM macro.

Use the Applescript Editor app on your system to test AppleScripts. You can see results as they happen in applications and in the result pane of the editor.

Use an ‘Execute AppleScript Action’ in KM to add AppleScripts to a macro.

The ‘Execute AppleScript Action’ has two modes: run-text-script and run-script-file.

On my own system I always use run-script-file, because it’s faster.


Becoming proficient with AppleScript takes some time, effort, a good book, and some mentoring.

The book I most often recommend (amongst 3 or 4):

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X

User Communitiy:

Applescript Users List
MacScripter.net

A basic tutorial:

AppleScript: Beginner’s Tutorial
Companion Video


Best Regards,
Chris

Pardon my ignorance, but I can’t figure out how to set the value of newTitle. I’ve successfully passed the eNoteTitle to KB and replaced the text, but I can’t figure out how to pass the value back to the second script.

Hey Sherman,

Here's a working example in which I've simplified the first AppleScript a bit by using the 'Execute AppleScript Action' itself to save the script output to a KM variable.

Although I've used text-scripts in this example, I always use script-files on my own system - because they are noticeably faster.

Best Regards,
Chris


Evernote { Massage Title of Selected Note }.kmmacros (6.6 KB)

I also put in some error-checking which is always a good idea.

-ccs

Thanks Chris I got it working. Much appreciated!

Now I have to figure out pattern matching so that I can extract yyyy/mm/dd and return yyyy-mm-dd. I’ve just gone with the heavy handed replace ‘/’ with ‘-’ which is fine if also a bit brutal. I’m thinking python might be the better tool for massaging text, although I don’t know if applescript or kbm works with python.

Also, is there a way to process a selection of notes. For example, take the top selection, process it and then remove from selection list, then process next until there is nothing selected.

Hey Sherman,

Find:
(\d{4})/(\d{2})/(\d{2})

Replace:
$1-$2-$3

Best Regards,
Chris

Hey Sherman,

If you want to do this efficiently then download and install the Satimage.osax (a small extension that adds regular expressions and other goodies to AppleScript).

tell application "Evernote"
  set _sel to selection
  repeat with theNote in _sel
    tell theNote
      set title to change "(\\d{4})/(\\d{2})/(\\d{2})" into "\\1-\\2-\\3" in (get title) with regexp
    end tell
  end repeat
end tell

Then you don’t have to go KM(AppleScript–>KM–>AppleScript) - you can just run one Execute AppleScript Action.

Note that this script is very quick and dirty with no error-checking, but I want you to see how much you can do with very code.

If you choose to go that route let me know, and I’ll write a script that has a few more smarts.

Best Regards,
Chris

That is just awesome Chris! I download Smile to have a play and that come with the Satimage.osax. I saved your script to a file and created a KB macro which very quickly got a few hundred note titles changed. I also found some material so I can learn more about regular expressions.

So not only did I get the job done, I’m learning more about this stuff as I go. I’d love to see a script with more smarts so I can get a bit smarter too :slight_smile:

Thanks heaps!
…sherm