@FireFingers21 Hey, thanks for whipping that up!
In the end, I decided to try my own hand at it, for two reasons: for one, I always try to make workflows without any dependencies--to keep things clean, simple, and transposable; and second, because I think problems are great opportunities to learn new things (despite me wanting an easier out initially by creating this thread 😅)
The good news is that I was able to solve my problem! This was my first time writing anything in Bash, so any feedback is welcome. Perhaps I could use better methods or clean up the logic.
@Stephen_C Thanks for the link from StackOverflow you posted. I used the method described to retreive tagged files from the system. Much appreciated.
Okay, here is my script. As mentioned, it works perfectly, but let me know if it can be better in someway. If not, then I hope it can help someone else out in the future.
# Specify Tag to Show
tag="Example"
tagColorEmoji="🟢"
# Initialize an array that will be comprised of Script Filter rows
filterRowsArray=()
# Get a list of tagged items (as file paths)
taggedItems=$(mdfind "kMDItemUserTags == $tag" -onlyin ~/)
# Loop through each tagged item to contruct a row object (as a string) and add to filterRowsArray.
while IFS= read -r file_path; do
filterRowsArray+=("{
\"title\": \"$(basename "$file_path")\",
\"subtitle\": \"$tagColorEmoji $tag\",
\"arg\": \"$file_path\",
\"type\": \"file\",
\"icon\": {
\"type\": \"fileicon\",
\"path\": \"$file_path\"
}
}")
done <<< "$taggedItems"
# Convert the filterRows array to a comma-separated string (JSON)
filterRowsJSON=$(IFS=,; echo "${filterRowsArray[*]}")
cat << EOB
{"items": [$filterRowsJSON]}
EOB