Hello all,
OSX 12.7.2
I’m working on an Automator quick action which runs an Applescript to peak inside a directory and see if there are any files (not folders), and if so, apply a tag (Red) to the parent directory.
My minimum version used labels, but since I’d like to expand on the idea and tag for multiple conditions, after much research on the weirdness which is tags I found the lovely FileManagerLib!
So far, I’ve been successful in getting the list of tags and updating it with new tags. However, I’ve run into problems removing tags. Basically, I want to filter for a tag to remove (ie “Red”) and keep the others in place.
Thank you in advance for anyone who might have some thoughts on what is going on!
Basically, something like the following doesn’t work:
set target_tag to "Red" as string
-- Initialize label index with existing labels
set existingTags to tags of folderAlias -- {"Orange", "Blue"}
if target_tag is in existingTags then --this works
set existingTags to (existingTags whose item is not equal to target_tag) --this does not work
-- "Error: Can’t get {"Orange", "Red"} whose item != "Red"."
-- do stuff
end if
I’ve tried all manner of looping over the list to filter out the target_tag
, including a helper method like:
on list_position(this_item, this_list)
repeat with i from 1 to count of this_list
if item i of this_list is this_item then return i
end repeat
return 0
end list_position
-- ERROR: The action “Run AppleScript” encountered an error: “Finder got an error: Can’t continue list_position.”
But this fails too. When I try to identify the classes of the individual list elements using something like the below, I get an empty result for each list element.
-- Get the class name as a string
set variableType to class of myVariable as string
-- Display the type in an alert dialog
display dialog "The variable is of type: " & variableType buttons {"OK"} default button "OK"
In the end, I am converting the tag list into a long string, and then filtering the string representation for the target_tag, and then recreating the list from the results, and feeding that into set tags
Here is my result, and its slooooooooow:
use AppleScript version "2.4"
use scripting additions
use script "FileManagerLib" version "2.3.5"
on run {input, parameters}
-- The tag that will be applied
set target_tag to "Red" as string
repeat with folderAlias in input
tell application "Finder"
set folderContents to list folder folderAlias without invisibles
set hasFiles to false
repeat with fileItem in folderContents
set filePath to (folderAlias as text) & fileItem
set itemKind to kind of (info for alias filePath)
if itemKind is not equal to "Folder" then
set hasFiles to true
exit repeat
end if
end repeat
-- Initialize label index with existing labels
set existingTags to tags of folderAlias
if hasFiles then
try
if "Red" is not in existingTags then
set end of existingTags to target_tag
add tags existingTags to folderAlias
end if
on error errMsg
display dialog "Error: " & errMsg
end try
else
if (count of existingTags) is not 0 then
try
if target_tag is in existingTags then
beep 2
-- All efforts to loop over the list have resulted in an error :/
-- The individual 'tag' indexes are not strings or any other named class that I can find. . .
-- set existingTags to (existingTags whose item is not equal to "Red") -- doesn't work
-- So we will stringify the list and filter that way
-- Set the text item delimiters to ", "
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
-- Turn the list into a string
set existingTagsString to existingTags as text
-- Set the target_tag as the new delimiter
set AppleScript's text item delimiters to target_tag
-- filter the strinifyed tag list with the target_tag to remove
set existingTagsString to existingTagsString as text
-- Create a new list from the results
set filteredTags to text items of existingTagsString
-- Reset AppleScript's delimiters
set AppleScript's text item delimiters to oldDelimiters
-- remove all tags
remove tags from folderAlias
-- add only the filtered tags
add tags filteredTags to folderAlias
end if
on error errMsg
display dialog "Error: " & errMsg
end try
end if
end if
end tell
end repeat
beep 1
return input
end run
It would be nice if FileManagerLib had a remove tag method, but is there a more robust approach to this?
Thanks again