[User command] Supply filenames twice in command-line

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

[User command] Supply filenames twice in command-line

Post by Sander Bouwhuis »

I want to call a command-line function by using a user command where I supply the item names twice.

For instance, assume the following 3 files are selected:
Z:\My Pictures\MyPic 1.jpg
Z:\My Pictures\MyPic 2.jpg
Z:\My Pictures\MyPic 3.jpg

This is what I want to do:
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "Z:\My Pictures\MyPic 1.jpg" -output "Z:\My Pictures\MyPic 1.jpg"
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "Z:\My Pictures\MyPic 2.jpg" -output "Z:\My Pictures\MyPic 2.jpg"
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "Z:\My Pictures\MyPic 3.jpg" -output "Z:\My Pictures\MyPic 3.jpg"

This is what I tried: (doesn't work):
Category : Open With
Action : Open selected List item(s) with specified application.
Caption : JpegTran
Application : "C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize <selitems> -output <selitems>
Mode : Multiple instance (pass each of the items to its own instance of the application).

Does anyone know how I can get this to work?
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [User command] Supply filenames twice in command-line

Post by TheQwerty »

<selitems> is going to send the selection to each instance of the executable.

When using openwith XY appends <items> to the command and that is replaced by the "space-separated list of the currently selected list items in quotes". Using the multiple instances mode will start and instance for each item and replace <items> with just that item.


The ideal solution would be to take your JpegTran UDC and replace both instances of <selitems> with <items>:

Code: Select all

"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize <items> -output <items>
Unfortunately, this doesn't work as XY will only replace the first instance of <items> and leaves the second untouched. (Maybe Don can change this?)

This means your only other option is to use a script:

Code: Select all

foreach ($item, Get('SelectedItemsPathNames','|'), '|', 'e', 'Nothing selected.') {
	run <<<CMD
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "$item" -output "$item"
CMD;
}
The above uses a HEREDOC to make it easier to understand the quoting (but forces the script to be multiple lines).

Note that you could technically continue to use openwith instead of run but XY will auto-append <items> if you don't include it so the easiest option there would be to do something like:

Code: Select all

foreach ($item, Get('SelectedItemsPathNames','|'), '|', 'e', 'Nothing selected.') {
	openwith <<<CMD
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "$item" -output <items>
CMD
	, 's', $item;
}
But that looks a bit ugly and is a bit confusing since <items> auto-quotes but our $item does not.

Run does offer some advantages in that you can ensure XY waits for the current instance to finish before proceeding to the next, and you can change how the launched application's window appears.

Hope that helps!

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: [User command] Supply filenames twice in command-line

Post by Sander Bouwhuis »

Thanks for your great help!

I tried it, but I get this error:
The system cannot find the file specified.

File: "C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "Z:\Test.jpg" -output "Z:\Test.jpg"

Parameters:
run
"C:\Utilities\JpegTran\JpegTran.exe" -copy none -optimize "Z:\Test.jpg" -output "Z:\Test.jpg"
I don't know why the JpegTran.exe cannot be found, as it seems correct:

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Utilities\JpegTran>JpegTran.exe
JpegTran.exe: must name one input and one output file
usage: JpegTran.exe [switches] inputfile outputfile
Switches (names may be abbreviated):
  -copy none     Copy no extra markers from source file
  -copy comments Copy only comment markers (default)
  -copy all      Copy all extra markers
  -optimize      Optimize Huffman table (smaller file, but slow compression)
  -progressive   Create progressive JPEG file
Switches for modifying the image:
  -crop WxH+X+Y  Crop to a rectangular subarea
  -flip [horizontal|vertical]  Mirror image (left-right or top-bottom)
  -grayscale     Reduce to grayscale (omit color data)
  -perfect       Fail if there is non-transformable edge blocks
  -rotate [90|180|270]         Rotate image (degrees clockwise)
  -scale M/N     Scale output image by fraction M/N, eg, 1/8
  -transpose     Transpose image
  -transverse    Transverse transpose image
  -trim          Drop non-transformable edge blocks
  -wipe WxH+X+Y  Wipe (gray out) a rectangular subarea
Switches for advanced users:
  -arithmetic    Use arithmetic coding
  -restart N     Set restart interval in rows, or in blocks with B
  -maxmemory N   Maximum memory to use (in kbytes)
  -outfile name  Specify name for output file
  -verbose  or  -debug   Emit debug output
Switches for wizards:
  -scans file    Create multi-scan JPEG per script file

C:\Utilities\JpegTran>
I put your script in User/Manage Commands.../Run Script. Did I do something wrong?
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [User command] Supply filenames twice in command-line

Post by TheQwerty »

When the "XYplorer - Scripting" dialog pops up showing the error can you please take a screenshot of it?

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: [User command] Supply filenames twice in command-line

Post by Sander Bouwhuis »

Settings.png
Script Error.png
To see the attached files, you need to log into the forum.
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [User command] Supply filenames twice in command-line

Post by TheQwerty »

Ah-ha - white space strikes again!

Remove the indentation before every line except 'run <<<CMD'.

It should look like:
2015-11-16 160446.png
To see the attached files, you need to log into the forum.

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: [User command] Supply filenames twice in command-line

Post by Sander Bouwhuis »

Now it works! Many many thanks!

I don't understand how whitespace could make this fail, but it works so I'm happy.
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

Post Reply