Search code examples
macostagsapplescriptevernote

Apple Tags, Apple Script and Evernote


I'm using the below code to add files to Evernote and to use the Apple Tags assigned to those files to be converted to Evernote tags when the script adds them.

The script I am using works fine but has one problem. The file I'm targeting has 2 tags.

  • test tag
  • test

Then I run the script this is what's added In Evernote to the imported file.

tags added to Evernote

Single word tags add fine but and tags of 2 words or more put quotation marks round the tags. I've tried several different ways to remove them but to no avail so I thought I'd post here and see if you wonderful people have any ideas.

Here the code I'm using.

local targetNotebook, the_tags

set theFile to alias "Users:thomMacBook:Desktop:Inbox:outbox:test.pdf"


set the_tags to paragraphs of (do shell script "mdls -raw -name kMDItemUserTags " & quoted form of POSIX path of theFile & " | sed 's/^[()]$//g' | ruby -ne 'each = $_.strip.end_with?(\",\") ? $_.strip[0...-1] : $_.strip; puts each if each != \"\"'")

set targetNotebook to "Testing"

tell application "Finder"
set createdFileDate to (the creation date of (theFile as alias))
set modFileDate to (the modification date of (theFile as alias))
end tell
tell application "Evernote"
launch
set theItem to create note from file theFile notebook targetNotebook tags the_tags created createdFileDate
set (modification date of theItem) to modFileDate
end tell

Thanks in advance


Solution

  • This worked for me:

        set the_tags to paragraphs of (do shell script
            "mdls -raw -name kMDItemUserTags " & ¬
            quoted form of theFile & ¬
            " | egrep -o '\\w+(\\s*\\w+)+'")
    

    Testing it against one of my files, it returned the following list:

        {"Green", "Tag1", "This Tag", "Here Is Another Tag", "Tag2"}
    

    My regex expression assumes that tags are comprised only of alphanumerics and spaces (which, I felt, was a reasonable assumption to make). If you have more exotic tags than this, leave a comment and I'll get back to you.