Jump to content

Search within folder currently selected in Finder


Recommended Posts

Hello,

 

I have been attempting for an hour to tweak the template workflow "File System Navigation for specified path" by combining it with "Dynamic File Search" (which works like a charm) in order to be able to type a shortcut while selecting a folder in Finder and find inside it not only its folders, but the files and whatever elements they themselves contain, without having to navigate down the tree... I'm afraid that's not very clear, here's an example : I select folder "iconpacks" (= it's highlighted in blue) in Finder, and instead of being shown its folders, I'd like to be able to search _directly_ for its folders and files – like in a regular "File Search" query, the only difference being that the scope should be the folder I selected in the first place, not my whole computer. Here is my poor attempt (more or less my first custom workflow, show some indulgence...) : https://we.tl/t-KvnccQWYAk. Attached is the result of "File System Navigation for specified path" : as you can see, the files contained by touch_portal_flat_white_iconpack are not shown.

 

Just to make sure I'm making myself understood : the shortcut should trigger a search session or search bar (confused terminology sorry) akin to that triggered by an elementary "File Search", with the only candidates being the result of glob.glob(path_of_folder, recursive=True) in Python.

 

Alfred 4.3.2, High Sierra 10.13

 

Thanks for your time and help!

Capture d’écran 2021-03-07 à 09.07.40.png

Edited by yet_another_alred_user
Typos
Link to comment

So basically, you want the Dynamic File Search, but you want to pass in the folder to search in via Hotkey?

 

The reason your workflow isn’t searching in the right place is because a Hotkey doesn’t work quite the same way as a File Filter (in the original Dynamic File Search). A File Filter only emits one path, so you use "scopes": ["{query}"] in the JSON Config to put it into the scopes array.


Current selection in macOS from a Hotkey, on the other hand, is already an array (because it may be multiple files), so you need to change your JSON Config to "scopes": "{query}"

 

Link to comment
  • 9 months later...

@deanishe If you're feeding more than one file path into the "scopes" field, and they're coming into the JSON Config as a string, how do you convert the string back into a list, so that the File Filter knows how to read them (i.e., adding back the missing quotes around each file path in the input)?

 

To explain, let's say the JSON Config receives the following string:

  • /Users/Name/Downloads, /Users/Name/Desktop

If you set the scopes to "{query}", as in the standard way, the attached file filter won't work because the JSON isn't formatted correctly. Each file path needs its quotes, as follows:

  • "/Users/Name/Downloads", "/Users/Name/Desktop"

Is there something that can be added to the JSON Config's incoming query that would add the missing quotes, so that it can work properly? For example, is there something I can add above the usual "{"alfredworkflow" : { ..." that will convert the string into an appropriately formatted/quoted list of file paths that I can use as the input/variable for "scopes"?

 

Thanks for your help! This issue has been driving me crazy (short trip, I know - Ha).

Edited by Jasondm007
Added Color
Link to comment
16 hours ago, Jasondm007 said:

To explain, let's say the JSON Config receives the following string:

  • /Users/Name/Downloads, /Users/Name/Desktop

 

Why does it receive that? You can't manipulate the formatting in the JSON Config itself. You need to make sure the input is valid to begin with.

 

Link to comment
13 hours ago, Jasondm007 said:

Python Run Script, which could convert it into an array that could be fed into the JSON Config?

 

You don’t need the JSON Config: all it does is emit a JSON-encoded alfredworkflow object. You can emit the same JSON directly from your Run Script.

 

import json
import sys

scopes = ['~', '/some/file/path']
config = {
    'alfredworkflow' : {
        'config' : {
            'argumenttrimmode' : 0,
            'keyword' : 'keyword',
            'daterange' : 0,
            'scopes' : scopes,
            # ...
            # ...
        }
    }
}
json.dump(config, sys.stdout, indent=2)

 

Edited by deanishe
Link to comment
  • 2 months later...

Hi @deanishe - By chance, do you know if it's possible to use a python script (similar to above) to feed more than one file to an "Open With" file action? I'm not sure if it's just a formatting problem, or if it's not possible yet with these newer file action options, but I haven't had any luck trying to update the the previous script.

 

For example, I've tried several variations of the following python script below (where the incoming files are a tab separated string and this script feeds to an "Action in Alfred" with the "Open With" "Jump to" option selected):

 

import json
import sys

query = "{query}"
theFiles = query.split("\t")

config = {
	'alfredworkflow' : {
		'config' : {
			'path' : theFiles,
			'type' : 1,
			'jumpto' : 'alfred.action.openwith'
		}
	}
}
json.dump(config, sys.stdout, indent=2)

 

As always, thanks for any help you can lend!

Link to comment
On 2/22/2022 at 2:07 AM, Jasondm007 said:

By chance, do you know if it's possible to use a python script (similar to above) to feed more than one file to an "Open With" file action?

 

No idea, tbh. If you already have a script, you can open the files with /usr/bin/open -a ApplicationName /path/to/file /path/to/other/file instead.

Link to comment

Hi @deanishe - Thanks for getting back to me! I hope all is well on your end.

 

Yeah, I was really hoping to feed the files to the "open with" file action, so I could select the appropriate app (as opposed to just opening it in the default or specifying the app myself, as you would need to do in the code above). I just like Alfred's UX for selecting the appropriate app more. 

 

