Fixing a button that trim images and restore their original timestamps

Hi! I'm trying to make a button that trims the selected images with ImageMagick, copies the timestamps of the source files and pastes them on the new ones with ClipboardEX. This is how my function looks like:

@runmode:hide
magick {filepath} -fuzz 5%% -trim +repage {filepath|noext}_trim{file|ext}
ClipboardEx COPYTIMESTAMPS FROM={filepath} TO={filepath|noext}_trim{file|ext}

And it works... kinda. It trims all images just fine, but the COPYTIMESTAMPS command only works for the first file. Since, as I understand it, the function runs once per file unless I use the @runonce modifier, this should work.

Am I missing something? Is there a better way to do this? Maybe it's a limitation of ClipboardEx?

Kinda. It expects to get all files passed at once. For the FROM part this would work

FROM="{allfilepath|sep=;}"

but I wouldn't know how to write the TO part.

ExifTool could do the job like this:

@runmode:hide 
@nodeselect 
@nofilenamequoting 
magick.exe "{filepath}" -fuzz 5%% -trim +repage "{filepath|noext}_trim{file|ext}"
exiftool.exe -overwrite_original -TagsFromFile "{filepath}" -FileCreateDate -FileModifyDate "{filepath|noext}_trim{file|ext}" 

Mhh, I'd say this should work.. the FROM/TO params support single/multiple items at once, I checked the code (but did not try yet on my own). Maybe try debugging your button with the Say script command first, you can see in the console whether the parameters you pass get through to the command as you expect them.
Say ClipboardEx COPYTIMESTAMPS FROM={filepath} TO={filepath|noext}_trim{file|ext}

ClipboardEx also has XLOG param, to get more output in the console:
ClipboardEx <param shere> XLOG=a

Say doesn't do anything on my pc, is there something else I need to install?

XLOG doesn't show anything weird, it just runs the command one time and that's it.

 2021-12-22 12:09 ClipEx:  CmdLine: ClipboardEx COPYTIMESTAMPS FROM=D:\Test\test1.jpg TO=D:\Test\test1_trim.jpg XLOG=all
 2021-12-22 12:09 ClipEx:  ClipboardEx_COPYTIMESTAMPS():
 2021-12-22 12:09 ClipEx:      FROM-Item: D:\Test\test1.jpg
 2021-12-22 12:09 ClipEx:      COPY-Item: D:\Test\test1.jpg
 2021-12-22 12:09 ClipEx:      ClipboardEx_PASTETIMESTAMPS():
 2021-12-22 12:09 ClipEx:          TO-Item   : D:\Test\test1_trim.jpg
 2021-12-22 12:09 ClipEx:          PASTE-Item: D:\Test\test1_trim.jpg
 2021-12-22 12:09 ClipEx:          Command: SetAttr CREATED="2021-07-02 07:58:21" MODIFIED="2021-07-02 07:58:21"

I even tried to add other commands at the end like Copy MOVE to make sure it isn't just Opus being weird, and it worked. Looks like lxp is right, I have to parse all the files at once. And I, too, don't know how to list them.

Yeah, maybe I should just do this. I wanted to try ClipboardEX to learn how to use scripts, and exiftool is so much slower than ClipboardEX, but maybe that just makes things more complicated.

You could run the two commands via a script, which could loop through the files and run them (as a pair) individually for each selected file.

The default script you get when creating a new script button should have most of the ingredients you need.

I don't know how to use JScript, I tried to modify the default one, but the only thing that I got it to do is to run the ClipboardEx COPYTIMESTAMPS on the first file again and again for each file selected. It's too much time to learn something that I know how to do now with the exiftools suggestion.

Thanks for your help, guys.

If ClipboardEx is executed only once and the previous command multiple times, there's nothing wrong with ClipboardEx. It probably is DO button magic, how it handles calls to script commands compared to how it handles calles to external programs.
If I remember correctly, I created a dedicated UserCommand called "ForEach", which kind of hides a script commands nature to DO and makes them run as usual command, thereby calling them multiple times as expected. Create "ForEach" user command like this:

EDIT: My screenshot does not upload, the forum is "Processing Upload.." forever, so just use my text explanation. You need to enter customize mode and find "Commands" tab in the "Customize" window, create new user command right there. UPDATE: Or see link below in next post for screenshot.

Name: ForEach
Template: CMD/R
Command code:

@nodeselect
&CMD&

Just prefix the line with "ClipboardEx" with "ForEach CMD=..".
Regarding "Say" it's another script command, you need to install before you can use it in DO, find it here:

The problem you encountered is probably related to this/mine:

Same issue:

Yooo, it works, and it looks pretty clean for a "hack". I didn't even know that user commands were a thing. Smart. I'm sure it will be helpful in the future. I got the Say script too.
Thanks!