Jump to content

Working with multiple selected items


Recommended Posts

in="{query}"

blah='	' read -a array <<< "$in"

echo "${array[0]}" > output.txt
echo "${array[1]}" >> output.txt

 

Multiple files are tab separated. Code above reads the {query} input, splits on a tab and stores it into an array called "array". Items can then be individually accessed or you can run them through a loop. Looping through would look like this:

for fn in "${array[@]}"
do
	echo "$fn" >> output.txt
done
Link to comment
  • 2 months later...

Hi, thanks Alfred team for the great tool.

 

How to handle multiple files, when some file names includes spaces?

 

this code

 

blah='    ' read -a array <<< "$in"

 

 

splits file name with spaces into several array elements.

 

Regards

Link to comment
in="{query}"

blah='	' read -a array <<< "$in"

echo "${array[0]}" > output.txt
echo "${array[1]}" >> output.txt
 

Multiple files are tab separated. Code above reads the {query} input, splits on a tab and stores it into an array called "array". Items can then be individually accessed or you can run them through a loop. Looping through would look like this:

for fn in "${array[@]}"
do
	echo "$fn" >> output.txt
done

Hi, thanks Alfred team for the great tool.

 

How to handle multiple files, when some file names includes spaces?

 

this code

 

blah='    ' read -a array <<< "$in"

 

 

splits file name with spaces into several array elements.

 

Regards

 

 

The code for separating the files’ names can be greatly simplified (no need for the array), by doing simply

echo "{query}" | perl -pe 's/\t/\n/g' > file_list.txt

That’ll take the tabs and convert them to newlines, achieving the same with less code, making it more easily reusable and maintainable.

Edited by Vítor
Link to comment

Hi, thanks Alfred team for the great tool.

 

How to handle multiple files, when some file names includes spaces?

 

this code

 

blah='    ' read -a array <<< "$in"

 

 

splits file name with spaces into several array elements.

 

Regards

 

I am doing it this way:

 

FILES="{query}"

OLDIFS="$IFS"
IFS='<tab>'

for f in ${FILES[*]}; do
    # do whatever you want with $f
done

IFS="$OLDIFS"

Using Vítor's solution requires you to create a temporary file. If the data is not needed anymore after processing it, I would omit the file system operation and use the array.

Link to comment

Thanks for answers,  but in my case it turned out, that simple {query} using was quite enough.

 

Is there any difference between:

 

VAR={query}

and 

VAR="{query}"

?

 

You should use VAR="{query}" to avoid problems with strings containing spaces.

Link to comment

Using Vítor's solution requires you to create a temporary file. If the data is not needed anymore after processing it, I would omit the file system operation and use the array.

Ah, yes, that is quite correct. I was taking David’s example of saving the filenames to a .txt file and simplifying it, but I agree, if they’re only needed for a one-off operation, you solution is better if the action should be performed for each file individually. If you need it to be applied to a bunch of files as whole, then using perl and xargs is still an acceptable solution. Something like

echo "{query}" | perl -pe 's/[\t\n]/\0/g' | xargs -0 <your_script>
Edited by Vítor
Link to comment

Ah, yes, that is quite correct. I was taking David’s example of saving the filenames to a .txt file and simplifying it, but I agree, if they’re only needed for a one-off operation, you solution is better if the action should be performed for each file individually. If you need it to be applied to a bunch of files as whole, then using perl and xargs is still an acceptable solution. Something like

echo "{query}" | perl -pe 's/[\t\n]/\0/g' | xargs -0 <your_script>

 

xargs is doing the same as the for loop. It reads the filenames from stdin, splits them at the null character and executes <your_script> for every single filename.

Link to comment

xargs is doing the same as the for loop. It reads the filenames from stdin, splits them at the null character and executes <your_script> for every single filename.

It is not, I tested it before posting that. If you used something like xargs -I {} <script> {}, then it would do it one by one, and by doing the similar xargs -J {} <script> {}, it would act on them in one go. For a short list of files, its behaviour (at least on OSX) seems to be similar to the -J option, when none is specified.

I tested it by using the CLI trash command on several files at once — the Finder acts differently when sending multiple files at once to the trash, or doing it one by one (in this case, you hear the trash sound multiple times, and you need to to ⌘+Z once for each file, to get them back).

Link to comment

It is not, I tested it before posting that. If you used something like xargs -I {} <script> {}, then it would do it one by one, and by doing the similar xargs -J {} <script> {}, it would act on them in one go. For a short list of files, its behaviour (at least on OSX) seems to be similar to the -J option, when none is specified.

I tested it by using the CLI trash command on several files at once — the Finder acts differently when sending multiple files at once to the trash, or doing it one by one (in this case, you hear the trash sound multiple times, and you need to to ⌘+Z once for each file, to get them back).

 

The man page is quite strange on these two switches...

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