Jump to content

Dynamic file search with filter


Recommended Posts

Looks like Alfred 3 might finally support the feature I'm looking for, but I have no idea how to achieve it.

 

What exactly I'm trying to achieve:

 

I have a lot of local development sites ( more than in this specific example )

~/Sites/site1/subdir/themes/theme1
~/Sites/site2/subdir/themes/theme3
~/Sites/site3/subdir/themes/theme4
~/Sites/site4/subdir/themes/theme2
~/Sites/site5/subdir/themes/theme5
~/Sites/site6/subdir/themes/theme2
~/Sites/site7/subdir/themes/theme2

// And there are many, many more of these:
~/Sites/site7/subdir/themes/theme2/docs/theme2.html
~/Sites/site7/subdir/themes/theme2/docs/theme2/resources/style.css
~/Sites/site7/subdir/themes/theme2/docs/theme2/resources/app.js

I often want to jump directly to "~/Sites/site7/subdir/themes/theme2" or "~/Sites/site33/doodle-dir/themes/foobar".

All of my themes have 1 thing in common - they're the direct child of a folder named "themes/", which is why I thought I could filter for that through RegEx (see below).

Returning everything (or even only folders) named "theme2" isn't going to be much help to me.

 

 

 

Here are the steps I took to make my workflow:

 

1. th {name} - reveals all folders matching {name} in "~/Sites" directory

2. [hit return button] - Like the "Dynamic file search" example sets the results to a variable

3. [File Filter] - This should let through only paths that contain "something/something/themes/{name}" ), I've set it to match regex: "(?!themes\/)[\w\d-_]+$" for {path} variable 

4. Return a list of directories that have passed the file filter, so that I can select one of them an open in finder

 


At first I thought I can just copy the "Dynamic File Search" workflow and replace the Workflow JSON with Filter, but that doesn't work for me.

 

 

Any pointers how to properly use the filters ? Is this even possible ?

 

 

 

 

 

 

Link to comment

That doesn't exactly solve my problem. Have another look at the example above more closely.


If I enter "th theme2" in Alfred, it's going to allow me to pick from all of the folders called "theme2" in "~/Sites", which could be docs sub-folder. I need a way to tell Alfred only to return the results that end with "/themes/theme2"

Link to comment

Got you. I'm 99% sure what you want can't be done without any coding.

 

In theory, you'd need to filter on kMDItemPath in the File Filter's Advanced tab, but the Spotlight metadata engine (which Alfred uses) doesn't allow you to use that attribute for searching (or sorting).

The only existing workflow I know designed for this kind of thing is Fuzzy Folders, but I'm pretty sure you'd need at least a two word query every time with that.

You're probably going to have to write your own Script Filter.

 

The following two commands will both return a list of all directories under ~/Sites that are also children of a "themes" directory. It's up to you to extract the sections of the path you want to filter on.

# mdfind is the command-line version of Spotlight
mdfind -onlyin ~/Sites 'kMDItemContentType == "public.folder"' | grep '/themes/'

# Or using find 
find ~/Sites -type d -path '*/themes/*'
Edited by deanishe
Link to comment
  • 3 years later...

@deanishe This is probably a stupid question, but is it possible to feed the output from the mdfind command directly into the "scopes" field of a File Filter (using a JSON Config utility)? 

 

In short, I'd like to create a standard File Filter for searching within a set of folders with a specific tag ("Notes"). Because these folders fluctuate, however, I didn't want to just drag a set of static folder locations into the File Filter's Scope panel.

 

Instead, I was hoping to feed the "scopes" field of a JSON Config utility the output of the following search (mdfind 'tag:Notes AND kind:folder') - which would then set the scope for the File Filter. 

 

As always, thanks for your help!

Link to comment
4 hours ago, Jasondm007 said:

is it possible to feed the output from the mdfind command directly into the "scopes" field of a File Filter

 

Maybe. You can expand variables in a JSON Config, but to set the "scopes" field, which is an array while variables are only strings, I guess you'd have to set the variable to a JSON array.

 

