Jump to content

Show Tagged Files


Recommended Posts

Hello everyone,

 

This is an idea I've had for a while, but I'm just unable to figure out how to write an appropriate script for.

 

What I want to achieve is this: use keyword (argument optional) to present list of all files with a particular tag (preferabbly viewable in Alfred 5.5's grid view), and where any arugment passed filters the results.

 

What I've been doing for the last few years is using a File Filter, where the Field is limited a particular value of kMDItemUserTags (the value being the tag name). And this has been working, but it has the following drawbacks:

  • After keyword input, no files are displayed until and argument is given (this is inherent to File Filter). So typically, I've been passing `.` to see all files with a particular tag, which is not only inconvenient, but also inelegant.
  • File Filters results utilize standard list view, whereas I think its preferable to view the results in the grid view.

 

I don't think this is too hard to do, but I'm not really sure where to begin. Any help would greatly be appreciated.

 

Thank you in advance!

Link to comment

I suspect that you're heading towards learning about Script Filters—because a combination of a shell script and some JSON will probably get you to where you want to be. My Search files by name workflow (that's a GitHub download link) will show you the basic sort of thing required.

 

I'm sure you're a much better programmer than I am but it took me a long time to even start getting to grips with script filters…so good luck!

 

Stephen

Link to comment

Stepehen, you're exactly right. I'll need to use shell script and then the JSON format to use with Script Filter to get to where I need to go. I figured as much, but thought I'd ask the forums to see if this sort of script has already been written. I'll take a look at your workflow to get grips on the shell scripting. Much appreciated!

 

If anyone has a script that achieves something similar, that too is appreciated! In the meantime, I'll write my own and see how it goes.

Link to comment

@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

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...