Why would this script stop working after changing Macs?

Can anyone see any reason this Applescript I’m running in an Alfred workflow would stop working after upgrading to and transferring from an M1 Mac mini (macOS 12.3.1) to a Mac studio (macOS 12.3)? It uses the tag command line program: GitHub - jdberry/tag: A command line tool to manipulate tags on Mac OS X files, and to query for files with those tags.

set tagName to “{query}”

– get selected files in Finder
set theSelection to {}
tell application “Finder”
set theSelection to selection
end tell

–for each file
repeat with thisPath in theSelection
–get path as bash path
set theFilePath to quoted form of the POSIX path of (thisPath as text)
–add the tag
do shell script "tag --add " & tagName & " " & theFilePath
end repeat

If I run the command manually in Terminal it works; I type tag --add ‘Send to Archive’ and then drag in a folder to get the path. (This is part of a workflow to get Hazel to move the folder to my NAS for storage.)

permissions issue? can you get it to run in Script Editor or Script Debugger?
(sorry, just saw it runs in Terminal, so permissions issue would have to be Alfred, if any)

maybe use a “set the clipboard to” your combo string and investigate if the command is as you expect?

or does do shell script require that you surround the combo in single quotes?

that’s all I got (shrug emoji)

I am suspicious of the lack of quotation marks around tagName. I think your command should be

do shell script "tag --add '" & tagName & "' " & theFilePath

The addition of the two single quotes makes it match your working Terminal command.

If your previous use of this script involved single-word tags, the lack of single quotes around the tag name wouldn’t matter. That it failed when you switched to a different computer may be a coincidence.

Thanks. It doesn’t look like a permission issue as I’ve checked and all the correct permissions are in place.

I tried putting “set the clipboard to” in there and the correct path is getting in there.

I also tried it in Script Editor but it’s not working. So maybe something to do with the “do shells script” command?

More data: When running it in Script Editor, the error is
error “sh: tag: command not found” number 127

Okay, I think I have it. When calling a command from a “do shell script”, you need to put the full path so I was missing “/opt/homebrew/bin/tag”

Learn something new every day!

1 Like

This is a common problem. Pretty sure any automation I’ve used (KM, Hammerspoon, cron …) needs absolute paths to work right.

I sometimes set up automations I need to run on two different machines with different usernames (but synced file structures once you get to the home directory). That’s doable too.

Thanks for that. I will have to keep that in mind for the future.