Use selected files as parameters to execute Command?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
tjsuominen
Junior Member
Junior Member
Posts: 26
Joined: 2009-01-07, 12:31 UTC

Use selected files as parameters to execute Command?

Post by *tjsuominen »

Hi,

My use case is as follows:
I would like to have command (in the toolbar) that takes selected files as input (as %1 and %2) and then execute the defined command.

The command utility is a command-line based pdftk that I use to e.g. stamp PDF-files. Now my question is: how to split (and is it even possible) the below command into executable TC "command" into the "Change single button" window:

pdftk %1 multistamp %2 output stamped.pdf

Where %1 is ment to be the 1st selected file and %2 naturally the second in TC's filebox.


Or do I have to make a simple .BAT -file or .CMD to make this work?

Thx!
//Timo
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Hi Timo.

- right click an existent button
- chose "Change..."
- press F1 key
- close [Cancel] the button dialog
- read the help:

%P%N = one file
%S, %S1,... %S10 = one or more files
%L, %F = List of all selected files. Use a batch or script to loop over them.


You have to play with correct quoting, %P%N is auto-quoted if space is found, other params you have to quote on yourself.
Or handle quotes in your script, by removing quote if any or not, and add quotes yourself.




I usually use a VBScript to loop over each line of the temp file from the "%L" parameter:

CMD: "D:\rive\path to my\script.vbs"
PARAM: "%L"





There are already maaany scripts to discover in this forum.

Do you need someone to write such script for you?

 
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Execute command line tool with two selected files

Post by *Stefan2 »

Just tested:

I think %C1 and %C2 are the right parameters



Copy&Paste button code for testing purpose

Code: Select all

TOTALCMD#BAR#DATA
cmd /k
ECHO pdftk "%C1" multistamp "%C2" output stamped.pdf
C:\windows\system32\cmd.exe
Test pdftk with "%C1" 


-1
(copy code, right click TC button bar, paste)


Result (same panel):

Code: Select all

pdftk "X:\PSPad5\Language\Bulgarian.ini" multistamp "X:\PSPad5\Language\Esperanto.ini" output stamped.pdf
or (from both panels)

Code: Select all

pdftk "X:\PSPad5\Language\Bulgarian.ini" multistamp "Z:\PDF\pdftk\pdftk.1.txt" output stamped.pdf



 
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

%C1 and %C2 return first two files from combined selection in both panels, so it may be confusing when there are selected files in some panel.
It would be easier to use just %P%N for the focused file in active panel and %T%M for the focused file in inactive panel.
tjsuominen
Junior Member
Junior Member
Posts: 26
Joined: 2009-01-07, 12:31 UTC

Post by *tjsuominen »

Hi:

Thx for supporting me :) I think I got forward, but still not success. I even read the help document for that part. Let me try to explain further.

pdftk: that is the name of the executable, runs everywhere from my Command prompt as it's location is added to the Windows 10 variables path.

My current Button command is (in single line):
cmd.exe /c pdftk %S1 multistamp %S2 output stamped.pdf

Parameters: <empty>

Start-path: Defined to the location where %S1 and %S2 files are located.

Stamped.pdf: generated PDF-file after successful execution.


My guess is that those %S1 and %S2 can not be picked to the Command?


What happens: I select from TC the two files (expect them to be the %S1 and %S2) > Click on the button > Some Window just flashes fast (can't see what it says) and no output (stamped.pdf) file is generated.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Yes, %S1, %S2 is the wrong way for your issue.

Test this button:

CMD: cmd /k
PARAM: ECHO pdftk "%S1" multistamp "%S2" output stamped.pdf



Read the output and understand what %S1 / %S2 does.


- - -


Why not testing my example with %C1, %C2 ?

CMD: cmd /k
PARAM: ECHO pdftk "%C1" multistamp "%C2" output stamped.pdf


Read the output and understand what %C1 / %C2 does.



Next remove ECHO from PARAM, add instead the path(*) to your PDFTK and let it run.

CMD: cmd /k
PARAM: X:\Tools\PDF\pdftk "%C1" multistamp "%C2" output stamped.pdf  

If you have space in path to your pdftk.exe, this will be a bit more complicated to quote the whole string.
Copy whole pdftk folder to a path without space and try from there.

(*) That may not be need for you, as your system already knows about pdftk, as you wrote. But may be need for others.




 
Last edited by Stefan2 on 2017-05-05, 11:33 UTC, edited 1 time in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

tjsuominen wrote: My current Button command is (in single line):
cmd.exe /c pdftk %S1 multistamp %S2 output stamped.pdf

Parameters: <empty>
...
That do not work.

TCs parameter like %P %N %S %C etc. can normally be use in Parameter field only.


- - -

Again, try with ECHO first:

CMD: cmd /k ECHO pdftk "%C1" multistamp "%C2" output stamped.pdf
PARAM:


RESULT:
pdftk "%C1" multistamp "%C2" output stamped.pdf


- - -

AND use cmd /k instead of /c, just for the testing time.


- - -

And the "Start-path: " can be left empty.




 
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

tjsuominen wrote:My current Button command is (in single line):
cmd.exe /c pdftk %S1 multistamp %S2 output stamped.pdf

Parameters: <empty>

Start-path: Defined to the location where %S1 and %S2 files are located.

Stamped.pdf: generated PDF-file after successful execution.
1. As Stefan2 said, you only can use %-parameters in Parameters field, so only cmd.exe should be in Command field. And, %S2 is not a path to second file, it inserts paths to first and second files.
2. If you use cmd.exe, it is better to quote full command that is specified after /c to prevent errors caused by quoted program path (dump cmd.exe likes to strip first and last symbol of command line if these symbols are quotes so e.g. command cmd.exe /c "C:\Program Files\App\app.exe" -file "C:\doc.txt" will be broken).
3. It is better to specify full path to output file, e.g. %P if you want it to be created in active panel (it is especially important when you have non-empty Start path).

So I would use such button:

Code: Select all

Command: cmd.exe
Parameters: /c "pdftk "%C1" multistamp "%C2" output "%P\stamped.pdf""
Finally, you don't need cmd.exe at all, you can start pdftk directly:

Code: Select all

Command: pdftk
Parameters: "%C1" multistamp "%C2" output "%P\stamped.pdf"
This will use focused files in both panels and produce stamped.pdf file in active panel.

Also keep in mind that you can't control which selected file is %C1 and which one is %C2.
tjsuominen
Junior Member
Junior Member
Posts: 26
Joined: 2009-01-07, 12:31 UTC

Post by *tjsuominen »

Excellent and big thanks - got it working exactly as I wanted!

This forum is (mostly) as super as TC itself :)

//t
Post Reply