Script to add tags to multiple items - DONE!

Figured it out, if anyone else has been stumped:

tell application "DEVONthink Pro"
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
			display dialog "Enter tags to add:" default answer "" buttons {"Cancel", "OK"} default button 2
			set newTag to the text returned of the result
			if newTag is not "" then exit repeat
		end repeat
		repeat with this_item in this_selection
			if exists tags of this_item then
				set current_tags to the tags of this_item
				set new_tags to current_tags & newTag
				set new_tags to newTag
			end if
			set the tags of this_item to new_tags
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
				display alert "DEVONthink Pro" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

For each item, checks to see if any tags. If so, adds to the list. Will not replace or duplicate existing tags.

most excellent. will come in handy. thanks acoyne.

I tried space and comma separated tags:

tag1 tag2 tag3
(or) tag1, tag2, tag3

and the result was

“tag1 tag2 tag3” <- one tag

haven’t tried to debug yet. :slight_smile: will try when i use it next

eventually will change the display dialog errors to use growl. and add some growl results. [count] tags (…) have been added to [count] items.

fyi, i also tried the script with focus in a note. wouldn’t that be the current selection? when in a smart folder (recent), the note disappeared from the smart folder and the current tab became blank. focus still there but without tab name or contents. empty.

hope i have some time to get around to some editing and a post of my version.

Comma separation should work. At least it does for me.

Or check that: it works when you first lay down some tags. If you then add more, it doesn’t work: the new tags all get strung together. No idea why.


yes thats it, thanks for the discovery. i tried all new tags. i’m just getting started. i’ll trying to get to it sometime. seems like fun but it’d probably be better for me to write in devonthink than play with it. :slight_smile:

The problem with the separation of multiple new tags is created because the result from the display dialog command is not converted into a list. The following script solves this problem.


display dialog "Enter tags to add (separated by commas):" default answer ""
set newTags to text returned of the result

set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set tagList to text items of newTags
set AppleScript's text item delimiters to old_delimiters

tell application id "com.devon-technologies.thinkpro2"
		set these_items to the selection
		if these_items is {} then error "Please select some contents."
		repeat with this_item in these_items
			set tags of this_item to (tags of this_item) & tagList
		end repeat
	end try
end tell

Just as a FYI, DEVONthink 2.1 ships with a script (Script menu>Tags> Add tags to selection) that will add tags to a multiple selection.

Nice :slight_smile: