I have a script which reads an items Finder tags, and outputs the results as a string:
{“VENDOR”, “ACCOUNT TYPE”, “DOCUMENT TYPE”, “DATE”}
I’d like to break that string up into variables for each of the tags (i.e. “VENDOR” = tag1, etc), so that the document can be renamed based on the tags, but not in the sequence in which Finder displays tags. I intend to accomplish that with Hazel.
I’m looking for sample scripts that can guide me on this, or even the answer. I’m in the processing of switching from Win11 to Mac, and while I have extensive VBA scripting experience, the learning curve is steep, and time is tight right now. I am aware of many of the resources out there - I just don’t have time to dig into them all right now.
That looks like it is already a list, perhaps from a log statement or osascript. You can step through the items with a repeat statement, but usually you would just use list indexes, or perhaps set up a record (dictionary/hash) instead of an arbitrary number of variables.
User tags are also available from Spotlight or the com.apple.metadata:_kMDItemUserTags extended attribute. I’m not sure if the plist keys get rearranged, but Spotlight items seem to be in the order they were created - do you have an example showing how you are hoping to plug in the items?
red_menace has answered the OP’s question, but I thought I would provide a working example:
set theList to {"VENDOR", "ACCOUNT TYPE", "DOCUMENT TYPE", "DATE"}
set theVendor to item 1 of theList --> "VENDOR"
set theAccountType to item 2 of theList --> "ACCOUNT TYPE"
set theDocumentType to item 3 of theList --> "DOCUMENT TYPE"
set theDate to item 4 of theList --> "DATE"
While your current application seems manageable, Apple never having informed the Finder’s dictionary that tags exist, scripting them is (understatement) challenging.
You may wish to check out Tag, a labor-of-love command line tool for scripting the hell out of macOS tags. To install it, first open Terminal and install the Homebrew package manager, then simply issue
brew install tag
at the command line.
The escapage required to run shell commands in AS is a bit eye bleeding at first glance, but manageable once you get the hang of it.
For example…
set vendorFiles to paragraphs of ¬
(do shell script ¬
"/opt/homebrew/bin/tag --find " & quoted form of "VENDOR")
issues
/opt/homebrew/bin/tag --find 'VENDOR'
which will return posix paths for all VENDOR tagged files on your system. Getting paragraphs of the resulting text produces an AS list rather than a text block, making it easy to do things like
repeat with thisFile in vendorFiles
do stuff
end repeat
In general, do shell script extends AppleScript’s sometimes inadequate abilities to include anything doable in macOS’ Unix underpinnings. If you’re new to macOS and enjoy automation, you may also wish to have a look at FastScripts, BetterTouchTool, and Keyboard Maestro, a wide and deep automation tool that integrates with but isn’t limited to AppleScript.
I’m not a fan of Homebrew, but does that still work with current systems? The repository hasn’t been updated in a while, and Apple has moved the Finder tag/color list at least a couple of times since then (don’t know where they are hiding it now), so at least that part is way out of date. Looks like they use a bunch of NSURL and Spotlight stuff though.
MacPorts likely has the same package if you prefer it, and “tag” works fine.
One can access Spotlight from Terminal using
mdfind kMDItemUserTags == 'VENDOR'
and whatnot, but while that will find tagged files, it won’t allow you to manipulate them. So far as I know “tag” is the only way to script the actual utilization of tags.
This topic got me looking at some of my tag handlers, where I noticed that the SQLite database that Apple had started using for Finder’s tags in Monterey is empty in Sonoma, so I was just wondering. The Tag utility looks like it mostly uses NSURL to get/set file resources - you can read and write tags the same way via AppleScriptObjC, and you can also use the xattr and plutil shell utilities if you prefer the command line.
It isn’t. You can get the tags with AppleScriptObjC.
This script reads the tags of a given file and displays one by one in a loop or “No tags found” if the item doesn’t have tags
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
set filePath to POSIX path of (choose file)
set fileURL to current application's NSURL's fileURLWithPath:filePath
set {_, theTags, theError} to fileURL's getResourceValue:(reference) forKey:(my NSURLTagNamesKey) |error|:(reference)
if theError is missing value then
if theTags is missing value then
display dialog "No tags found"
else
repeat with aTag in theTags as list
display dialog aTag
end repeat
end if
else
display dialog (theError's localizedDescription()) as text
end if