Alternatively, you can echo the configuration JSON from a Run Script action immediately before your File Filter instead, with the appropriate "scopes" already set. That works in much the same way as a JSON Config, except there isn't any variable expansion, including {query}/(query). So if you want a literal {query} in the config, just write {query} (instead of (query) like you would in a JSON Config), and if you want your actual query inserted, do it manually instead of using {query}.

 

I hope that makes sense.

Link to comment
  • 10 months later...

I've managed to set up a workflow that dynamically changes the scope (based on environment variables), but I find the solution a bit clunky, so I wonder if there's a better solution. Now, I use a Keyword action, then a Run Script action to generate the configuration JSON, and then pass that to a File Filter. The output is what I expect.

 

One problem is that by using a Keyword action to invoke the workflow, there's an extra step at the start. I first have to type the keyword plus the query, press enter, before the search results are shown. But if I were to call the File Filter action directly, the results show up immediately. Is there a way to make remove that extra step?

 

I really appreciate any help you can provide. Thank you.

Link to comment

Hi @deanishe, thanks for responding! Here's the goal/desired behavior: I have multiple environment variables that are set to directory paths (e.g., path1=/users, path2=/users/desktop). I want to create a File Filter that restricts the search scope to path1 and path2 (i.e., restricts the scope dynamically based on environment variables). What's the best way to accomplish this?

 

My current solution involves 4 steps:

1. keyword input with query  (e.g., search xyz)

2. run script to dynamically generate the scopes

3. the output of step 2 is passed to a File Filter

4. process the selected file with another script

 

The problem with this 4-step solution is that since I'm using a keyword input with search query (step 1), the no matching results will be shown from the File Filter (step 3) until I press enter. In contrast, if I were to use create a workflow that includes only steps 3 and 4 (and I manually specify the scope in the File Filter), whenever I invoke the File Filter with a keyword and query (e.g., search xyz), matching results are shown immediately, without having to first press enter. So I'm wondering if I can make File Filter restrict the scope dynamically "behind the scenes" so I don't have to use a keyword input with query or another file filter (as with the Dynamic File Search example) to first restrict the scope before listing matching files?

 

Hope that's clear. Thanks!

 

 

Link to comment

You could make the query optional on the first Keyword, so you can go to the File Filter with just ↩. But you can't really jump straight to the File Filter if it needs a JSON Config. Control flow must pass through that first.

 

What's the purpose of setting the search scope via environment variables?

Link to comment
4 hours ago, deanishe said:

You could make the query optional on the first Keyword, so you can go to the File Filter with just ↩. But you can't really jump straight to the File Filter if it needs a JSON Config. Control flow must pass through that first.

 

What's the purpose of setting the search scope via environment variables?

 

Thanks for explaining! I think I'll either use your solution or use replace my current step 1 (keyword input) with a Hotkey (which can bypass the first ↩).

 

Here's why I'm trying to set File Filter's search scope via environment variables. I've been sharing my workflow with other people and the workflow is already designed such that it also happens that the search scopes required for File Filter have already been saved as environment variables. So my thought is, since I already have all the scope information in the environment variables, why not simplly use them to define File Filter's search scope dynamically? Since different users will specify different scopes, it seems the easiest way to make the workflow work for others (assuming the scopes have already been saved as environment variables) is to set the scope via environment variables. If not, users will have to manually edit their scopes in the File Filter.

 

I initially thought Script Filter addresses this problem, but quickly realized it's for a different purpose, but I really like how it works. Maybe a new feature like "Script File Filter" would be ideal for my situation...

 

Thanks very much again!

Link to comment
6 hours ago, ausius said:

I initially thought Script Filter addresses this problem

 

It can. It’s fairly straightforward to emulate a File Filter using mdfind.

 

6 hours ago, ausius said:

If not, users will have to manually edit their scopes in the File Filter.

 

Bear in mind that you can also configure the File Filter by programatically editing info.plist. Perhaps not ideal for this case because of the setup step, but it's there as a nuclear option.

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