Evernote Empty Tag Remover

Description

Evernote has a built-in way to hide unused tags, but here's a quick way to permanently remove unused Tags. This script scans every tag in your Evernote account, looking for tags which have no notes associated with them. If you have a large number of tags, it will take several minutes to process the list. When it finds an "empty tag", it copies the tag name to a list -- and deletes the tag for you. When the script has finished, it creates a new note in Evernote with an alphabetically-sorted list of the tags that it deleted. If you'd like to test the script first without deleting the tags, just comment out the "delete tag (name of theTag)" line. You can then review the list in Evernote to confirm that the script is working properly!

The Code

(*
Veritrope.com
Evernote Empty Tag Remover
VERSION 1.2
February 9, 2016

// UPDATE NOTICES
     ** Follow @Veritrope on Twitter and Facebook for Update Notices! **

// SUPPORT VERITROPE!
     If this AppleScript was useful to you, please take a second to show your love here:
     http://veritrope.com/support

// SCRIPT INFORMATION AND UPDATE PAGE
     http://veritrope.com/code/evernote-empty-tag-remover
     
// REQUIREMENTS
     More details on the script information page.

// CHANGELOG
1.2 - ADDITIONS BY BRENDAN MURPHY (@BrendanP_Murphy) Added switch for parent tags, Dialog alert before deletion, Alert if no tags to delete

// TERMS OF USE:
     This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.


*)


(*

======================================

// USER SWITCHES (YOU CAN CHANGE THESE!)

======================================

*)


-- SET THIS TO "OFF" IF YOU DON'T WANT TO DELETE EMPTY PARENT TAGS
property deleteParentTags_Switch : "OFF"

(*

======================================

// MAIN PROGRAM

======================================

*)




property unusedTags : {}



set unusedTags to {}

tell application "Evernote"
   
    try
       
        --GET LIST OF EVERNOTE TAGS
       
        set theTags to every tag
       
       
       
        --DETERMINE EMPTY TAGS
       
        repeat with theTag in theTags
           
            set theNotes to {}
           
            --set theName to """ & name of theTag & """
           
            set theNotes to (find notes "tag:"" & name of theTag & """)
           
            --set theNotes to (find notes "tag:" & theName)
           
            if theNotes is {} then
               
                copy name of theTag to the end of unusedTags
               
            end if
           
        end repeat
       
    end try
   
   
   
    --GET LIST OF PARENT TAGS (IF SWITCH IS OFF)
   
    --REMOVES PARENT TAGS FROM "unusedTags" LIST
   
    if deleteParentTags_Switch is "OFF" then
       
        set parentTags to my get_parentTags(theTags)
       
        set deleteTags to {}
       
        repeat with unusedTag in unusedTags
           
            if unusedTag is in parentTags then
               
               
               
            else
               
                copy contents of unusedTag to the end of deleteTags
               
            end if
           
        end repeat
       
        set unusedTags to deleteTags
       
    end if
   
   
   
    --SORT LIST OF TAGS TO BE DELETED
   
    set sortedTags to my simple_sort(unusedTags)
   
    set oldDelim to AppleScript's text item delimiters
   
    set AppleScript's text item delimiters to return
   
    set sortedList to sortedTags as text
   
    set AppleScript's text item delimiters to oldDelim
   
   
   
    --DELETE EMPTY TAGS
   
    try
       
        if unusedTags is {} then
           
            display alert "There are no empty tags to delete" as informational ¬
               
            buttons("OK")
           
        else
           
            set dialogAnswer to display alert "DELETE TAGS CONFIRMATION!!" message "Are you sure you want to delete the following tags?" & return & "You cannot undo this action." & return & return & sortedList buttons {"Yes", "Cancel"} default button "Cancel"
           
            if button returned of dialogAnswer is "Yes" then
               
                repeat with unusedTag in unusedTags
                   
                    delete tag (unusedTag)
                   
                end repeat
               
                set n to create note with text sortedList title "Deleted Tags"
               
                display alert "Tags have been deleted!" as informational ¬
                   
                buttons("OK")
               
            else
               
                display alert "Process has been cancelled" as informational buttons {"OK"}
               
                return
               
            end if
           
        end if
       
    end try
   
   
   
end tell





(*

======================================

// EVERNOTE SUBROUTINES

======================================

*)




--GET LIST OF PARENT TAGS

on get_parentTags(theENtags)
   
    local parentTags, theENtags, theENTag, this_error, sortedParents
   
    set parentTags to {}
   
    tell application "Evernote"
       
        try
           
            repeat with theENTag in theENtags
               
                if parent of theENTag exists then
                   
                    if name of parent of theENTag is in parentTags then
                       
                       
                       
                    else
                       
                        copy name of parent of theENTag to the end of parentTags
                       
                    end if
                   
                end if
               
            end repeat
           
        on error error_message number error_number
           
            set this_error to "Error: " & error_number & ". " & error_message as string
           
        end try
       
    end tell
   
    set sortedParents to my simple_sort(parentTags)
   
    set parentTags to sortedParents
   
    return the parentTags
   
end get_parentTags







--SORT SUBROUTINE

on simple_sort(my_list)
   
    set the index_list to {}
   
    set the sorted_list to {}
   
    repeat (the number of items in my_list) times
       
        set the low_item to ""
       
        repeat with i from 1 to (number of items in my_list)
           
            if i is not in the index_list then
               
                set this_item to item i of my_list as text
               
                if the low_item is "" then
                   
                    set the low_item to this_item
                   
                    set the low_item_index to i
                   
                else if this_item comes before the low_item then
                   
                    set the low_item to this_item
                   
                    set the low_item_index to i
                   
                end if
               
            end if
           
        end repeat
       
        set the end of sorted_list to the low_item
       
        set the end of the index_list to the low_item_index
       
    end repeat
   
    return the sorted_list
   
end simple_sort

2 Responses to “Evernote Empty Tag Remover”

  1. marcelo February 10, 2011 at 8:05 pm #

    worked perfectly

  2. Can Koklu July 13, 2011 at 4:38 am #

    works fine.. very useful..