Find images and convert them

Hi,
I want to create button that should do the following:

  1. Search for specific image types in the actual folder (recursive)
  2. Select all in the resulting list
  3. Convert all the images to defined format/size

So far I got this working. I created a button that runs the following internal commands:

Find NAME="*.(jpg|bmp|png|gif|webm)" SHOWRESULTS=source RECURSE IN {sourcepath$}
Select ALL
Image CONVERT=jpg QUALITY=90 WIDTH=500 PRESERVEASPECTRATIO HERE REPLACE ADDSUFFIX "_thumb"

When I run each command on its own it works, but when I run it like it is here it finds the images and selects them, but don't start the conversion. To me it looks like a timing issue,
like the conversion starts and no file is selected.

Any idea?

Regards

Stefan

These commands don't mix too well in a command button. You are better off with a script like this:

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    cmd.deselect = false;

    var extensions = DOpus.Create().StringSetI('.jpg', '.bmp', '.png', '.gif', '.webm');

    cmd.ClearFiles();
    var folderEnum = fsu.ReadDir(tab.path, 'r'); // recursive
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        if (folderItem.is_dir) continue;
        if (!extensions.exists(folderItem.ext)) continue;
        cmd.AddFile(folderItem);
    }
    cmd.RunCommand('Image CONVERT=jpg QUALITY=90 WIDTH=500 PRESERVEASPECTRATIO HERE REPLACE ADDSUFFIX "_thumb"');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>37535</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    var extensions = DOpus.Create().StringSetI(&apos;.jpg&apos;, &apos;.bmp&apos;, &apos;.png&apos;, &apos;.gif&apos;, &apos;.webm&apos;);</instruction>
		<instruction />
		<instruction>    cmd.ClearFiles();</instruction>
		<instruction>    var folderEnum = fsu.ReadDir(tab.path, &apos;r&apos;); // recursive</instruction>
		<instruction>    while (!folderEnum.complete) {</instruction>
		<instruction>        var folderItem = folderEnum.Next();</instruction>
		<instruction>        if (folderItem.is_dir) continue;</instruction>
		<instruction>        if (!extensions.exists(folderItem.ext)) continue;</instruction>
		<instruction>        cmd.AddFile(folderItem);</instruction>
		<instruction>    }</instruction>
		<instruction>    cmd.RunCommand(&apos;Image CONVERT=jpg QUALITY=90 WIDTH=500 PRESERVEASPECTRATIO HERE REPLACE ADDSUFFIX &quot;_thumb&quot;&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

2 Likes

Thanks! That did it.