Applescript to delete tags?

Hi there –

Would it be possible for someone (applescript wise) to build one handy script that allows selected files to have their tags deleted (aka untagging)? I just spent a couple of hours deleting tags and I’m inclined not to use them at all (except the group name automatic tags) until they work properly.

– MJ

I am also out after this type of script :slight_smile:

What’s wrong with simply deleting the user tag folders from the “Tags” folder?

That doesn’t remove tags from “selected” documents.

Exactly. Imagine that I have 30 files from which I want to delete tags (but I don’t want to delete the same tags from other files). If I’m not mistaken I now have to go over them one by one until that is done. :frowning:

You could remove them from the enclosing “tag” group (assuming that you want to delete the same tag for all of them).

Yes, but what I normally want to do is delete tags in several files, but maintain the same tags in some other files.

That’s possible doing the way described above:

Let’s say Tags > A contains the files 1, 2, 3, 4, then “trashing” 1 & 2 (by selecting them in the “group” Tags > A and moving them to the trash or simply pressing the Delete key), removes the Tag A from 1 & 2 but 3 & 4 still have that tag.

I’ll try that. Thanks.

I’m sorry , but your workaround isn’t perfect. Because, if you set protection flag, notes will not disappear in trash within empty function.

btw: Removing tags on file notes via removing file notes in tag folders seems a bit irritating. Because, ever I mean I remove file notes, not tags. What a pity there are no more basic functions to manipulate tags.

Feel free to use my script. It’s a modified version of Christian’s ‘add tags’-Script.


-- Remove tags to selection
-- Created by Snare Dubes on Mon Feb 19 2012.
-- Copyright (c) 2012. All rights reserved.

tell application id "com.devon-technologies.thinkpro2"
	try
		set theSelection to the selection
		if theSelection is {} then error "Please select some items."
		
		repeat
			set theTag to display name editor "Remove Tags" info "Tags (separated by semicolons):"
			if theTag is not "" then exit repeat
		end repeat
		
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ";"}
		set toRemoveTags to text items of theTag
		repeat with theRecord in theSelection
			set origTags to tags of theRecord
			set reduTags to {}
			repeat until origTags = {}
				set oneTag to (first item of origTags)
				set origTags to (rest of origTags)
				considering case
					if oneTag is not in toRemoveTags then
						set reduTags to reduTags & {oneTag}
					end if
				end considering
			end repeat
			set tags of theRecord to reduTags
		end repeat
		set AppleScript's text item delimiters to od
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

1 Like

This works for me:

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some items."
		
		set theTags to name of every parent of current database whose tag type is ordinary tag
		
		--set myTags to (choose from list theTags with multiple selections allowed)
		
		repeat with this_item in this_selection
			set theTags to tags of this_item
			if (count of items of theTags) > 0 then set the tags of this_item to ""
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

Sorry to bump this up again. Is there still no simpler way to remove a specific tag from a record’s tag list than the scripts above?

In case of multiple selected items you could use Tools > Batch Processing and the Remove Tags action.

Thank for the suggestion but I was not specific enough. I meant: “Is there still no simpler way to remove a specific tag by script from a record’s tag list than the scripts above?”

Thing is, I have a script up and running that moves files depending on their tags—one single sorting Smart Rule that executes the script instead of many sorting Smart Rules which have to be kept in the right order, especially when more sorting Smart Rules get added.

What’s missing in my script is only the last step of removing those “destination tags” as I like to call them.

You could add the Remove Tags action to the smart rule too. Or is the list of tags which should be removed dynamic?

Well, the list of tags is not overly dynamic. But it might be expended anytime if necessary. And I’d like to have to access to only one place, i. e. the script.

This is my solution (theItem is the tag of the record and theList is the tags of the record):

on removeItemfromList(theItem, theList)
	set theNewList to {}
	repeat with n from 1 to count of theList
		set theNewItem to item n of theList
		if theNewItem is not theItem then set theNewList to theNewList & theNewItem
	end repeat
	return theNewList
end removeItemfromList

It’s circuitous and I would have preferred code like remove theTag from theTags but it works. Distribution by tag script is finished!