Jump to content
luckman212

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

Share this post


Link to post
12 hours ago, luckman212 said:

Is there a better way to do this?

 

Not with a File Filter: they specifically don't show anything until you enter a query.

 

You could use mdfind to get the image files and display them in a Script Filter.

Share this post


Link to post

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"

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...