Evernote Bulk Tag and Untag

Description

Reader Dante wrote me asking for an assist on a script project -- A Bulk Tagger/Untagger for Evernote. This script will let you enter a tag name (or a list of names separated by commas) and then add or remove it from the notes currently selected in Evernote. This script makes good use of two of the Evernote tagging subroutines from the Code Library: One that creates a delimited list of tags and another that checks that list against Evernote's existing tags and formats it for use, creating new tags along the way if necessary!

The Code

(*
http://veritrope.com
Evernote -- Bulk Tag and Untag
Version 1.01
September 16, 2011

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/evernote-bulk-tag-and-untag

//CHANGELOG
1.01  Added Delimiter Fix from Jonathan Clark
1.00  Initial Release
*)


(*
======================================
/// MAIN PROGRAM
======================================
*)


--YOU CAN CHANGE THE DEFAULT TAG / DIALOG TITLE HERE
set defaultTag to "ENTER YOUR TAGS HERE"
set DialogTitle to "Assign Or Unassign Tags On Selected Items In Evernote"

tell application "Evernote"
    set noteList to selection
end tell

display dialog "Enter Your Tags Separated By Commas" with title DialogTitle default answer defaultTag buttons {"Assign", "Unassign", "Cancel"} default button "Assign" cancel button "Cancel" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
set dialogresult to the result
set userInput to text returned of dialogresult
set ButtonSel to button returned of dialogresult
set theDelims to {", ",","}
set EVTag to my Tag_List(userInput, theDelims)

set final_Tag_List to my Tag_Check(EVTag)

if ButtonSel is "Assign" then
    tell application "Evernote"
        assign final_Tag_List to noteList
    end tell
else if ButtonSel is "Unassign" then
    tell application "Evernote"
        unassign final_Tag_List from noteList
    end tell
end if

(* ======================================
/// TAGGING SUBROUTINES
=======================================*)

--CREATES TAGS LIST
on Tag_List(userInput, theDelims)
    set oldDelims to AppleScript's text item delimiters
    set theList to {userInput}
    repeat with aDelim in theDelims
        set AppleScript's text item delimiters to aDelim
        set newList to {}
        repeat with anItem in theList
            set newList to newList & text items of anItem
        end repeat
        set theList to newList
    end repeat
    return theList as list
    set AppleScript's text item delimiters to oldDelims
end Tag_List

--CREATES TAGS IF THEY DON'T EXIST
on Tag_Check(theTags)
    tell application "Evernote"
        set finalTags to {}
        repeat with theTag in theTags
            if (not (tag named theTag exists)) then
                set makeTag to make tag with properties {name:theTag}
                set end of finalTags to makeTag
            else
                set end of finalTags to tag theTag
            end if
        end repeat
    end tell
    return finalTags
end Tag_Check

6 Responses to “Evernote Bulk Tag and Untag”

  1. JC September 16, 2011 at 3:05 am #

    Some of the time when running this from the AppleScript Editor I get the following error “Tag names must be unique. number 21”. I can’t find a pattern for this yet, other than it only happens when I’m putting more than 1 tag in. It doesn’t matter whether these are new tags or not, nor whether they’re multi-word tags.

    • JC September 16, 2011 at 5:27 pm #

      Aha, I’ve narrowed this down further, and solved it. I tend to put a space after the comma in the list of tags, as you do when writing ordinary sentences. So in a list of tags “A, B, C” you land up with ” B” and ” C” as tags. The AppleScript decides that ” B” is new and creates it, whereupon Evernote is presumably cleverer and decides you really mean just “B”, which already exists. And hence the error message.

      My AppleScript isn’t great, but I’ve solved this by changing an early line to:

      set theDelims to {", ",","}

      It would be better to remove leading and trailing spaces from all tags.

      • Justin Lancy September 16, 2011 at 7:52 pm #

        Good Catch, Jonathan!

        Your fix has been folded into the source code above!

        Many Thanks,
        Justin

  2. Phil Dube December 16, 2012 at 11:43 pm #

    Justin,

    I tried running this applescript, but I got an error and I can’t figure out why it keeps erring.

    it says the following:

    “AppleScript Error

    Evernote got an error: AppleEvent handler failed.”

    and then it highlights the following in your code:

    “make tag with properties {name:theTag}”

    any advice?

    Thanks,

    Phil

    • Justin Lancy January 20, 2013 at 5:02 am #

      Not sure what’s going on, Phil… it works for me!

      Anyone else having an issue?

  3. Holly April 28, 2013 at 7:47 am #

    this is awesome! thanks for this…just really saved me a ton of time and work.