Jump to content

Limit File Filter results to only show folders in the root of Search Scope folder(s)


Recommended Posts

I've created a workflow with a File Filter, and set it to only show folder results: 

 

1389847355_ScreenShot2020-02-20at4_25_15PM.thumb.png.d20a5b64e707819da3257d516a8b84e0.png

 

Then in the Scope tab I've added one folder: 

 

499069007_ScreenShot2020-02-20at5_09_27PM.thumb.png.c7aa0d131e29868b1e12df7d0634d905.png

 

This works, but it produces results from all subfolders of the specified folder. 

 

Is there a way to restrict it to only show results from the root folder provided (and not subfolders of that folder)?
 

I found this question from 2015 asking a similar question about the script filter, but there were no good viable solutions. 

Link to comment

No, it's not possible with a File Filter. They are fundamentally designed to search the whole file tree, as is the Mac metadata index they rely on.

 

You have two options:

  1. Tell Alfred to browse the folder with the AppleScript tell application id "com.runningwithcrayons.Alfred" to browse "~/Documents/". This will only the contents of that directory (no subdirectories), but it will show files as well as folders, however. . This will only the contents of that directory (no subdirectories), but it will show files as well as folders, however.

  2. Write a workflow with a Script Filter that only shows folders (e.g. using find /path/to/directory -type d -depth 1)

 

Link to comment

@deanishe Thank you! I definitely wanted folders only so I went with the second suggestion. I replaced the File Filter with a Script Filter and this script: 

 

find ~/projects ~/internal -maxdepth 1 -name "*$1*" -type d | \
while read line; \
do /usr/local/bin/jq -n \
--arg uid "$(echo \"$line\" | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')" \
--arg title "$(basename "$line")" \
--arg subtitle "${line/\/Users\/username/~}" \
'{uid: $uid, title: $title, subtitle: $subtitle}'; \
done | \
/usr/local/bin/jq -n '.items |= [inputs]'

It uses jq to build the JSON output that Alfred requires. `jq` needs to be referenced by the full path of the binary, I'm assuming because Alfred does not have access to, or knowledge of, the shell's $PATH variable.

 

One more thing I'd like for this workflow... I want to have each result row show a folder icon. As it is now it just shows the workflow's icon: 

 

915073738_ScreenShot2020-02-26at5_38_23PM.png.a141b9ef7d689d673bb124199693bb0a.png

 

I read the icon and type sections of the documentation but it's not making sense to me. I tried `"type": "folder"` and also `"icon": { "type": "fileicon" }` but neither seems to have any effect.

 

It appears I could have an icon file loaded from a file but since my results are always going to be folders, is there an fast/native way to just display a folder icon (other than replacing the workflow's icon with a folder icon, since I want that to be different)?

Link to comment
9 minutes ago, alfred_user said:

I'm assuming because Alfred does not have access to, or knowledge of, the shell's $PATH variable.

 

That's right. On macOS, applications aren't launched from your shell (unless you explicitly run one from a shell), so they don't have your shell's environment.

 

9 minutes ago, alfred_user said:

I read the icon and type sections of the documentation but it's not making sense to me.

 

You must specify a value for path. type is optional; path isn't.

 

9 minutes ago, alfred_user said:

is there an fast/native way to just display a folder icon

 

"icon": {
    "type": "filetype",
    "path": "public.folder"
}
Edited by deanishe
Link to comment

Adding that icon key worked, thanks!

 

I also realized I need the arg key in the object in order to pass the selected path on to the output. Completed script here: 

 

find ~/projects ~/internal -maxdepth 1 -name "*$1*" -type d | \
while read line; \
do /usr/local/bin/jq -n \
--arg uid "$(echo \"$line\" | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')" \
--arg title "$(basename "$line")" \
--arg subtitle "${line/\/Users\/username/~}" \
--arg arg "$line" \
'{uid: $uid, title: $title, subtitle: $subtitle, arg: $arg, icon: { type: "filetype", path: "public.folder" } }'; \
done | \
/usr/local/bin/jq -n '.items |= [inputs]'

 

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