It's easy to feed files to Alfred's "open with" file action, when done one at a time. I just can't figure out how to send more than one via script. Obviously, Alfred's able to do it, given that it's already accomplished when files are directly selected and triggered via hotkey. Do you know the format that Alfred uses when handing macOS selections via hotkey? I should be able to use a python script to format the file paths accordingly, right?

 

Thanks again for all of your help! Cheers!

Link to comment
On 3/1/2022 at 4:59 PM, Jasondm007 said:

It's easy to feed files to Alfred's "open with" file action, when done one at a time. I just can't figure out how to send more than one via script. Obviously, Alfred's able to do it, given that it's already accomplished when files are directly selected and triggered via hotkey.

 

Alfred is passing through the arguments, not path. The latter is like if you had set the value manually in the Action in Alfred, which is not what you’re looking for. Speaking of which, why not configure the object directly via the GUI instead of overwriting it via JSON? Action in Alfred is simple enough that if you’re feeling the need to override its configuration, you’re probably hiding extra complexity somewhere else. If you truly need different Action in Alfred configurations in the same Workflow, consider if multiple objects won’t be a simpler solution.


Anyway, to pass multiple arguments send them via an array in arg:

 

config = {
  "alfredworkflow" : {
    "arg": sys.argv[1:]
  }
}

 

The above assumes with input as argv, which you should use instead of with input as {query}. It uses from argument 1 to the end because in python argv[0] is the script itself.

Edited by vitor
Link to comment

HI @vitor - Thanks a ton for the detailed explanation. It was incredibly helpful, and I think I understand what you mean by arguments versus file paths.

 

I'm happy to report that since you and @deanishe last recommended that I start learning python—and stop relying on AppleScript so much—that I have been slowly picking up things here and there. As you can tell from the above, however, I often have a hard time applying things outside of those tutorials, such as in Alfred.

 

In the above scenario, let's assume the file paths are coming in to the python script as a tab-separate string (e.g., "/Users/theName/Desktop/1.pdf /Users/theName/Desktop/2.pdf"). If I want to format that string appropriately, and then feed it directly to an Alfred action (whose Treatment = "File" and Jump to = "Open with..."), are you saying that the following will work:

 

import json
import sys

theInput = "{argv}"
theFiles = theInput.split("\t")

config = {
    "alfredworkflow" : {
        "arg": sys.theFiles[1:]
    }
}
json.dump(config, sys.stdout, indent=2)

 

In other words, I don't need to include any of the action's config-related information (e.g., path, type, jumpto)? 

 

Sorry for being such a luddite here. And, thanks again for all of your help!

Link to comment
3 minutes ago, Jasondm007 said:

are you saying that the following will work

 

Close; you have a few mistakes.

  • theInput = "{argv}" should be theInput = "{query}".
  • sys.theFiles[1:] should just be theFiles[1:].

sys.argv is an array that contains the arguments passed to a script; if you already have another array you call it directly, no sys..

 

Also, you can get rid of theInput and do theFiles = "{query}".split("\t") directly, no need for the extra variable.

 

12 minutes ago, Jasondm007 said:

In other words, I don't need to include any of the action's config-related information (e.g., path, type, jumpto)? 

 

Correct. Set the object’s configuration in the object itself via the GUI. It’s nice that the option exists to set and override the configuration via JSON, but that’s an advanced option most people will never need to touch.

 

16 minutes ago, Jasondm007 said:

Sorry for being such a luddite here.

 

Wouldn’t that imply you were against technology? That’s the opposite of how I see you, you’re trying to learn!

 

29 minutes ago, Jasondm007 said:

let's assume the file paths are coming in to the python script as a tab-separate string

 

But it bears repeating: you should be using with input as argv, not with input as {query}, and be passing your files as proper arguments, not tab separated. To avoid an XY problem, be sure to always include your goal as part of the question, above the method you think will get you there.

Link to comment

Hi @vitor - Thanks again for the detailed feedback above! This is really helpful, and I think I'm on the same page with everything above.

 

However, I still can't seem to get the file action to open both files at the same time. Instead, the Open With... file action always just opens one of the files it's passed. I must be missing something still?

 

For testing purposes, I tried to simply place the string of file paths in the script, but I still run into the same problem (and tinkering with json.dump, etc.): similar to the following, but with changes to theName, of course

 

import json
import sys

# theInput = "{query}"
theInput = "/Users/theName/Desktop/1.pdf	/Users/theName/Desktop/2.pdf"

theFiles = theInput.split("\t")
config = {
    "alfredworkflow" : {
        "arg": theFiles[1:]
    }
}
json.dump(config, sys.stdout, indent=2)

 

Is there something special that needs to be declared in the config so that Alfred's file action knows to try and open both of them at the same time (with the selected Jump to)?

 

Thanks again for all of the tips above. And, I understand what you mean about the XY problem, simplifying the variables, etc. I was hoping to just get down a basic working example, so that I could go from there. Thanks!

Link to comment

Ah, forgot to correct something above. theFiles[1:] should be theFiles, because you no longer want to ignore any element (with sys.argv, you’re ignoring the script itself from the array). Fix that and you should be good to go.

Link to comment
  • 4 months later...
On 3/3/2022 at 8:10 AM, Jasondm007 said:

As always, @vitor , your suggestion worked perfectly! Thanks again for the helpful explanations today, too. I think I'm finally starting to get my head around some aspects of python and Alfred! Much appreciated!!

@Jasondm007 request for final version of this work as feasible

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