Jump to content

Processing multiple files from a bash script


Recommended Posts

Hi - I'm a relative Alfred newbie, so apologies if this is obvious or answered somewhere that I missed!

I'm trying to run a bash script which outputs a number of file names, and then allow the user to act on those files, like they would on the results of a normal search - opening the file, revealing it in finder, etc.  (The script is running a custom `ripgrep` command - similar to `grep -l` - which just spits out matching filenames)

But I can't work out how to get the results of the script to be treated as multiple files - if I pass it to (say) the "File Buffer" action, which sounds like it might be what I want, it does nothing - and from looking at the debugger output, it seems like my bash script output is treated as one large string, instead of a number of separate lines.

 

Is this possible?  Would it help if the output was JSON not lines? (I could pipe it through `jq` if I needed JSON)

 

At the moment I have switched to running it in a terminal, which is good enough for now - but it feels like it'd be nice to be able to integrate this sort of thing into Alfred more completely.

Link to comment
5 hours ago, Korny said:

if I pass it to (say) the "File Buffer" action, which sounds like it might be what I want, it does nothing

 

It must be doing something, namely adding it to the File Buffer. Otherwise it must be outputting something to the debugger.


Separate the full paths to the files by tabs, so Alfred understands it’s several and connect it to an Action in Alfred Action, which seems to be what you want.

Link to comment
2 hours ago, vitor said:

Separate the full paths to the files by tabs

 

You should be able to use a JSON array since Alfred 4.1. It's not yet documented, as it was added kinda last minute and support might not be complete. The File Buffer action definitely supports it, as that's why support for multiple arguments was added IIRC.

Link to comment
  • 1 year later...

Dear all

 

I want to open multiple files selected in Finder in their online onedrive version. For one file, the workflow below works well (original source: . This is the code that I am using. How do I use the output of this in next "open url" block to do custom search using each file name? 

 

from os.path import basename
import re, sys

query = "{query}"

# replace strings seperated with <tab> for new lines
query = re.sub('\t', '\n', query)

# convert multiline string into list
query = query.splitlines()

# get the number of list contents
num = len(query)

# get basename of each list element and replace it
for i in range(0,num):
    query = basename(query)

# convert list into multiline string again
wynik = "\n".join(query)

# print without trailing new line
sys.stdout.write(wynik)

Screenshot 2022-07-14 at 8.39.29 PM.png

Link to comment

@pankajsz When asking for help, please share the Workflow, not just pasted code and screenshots. That forces people to decipher and recreate your Workflow mentally. To get help, strive to make less work for the helper. For example, you’re using {query} but should be using argv (though in this case it is probably OK), but it’s difficult to suggest what to change if I don’t know how your Workflow is set up.

Link to comment
On 7/19/2022 at 5:15 AM, vitor said:

@pankajsz When asking for help, please share the Workflow, not just pasted code and screenshots. That forces people to decipher and recreate your Workflow mentally. To get help, strive to make less work for the helper. For example, you’re using {query} but should be using argv (though in this case it is probably OK), but it’s difficult to suggest what to change if I don’t know how your Workflow is set up.

Yes @vitor. will do it. I uploaded Alfred workflow that works well on single file at https://we.tl/t-bPIYNlsNSY and need assistance to make it work for multiple files

 

I have also attached AppleScript that works on multiple files -- I can't figure out how to merge these things together.

Link to comment

This should work. You’ll need the latest Alfred pre-release. Take a look at the code in the Run Script, and see how much smaller it can be and remain readable. You’ll notice I changed it to with input as argv, meaning sys.argv[1] instead of {query}. That will save you having to escape things and is in general better (hence why it’s the default).

Edited by vitor
Link to comment
5 hours ago, vitor said:

This should work. You’ll need the latest Alfred pre-release. Take a look at the code in the Run Script, and see how much smaller it can be and remain readable. You’ll notice I changed it to with input as argv, meaning sys.argv[1] instead of {query}. That will save you having to escape things and is in general better (hence why it’s the default).

thanks vitor. the link above doesn't work for me. Request you to please upload via alternative way

Link to comment

This is cool, but in case someone wants a step that doesn't require installing python3 (which isn't preinstalled on any macOS...) this zsh script should also accomplish the same thing, unless I am missing something?

 

image.thumb.png.121aa6ae053dcf63281b411e3f9a9259.png

 

image.png.ea0cc44d44f69c316ca117409e5c1ae7.png

Link to comment
3 minutes ago, luckman212 said:

unless I am missing something?

 

You’re not, that’ll work too. But @pankajsz already had partial code which looks like they wrote it themselves, so I opted to fix it in their chosen language instead of rewriting as a shell script. Python is far from being my language of choice, but when you’re learning it’s better to correct the course than veer wildly. With the former you can try to understand what changed and add it to your arsenal; with the latter you’re left with the proverbial fish but not the fishing skills.

 

Not to say that I disagreed with sharing the Zsh code; I don’t in the slightest. Merely explaining my choice in this situation since you asked. In a case where the Python code had to be fifteen lines and the Zsh code three, I’d have eschewed the Python route.

 

Technically you don’t even need the Run Script and Split Args, you can replace both with the Get Path Basename Automation Task that went live with yesterday’s 2022.8 release. That I did mean to mention but forgot.

Link to comment
9 hours ago, luckman212 said:

This is cool, but in case someone wants a step that doesn't require installing python3 (which isn't preinstalled on any macOS...) this zsh script should also accomplish the same thing, unless I am missing something?

 

image.thumb.png.121aa6ae053dcf63281b411e3f9a9259.png

 

image.png.ea0cc44d44f69c316ca117409e5c1ae7.png

Tried this but it doesn't open the custom_url step after run script. 

Link to comment
9 hours ago, vitor said:

 

You’re not, that’ll work too. But @pankajsz already had partial code which looks like they wrote it themselves, so I opted to fix it in their chosen language instead of rewriting as a shell script. Python is far from being my language of choice, but when you’re learning it’s better to correct the course than veer wildly. With the former you can try to understand what changed and add it to your arsenal; with the latter you’re left with the proverbial fish but not the fishing skills.

 

Not to say that I disagreed with sharing the Zsh code; I don’t in the slightest. Merely explaining my choice in this situation since you asked. In a case where the Python code had to be fifteen lines and the Zsh code three, I’d have eschewed the Python route.

 

Technically you don’t even need the Run Script and Split Args, you can replace both with the Get Path Basename Automation Task that went live with yesterday’s 2022.8 release. That I did mean to mention but forgot.

If possible, please share the workflow using these new features from the latest release. will use it for learning and for future use.

Link to comment
7 hours ago, pankajsz said:

If I select and run on two files, it opens safari tabs three times.


As I said:

 

23 hours ago, vitor said:

You’ll need the latest Alfred pre-release.


In which that won’t happen.

 

7 hours ago, pankajsz said:

using these new features from the latest release.


It’s described above:

 

16 hours ago, vitor said:

you don’t even need the Run Script and Split Args, you can replace both with the Get Path Basename Automation Task


That’s just it. Delete two objects; put a new one in their place. The learning process involves reading instructions and trying to replicate them.

Link to comment
6 hours ago, vitor said:


As I said:

 


In which that won’t happen.

 


It’s described above:

 


That’s just it. Delete two objects; put a new one in their place. The learning process involves reading instructions and trying to replicate them.

Very simple and easy with Automation task. I used Get Path Basename without extension. thanks

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