Jump to content

Passing '*' as {query} to File Filter... bad?


Recommended Posts

I've got a workflow where at some point I want to present a File List showing 10 most recently created images from the last 3 days, from my Desktop and Downloads folders combined.

 

The File Filter object is set like this:

image.png.7d16f867dd69ca63cb01f5d3511aba84.png

image.png.f0f33e64c4d7ff855499f8cdbceb871c.png

image.png.1590569bfb82fc8ef7b72d84457e0593.png

This only works if I pass something in via {query}. If {query} is blank, nothing is shown until I either type a * or a [space] or something. So my kludgy workaround is to set {query} to "*" right before before this step. It seems to work fine but feels hacky and wrong. Is there a better way to do this?

image.png.e618637940e716cf97d20c7096fd20f9.png

 

Edited by luckman212
Link to comment

Man I really went down a rabbit hole on this one. But, thanks for the guidance @deanishe !

 

I finally got this working the way I want with a Script Filter (as you suggested) and some help from jq. There were some rough spots, because mdfind -onlyin does not accept multiple folders, nor a complex query using kMDItemPath. So I had to string together 2 commands into a variable. It was also "fun" wrestling with jq's syntax to parse the tab separated input, filter out nulls etc. This is most certainly better done in Python but I wasn't sure how to get the Spotlight results and was in a bit of a hurry. So yeah I'm sure there are better ways to do this...

 

In case anyone is curious, here's how I did it:

 

/bin/bash

tab=$'\t'
query='kMDItemContentTypeTree == "public.image"d && kMDItemContentModificationDate >= $time.today(-3d)'
results=$({ mdfind -onlyin ~/Desktop $query; mdfind -onlyin ~/Downloads $query; })
if [ -z "$results" ]; then
	cat <<-EOJ
	{ "items": [{
		"title": "No recent images found",
		"valid": false,
		"subtitle": "no recent images found in Desktop or Downloads",
		"icon": { "path": "error.png" }
	}]}
	EOJ
	exit
fi

tsv=$(while IFS= read -r FILE; do
	fname="${FILE##*/}"
	fullpath="$FILE"
	dirname="$(dirname "$FILE")"
	fdate="$(date -r "$FILE" +'%a %-m-%-d-%Y  %-l:%M%p')"
	echo "$fname${tab}$fullpath${tab}$dirname${tab}$fdate"
done <<<"$results")

/usr/local/bin/jq --slurp --raw-input '{ items: [ split("\n")[] | split("\t") | select(.[0] != null) | {
	title: .[0],
	arg: .[1],
	subtitle: .[2],
	mods: {
		shift: { valid: true, arg: .[1], subtitle: .[3] }
	},
	text: { copy: .[1], largetype: .[1] },
	icon: { type: "fileicon", path: .[1] }
}]}' <<<"$tsv"

 

Link to comment
4 hours ago, luckman212 said:

mdfind -onlyin does not accept multiple folders

 

It does. You just repeat the -onlyin option, i.e. mdfind -onlyin ~/Desktop -onlyin ~/Downloads $query

 

4 hours ago, luckman212 said:

nor a complex query using kMDItemPath

 

It accepts some pretty complex queries with judicious use of brackets.

Link to comment
2 hours ago, deanishe said:

It accepts some pretty complex queries with judicious use of brackets.

 

The page linked from this SuperUser post is gone now, but according to that (and my own tests) it doesn't accept kMDItemPath.

 

2 hours ago, deanishe said:

just repeat the -onlyin option, i.e. mdfind -onlyin ~/Desktop -onlyin ~/Downloads $query.

 

Nice! that will speed things up a bit for sure. How did you know that by the way? It doesn't seem to be documented anywhere.

Edited by luckman212
Link to comment
10 hours ago, luckman212 said:

it doesn't accept kMDItemPath

 

I didn't know that. Makes a kind of sense, I suppose.

 

10 hours ago, luckman212 said:

How did you know that by the way?

 

No idea. I've been using it for so long, I can't remember. I probably saw it somewhere or guessed based on the fact that the API accepts multiple paths.

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...