How to quote arguments passed to script?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
zBernie
Posts: 181
Joined: 08 Dec 2016 17:20

How to quote arguments passed to script?

Post by zBernie »

I have a powershell script named IM-Combine-Images-Vertically.ps1 which makes a call to the ImageMagick convert command. The script works from the powershell command line when I enclose each of the file names in quotes, due to the space in the path name. The file names must also be separated by a comma. This works:

PS C:\Users\bernie\PowerShell> ./IM-Combine-Images-Vertically.ps1 "C:\Users\bernie\Photo Staging\P1050492.JPG", "C:\Users\bernie\Photo Staging\P1050491.JPG"

This script does not work from my XYplorer custom button. How can I pass the arguments from XY similar to the above command line version?

Code: Select all

    $var_file = "<selitems ,>";
    runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\bernie\PowerShell\IM-Combine-Images-Vertically.ps1 $var_file");

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to quote arguments passed to script?

Post by highend »

Code: Select all

$var_file = quote(<get SelectedItemsPathNames '", "'>);
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 181
Joined: 08 Dec 2016 17:20

Re: How to quote arguments passed to script?

Post by zBernie »

highend wrote:

Code: Select all

$var_file = quote(<get SelectedItemsPathNames '", "'>);
That was close, but did not work. Placing a ::text in front of $var_file shows these values, which appear to be missing a couple of quotes:

$var_file = quote(C:\Users\bernie\Photo Staging\20170128_101317.jpg", "C:\Users\bernie\Photo Staging\20170128_101258.jpg)


Here's what works from the command line:
PS C:\Users\bernie\PowerShell> ./IM-Combine-Images-Vertically.ps1 "C:\Users\bernie\Photo Staging\P1050492.JPG", "C:\User
s\bernie\Photo Staging\P1050491.JPG"

-Thanks

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to quote arguments passed to script?

Post by highend »

There are no missing quotes. What you posted is only a not fully-processed script line.
quote(...) adds the surrounding quotes.

Why it still doesn't work?
Maybe the outer quotation marks are swallowed during the runret call
Anyway, show the content of the powershell script
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 181
Joined: 08 Dec 2016 17:20

Re: How to quote arguments passed to script?

Post by zBernie »

highend wrote:There are no missing quotes. What you posted is only a not fully-processed script line.
quote(...) adds the surrounding quotes.

Why it still doesn't work?
Maybe the outer quotation marks are swallowed during the runret call
Anyway, show the content of the powershell script

This is the entire script IM-Combine-Images-Vertically.ps1:

param(
[ string[]]$fnames
)

convert $fnames -append merged-vertical.jpg

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to quote arguments passed to script?

Post by highend »

Your powershell script doesn't do more that throwing it's input params onto convert from the ImageMagick package

1. You can do exactly the same with XY rendering this "intermediate scripting layer" unnecessary
2. Quoting powerscript script calls with additional quoted params is... at least tricky (I'd say: complex)
So my advice is: 1.
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 181
Joined: 08 Dec 2016 17:20

Re: How to quote arguments passed to script?

Post by zBernie »

highend wrote:Your powershell script doesn't do more that throwing it's input params onto convert from the ImageMagick package

1. You can do exactly the same with XY rendering this "intermediate scripting layer" unnecessary
2. Quoting powerscript script calls with additional quoted params is... at least tricky (I'd say: complex)
So my advice is: 1.
This quoting is driving me nuts! I'm trying to run the convert command without the powershell script. I've tried various permutations of quoting like that shown below, and still cannot get the convert command to run correctly. I read that the run command will interpret a hyphen as a math operator so it needs to be quotes. But still not getting the quoting correct.

Code: Select all

$var_file = quote(<get SelectedItemsPathNames '", "'>);
     run '"convert" $var_file "-append merged-vertical.jpg"');

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to quote arguments passed to script?

Post by highend »

01. Use code tags when you post code, please
02. There are probably various issues with what you posted
a.) Typing error on your run... line (the closing parenthesis)
b.) Enclosing the command into single quotation marks is a good idea
but will fail miserably because variables inside it will NOT be resolved any more
c.) You don't separate multiple images via "," when called from a command line
d.) After fixing that error there is a chance that you're using a 64-bit OS?
In that case you may suffer from file system redirection
E.g.: Execute this

Code: Select all

    $result = runret("convert");
    text $result;
Do you get something like
A filesystem must be given
instead of the long output of the imagemagick convert utility?

If yes, call it with an explicit path!
Showing both ways (runret/run):

Code: Select all

    $var_file = <get SelectedItemsPathNames '" "'>;
    $result = runret("""C:\Program Files\ImageMagick-6.7.5-Q16\convert.exe"" ""$var_file"" -append ""<curpath>\merged-vertical.jpg""");
    // run """C:\Program Files\ImageMagick-6.7.5-Q16\convert.exe"" ""$var_file"" -append ""<curpath>\merged-vertical.jpg""";
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 181
Joined: 08 Dec 2016 17:20

Re: How to quote arguments passed to script?

Post by zBernie »

Thank you! I had to use the path to convert.exe. I don't think I would have ever gotten that triple double quote syntax on my own!

Post Reply