How to let custom meta data (boolean) change if certain tag exists (w Applescript)

Hi,

I was trying to have custom meta data (boolean) changed if a certain tag exists. I tried to adjust an applescript and managed to have the boolean item changed to true. But it always changes to true, no matter if the specified tag exists.

Can anyone help?

In the script below the variable “xxx” is for the tag. The variable “yyy” is for the custom meta data (boolean).

tell application id "DNtp"
	set theRecords to selected records
	if theRecords = {} then error "Please select some records"
	repeat with thisRecord in theRecords
		set theTag to "xxx"
		set md to true
		if exists theTag then
			add custom meta data (md as boolean) for "yyy" to thisRecord
		end if
	end repeat
end tell

Thank you!

I doubt that “if exists theTag” does what you think it does. I’d guess that is always true since you defined theTag. Would be weird if it didn’t exist after that.

You must check if it is a part of the tags property of the current record.

Also, setting theTag to the same value every time in the loop is pointless. Do it once outside of the loop.

Thanks for your suggestions :+1:. I had another trial/error session but succeeded:

property tagDelimiter : "xxx"

tell application id "DNtp"
	if (selected records) is {} then return
	repeat with theRecord in (selected records)
		set recordTags to tags of theRecord
		repeat with theTag in recordTags
			if theTag begins with tagDelimiter then
				set md to true
				add custom meta data (md as boolean) for "yyy" to theRecord
				exit repeat
			end if
		end repeat
	end repeat
end tell

Your code can be simplified if you’re just trying to check if a file has a certain tag.

tell application id "DNtp"
	if (selected records) is {} then return
	repeat with theRecord in (selected records)
		if (tags of theRecord) contains "pdfs" then
			add custom meta data "Journal" for "Format" to theRecord
			--- And/or do other stuff
		end if
	end repeat
end tell

This line could even be a one-liner without the need for a closing end if

if (tags of theRecord) contains "pdfs" then add custom meta data "Journal" for "Format" to theRecord

As a continuation of the lesson, you could even use this syntax to pre-filter the items to loop through…

set tagMatchedFiles to (selected records whose (tags contains "pdfs"))
repeat with theRecord in tagMatchedFiles
add custom meta data "Journal" for "Format" to theRecord
end repeat
1 Like

Great, thank you!

I was also able now to uncheck entries which don’t have the tag (which I tried in vain for the last hour):

tell application id "DNtp"
	if (selected records) is {} then return
	repeat with theRecord in (selected records)
		set tagMatchedFiles to (selected records whose (tags does not contain "xxx"))
		repeat with theRecord in tagMatchedFiles
			set md to false
			add custom meta data (md as boolean) for "yyy" to theRecord
			exit repeat
		end repeat
	end repeat
end tell

You’re welcome!
You can have a unnecessary loop. Change your code to this…

tell application id "DNtp"
	if (selected records) is {} then return
	set tagMatchedFiles to (selected records whose (tags does not contain "xxx"))
	repeat with theRecord in tagMatchedFiles
		set md to false
		add custom meta data (md as boolean) for "yyy" to theRecord
		exit repeat
	end repeat
end tell