Xfce Forum

Sub domains
 

You are not logged in.

#1 2021-07-19 00:36:09

Dini
Member
Registered: 2021-07-19
Posts: 15

'Custom Action' variables into bash

Hi Xfce team,

I'm loving XFCE, just trying to customize some stuff in the Thunar program.

How do I send those variables to a bash script? I feel like I've tried everything. Still learning though.

I've tried...

f=%f F=%F n=%n N=%N d=%d D=%D /path/to/script.sh

...but it just doesn't work. It only works if I select one file. If I select more the script doesn't even launch.

Thanks team!

Offline

#2 2021-07-19 00:58:42

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,948

Re: 'Custom Action' variables into bash

Hello and welcome.

You would send it as a parameter to the script. So for example:

/path/to/script.sh %f

...and then within in your script, you would process it as $1.

Example
Custom action command:

/path/to/script %f

...and the script (requires the zenity package):

#!/bin/bash

zenity --info --text="$1"

...note that %f is being processed as $1.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2021-07-19 22:56:02

Dini
Member
Registered: 2021-07-19
Posts: 15

Re: 'Custom Action' variables into bash

Thanks for responding ToZ,

1) Okay, so I wish your suggestion did the trick, but unfortunately it still seems to falter when I give it an %F array made of more than one file. If I select multiple files then run the 'Custom Action', the '%F' expands into many separate strings. So this means iterating over parameters.

Eg. If I send all six variables (%f %F %n %N %d %D) to the bash script I'd need to do some math on the number of parameters to get the number of files to iterate over. So 903 parameters means that I supplied 300 files (ie. - 3 then divide by 3) and so to get the correct parameter for the %n variable, I'd need to offset the index by 1x300+1 to get it. Its a bit overly fancy but I can maybe do it (if there's no limit on the number of parameters that can be supplied. I'm just wondering if there's a better way.

2) Maybe the best option is to encase array variables like %N in (") quotes and then try to work backwards to repair any damage that is done to that array later (inside the script). So...

/path/to/script.sh "%N"

...turns the two folders Peanut's Jelly and Scones with Jam into...

'Peanut'\''s Jelly' 'Scones with Jam'

I think there's an way to repair the damage done here, yes?? It's getting late now and my brain is actually turning to jelly. lol

Would be great to get input on the matter again.

3) Maybe just ignore all the %F and $D variables and assume that I can only manipulate files in one folder at a time? Is this wise/correct? Then I'd just use the 'pwd' command to determine the enclosing folder, and iterate over 300 or so parameters/files (again, not sure if Bash allows this many params).

4) I'm assuming there's no way to do "Pass by Reference" for these (%X) variables, because it looks like Bash knows nothing about them and it's a special Thunar thing. Am-i-rite?

Sorry for the long reply smile

Last edited by Dini (2021-07-19 23:00:37)

Offline

#4 2021-07-19 23:11:23

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,948

Re: 'Custom Action' variables into bash

Dini wrote:

1) Okay, so I wish your suggestion did the trick, but unfortunately it still seems to falter when I give it an %F array made of more than one file. If I select multiple files then run the 'Custom Action', the '%F' expands into many separate strings. So this means iterating over parameters.

Eg. If I send all six variables (%f %F %n %N %d %D) to the bash script I'd need to do some math on the number of parameters to get the number of files to iterate over. So 903 parameters means that I supplied 300 files (ie. - 3 then divide by 3) and so to get the correct parameter for the %n variable, I'd need to offset the index by 1x300+1 to get it. Its a bit overly fancy but I can maybe do it (if there's no limit on the number of parameters that can be supplied. I'm just wondering if there's a better way.

You are correct. Consider something like the following in the script (that processes each parameter separately):

for x in "$@"
do
   ...do something with "$x" #each parameter is processed in succession
done

...and call it via ./script.sh "Peanut's Jelly" "Scones with Jam".

Or save it to an array:

params=( "$@" ) #save parameters into array
echo ${params[0]} #item at position 1
echo ${params[1]} #item at position 2
echo ${#params[@]} #array size

2) Maybe the best option is to encase array variables like %N in (") quotes and then try to work backwards to repair any damage that is done to that array later (inside the script).

I believe they are passed in quotes to the script. See examples above.

3) Maybe just ignore all the %F and $D variables and assume that I can only manipulate files in one folder at a time? Is this wise/correct? Then I'd just use the 'pwd' command to determine the enclosing folder, and iterate over 300 or so parameters/files (again, not sure if I can do this many params).

With a proper script, you should be able to iterate over any number of passed parameters (though I believe there is a limit in the size of the paramters - not sure what it is off the top of my head.

4) I'm assuming there's no way to do "Pass by Reference" for these (%X) variables, because it looks like Bash knows nothing about them and it's a special Thunar thing. Am-i-rite?

Sorry, not sure I understand. Can you provide an example?


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#5 2021-07-20 00:22:39

Skaperen
Member
From: right by Jesus, our Saviour
Registered: 2013-06-15
Posts: 812

Re: 'Custom Action' variables into bash

i have used parameters over 2048 characters in bash scripts.  i have also done more than 10000 short parameters.  i have done a total exceeding 80000.  bash slows down working with these.  often, another language like Perl or Python (even just for part) can help and do stuff easier and faster.

Offline

#6 2021-07-20 00:39:20

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,948

Re: 'Custom Action' variables into bash

Yup. You are correct.

$ getconf ARG_MAX
2097152

On my system, it can handle over two hundred thousand characters.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#7 2021-07-29 11:08:05

Dini
Member
Registered: 2021-07-19
Posts: 15

Re: 'Custom Action' variables into bash

Big thanks to both of you,

Quick update, I needed to pass the parameters other scripts in the bash script.

I'm calling the script with (no quotes):

/path/to/script.sh %N

Then, in the script when I needed to spit out the filenames I did

command "$@"

which was similar to your example with assigning the values to a "params" array, Toz.

And when I needed to loop through them in a loop I did

for file_or_folder_name in "$@"; do

  # if a (d)irectory, append forward-slash.
  if [ -d "$file_or_folder_name" ]; then
    file_or_folder_name="${file_or_folder_name}/"
  fi

  # Write name to file.
  echo "$file_or_folder_name" >> "testing-file.txt";

  # Write full path to file
  echo "$(pwd)/$file_or_folder_name" >> "testing-full-path-to-file.txt";
done

WARNING: I'm kinda assuming that all the files passed to the script are in the same folder that triggered the script (as you can see I just used `pwd' when I needed to spit out the "present working directory"). It seems to work fine for Custom Actions.

I remember reading once that there was a way to assign a bunch of arguments to another argument/parameter in Bash (so you'd call the script above like "-N %N -n %n") but alas it seems that wasn't necessary.

Woohoo! It's all just Bash stuff in the end really, but could still help someone.

Offline

#8 2021-07-30 01:26:35

Skaperen
Member
From: right by Jesus, our Saviour
Registered: 2013-06-15
Posts: 812

Re: 'Custom Action' variables into bash

bash has ways to figure out just about everything you get.  sometimes there is some confusion with how an interactive bash parses input to make commands and a script-interpreter bash handles the arguments it gets when the script is run as a command.  i have 3 decades experience with bash and C, and nearly a decade with python (no C++ or perl).  i also check for questions i can answer on https://www.linuxquestions.org/ under the same name as here.  lots of other bash experts there, too.  and just about everything can be found online with your favorite major search engine.

Offline

Board footer

Powered by FluxBB