Hi,
I want to create button that should do the following:
Search for specific image types in the actual folder (recursive)
Select all in the resulting list
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
lxp
January 4, 2021, 7:09pm
2
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('.jpg', '.bmp', '.png', '.gif', '.webm');</instruction>
<instruction />
<instruction> cmd.ClearFiles();</instruction>
<instruction> var folderEnum = fsu.ReadDir(tab.path, 'r'); // 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('Image CONVERT=jpg QUALITY=90 WIDTH=500 PRESERVEASPECTRATIO HERE REPLACE ADDSUFFIX "_thumb"');</instruction>
<instruction>}</instruction>
</function>
</button>
This post explains how to add buttons to toolbars and menus, and also applies to raw commands for things like standalone hotkeys.
For how to use Script Add-Ins, please scroll down to see the post below this one.
Much of this assumes Directory Opus…
2 Likes