Printable Size

About:
This script adds a Command and a Column to DOpus which can be used to show the maximum printable size for all selected image files. The Command can either output to the Script Log or to a custom dialog. The Column is configurable and in addition to the actual print size the current DPI and units used can be toggled on/off in the column.

(I doubt anyone has a use for so many different ways to show the calculations :slight_smile: but it's been a learning exercise for me and perhaps someone might benefit from the code. Even better, a more experienced javascript wrangler might offer some insight or ideas for improvement).

History:

  • 1.0 - Initial Release.

Installation:

  1. Download: Printable Size 1.0.js.txt (7.3 KB)
  2. Drag the .js.txt file to Preferences / Toolbars / Scripts.

Script Config:
A number of settings are available in the normal Script Addin configuration editor. These options affect either both the command and column or just the column.

Both

  • DPI - Sets the DPI number which is used to calculate the printable size.
  • Inches - Set this to True to show all output in inches rather than centimetres.

Column Only

  • Directories - Enter a string to be used for directories (can be blank).
  • Files - Enter a string to be used for non image files (can be blank).
  • Show DPI - Choose whether the DPI setting is displayed in the column.
  • Show Units - Choose whether the units (cm/inches) are displayed in the column.

Usage: Column
To use the column (once the script is installed) simply choose your options and then turn on the "Script/Printable Size" column.

Usage: Command

  1. Download: Printable Size.dcf (257 Bytes)
  2. Select "Settings/Customize Toolbar..." from your Lister and then drag the button file to any toolbar you like.

The script adds the "PrintableSize" command to DOpus and it accepts a number of arguments. These arguments will override identical settings configured in 'Preferences/Scripts'.

Argument	Type	Possible Values		Description
 
 DPI		/N		<integer>			The Dots Per Inch (dpi) number which will be used for calculating
 										the printable size.
 
 INCHES		/S		(no value)			Changes the output of the script to use inches rather than centimetres.
 												
 SIMPLE		/S		(no value)			Forces the script to send all results to the Script Log and no dialog
 										window will be opened.
 											
 											e.g.    PrintableSize DPI=350 INCHES SIMPLE 

Screenshots:

The first image shows the Column in a lister:

The second image shows results from the Command in the Script Log:

The third image shows results from the Command displayed using the custom dialog:

Script:

// Print Size
// (c) 2020 Steve Banham

scriptName = "Printable Size";
scriptVersion = "1.0";
scriptDate = "24/3/2020";
scriptCopyright = "(c) 2020 Steve Banham";
scriptMinVersion = "12";
scriptDesc = "Provides a Column and a Command to show the maximum printable size of image files.";

function OnInit(initData) {
    initData.name = scriptName;
    initData.version = scriptVersion;
    initData.copyright = scriptCopyright;
    initData.desc = scriptDesc;
    initData.default_enable = true;
    initData.min_version = scriptMinVersion;
    //initData.log_prefix = "";

    initData.config_desc = DOpus.Create.Map();
    initData.config_groups = DOpus.Create.Map();
    var configName = "";

    configName = "DPI";
    initData.Config[configName] = "200";
    initData.config_desc(configName) = "Select the DPI to use when calculating print size.";
    initData.config_groups(configName) = "Both";

    configName = "Inches";
    initData.Config[configName] = false;
    initData.config_desc(configName) = "Display results in inches."
    initData.config_groups(configName) = "Both";

    configName = "Show Units";
	initData.Config[configName] = true;
	initData.config_desc(configName) = "Include measurement unit in the column."
    initData.config_groups(configName) = "Column Only";
    
    configName = "Show DPI";
	initData.Config[configName] = true;
	initData.config_desc(configName) = "Include DPI in the column."
    initData.config_groups(configName) = "Column Only";
	
	configName = "Directories";
	initData.Config[configName] = "<dir>";
	initData.config_desc(configName) = "The string used in the column for directories (can be blank).";
	initData.config_groups(configName) = "Column Only";
	
	configName = "Files";
	initData.Config[configName] = "---";
	initData.config_desc(configName) = "The string used in the column for files which aren't images (can be blank).";
	initData.config_groups(configName) = "Column Only";
	
	var col = initData.AddColumn();
	
	col.name = "PrintableSize";
    col.method = "OnPrintableSize";
    col.label = "Printable Size";
    col.autogroup = true; 
    col.autorefresh = true;
    col.justify = "left";
    col.match.push_back("Yes");

    var cmd = initData.AddCommand();
    cmd.name = "PrintableSize";
    cmd.method = "onPrintSize";
    cmd.desc = "Calculate the printable size of an image file.";
    cmd.label = "PrintableSize";
    cmd.template = "DPI/N,SIMPLE/S,INCHES/S";
}

function onPrintSize(scriptCmdData) {

    var srcTab = scriptCmdData.func.sourcetab;
    var argDpi = Script.Config["DPI"];
    var argInches = Script.Config["Inches"];
    var strUnits = "cm";

    if (argInches == true){
        strUnits = "\"";
    }
    if (scriptCmdData.func.args.got_arg.inches) {
        argInches = true;
        strUnits = "\"";
    }
    if (scriptCmdData.func.args.got_arg.dpi) {
        argDpi = scriptCmdData.func.args.dpi;
    }

    if (srcTab.stats.selfiles == 0) {
        return;
    }

    var showDialog = true;

    if (scriptCmdData.func.args.got_arg.simple) {
        showDialog = false;
        DOpus.ClearOutput();
    }
    else {
        var dlg = DOpus.Dlg;
        dlg.window = srcTab;
        dlg.title = scriptName;
        dlg.template = "dialog1";
        dlg.detach = true;
        dlg.Show();
        showDialog = true;

        if (!argInches) dlg.Control("editUnits").value = "Centimetres";
        if (argInches) dlg.Control("editUnits").value = "Inches";
        dlg.Control("editDPI").value = argDpi;
    }

    if (scriptCmdData.func.args.got_arg.dpi) argDpi = scriptCmdData.func.args.dpi; 

    for (var eItems = new Enumerator(srcTab.selected_files); !eItems.atEnd(); eItems.moveNext()) {
        
        if (eItems.item().metadata() == "image") {

            var prtMaxWidth = eItems.item().metadata.image_text.picwidth / argDpi;
            var prtMaxHeight = eItems.item().metadata.image_text.picheight / argDpi;
            
            if (strUnits == "cm") {
                prtMaxWidth = prtMaxWidth * 2.54;
                prtMaxHeight = prtMaxHeight * 2.54;
            }
    
            prtMaxWidth = prtMaxWidth.toFixed(2);		
            prtMaxHeight = prtMaxHeight.toFixed(2);
            
            prtComplete = prtMaxWidth + strUnits + " x " + prtMaxHeight + strUnits;

            if (showDialog) {
                var i = dlg.Control("listviewResults").AddItem(eItems.item().name);
                dlg.Control("listviewResults").GetItemAt(i).subitems(0) = prtComplete;
            }
            if (!showDialog) {
                DOpus.Output(prtComplete + " (" + eItems.item().name + ")");
            }
        }
    }    
    if (!showDialog) return;
    
    dlg.Control("listviewResults").columns.AutoSize();
       
    while (true) {
         var msg = dlg.GetMsg();
        if (!msg.result) break;
    }
}

function OnPrintableSize(scriptColData) {
    
    var argInches = Script.Config["Inches"];
    var strUnits = "\"";

	if (scriptColData.item.is_dir) {
		scriptColData.value = Script.Config["Directories"];
		return;
	}
	
	if (scriptColData.item.metadata() == "image") {	
	
		var prtMaxWidth = scriptColData.item.metadata.image_text.picwidth / Script.Config["DPI"]
		var prtMaxHeight = scriptColData.item.metadata.image_text.picheight / Script.Config["DPI"];
		
		if (argInches == false) {
			prtMaxWidth = prtMaxWidth * 2.54;
            prtMaxHeight = prtMaxHeight * 2.54;
            strUnits = "cm";
		}
		
		prtMaxWidth = prtMaxWidth.toFixed(2);		
		prtMaxHeight = prtMaxHeight.toFixed(2);

		var strDPI = " (" + Script.Config["DPI"] + " dpi)";
		
		if (Script.Config["Show DPI"] == false) {
			var strDPI = "";
		}
		
		if (Script.Config["Show Units"] == false) {
			strUnits = "";
		}
		
		scriptColData.value = prtMaxWidth + strUnits + " x " + prtMaxHeight + strUnits + strDPI;
	}
	else {
		scriptColData.value = Script.Config["Files"];
	}
}

function OnAboutScript(aboutData)
{
	var dlg = DOpus.Dlg;
	dlg.window = aboutData.window;
	dlg.title = scriptName;
	dlg.message = scriptName + " v" + scriptVersion + "\t\t\t\t" + scriptDate+  "\n\n" + scriptDesc + "\n\n" + scriptCopyright;
	dlg.buttons = "Close";
	dlg.icon = "info";
	dlg.show;
}

==SCRIPT RESOURCES
<resources>
	<resource name="dialog1" type="dialog">
		<dialog fontsize="9" height="255" lang="english" resize="yes" standard_buttons="ok" title="Printable Size" width="274">
			<control fullrow="yes" height="191" name="listviewResults" resize="wh" sort="yes" type="listview" viewmode="details" width="264" x="5" y="43">
				<columns>
					<item text="Filename" />
					<item text="Maximum Printable Size" />
				</columns>
			</control>
			<control halign="center" height="10" name="staticDPI" title="DPI:" type="static" width="27" x="41" y="19" />
			<control halign="center" height="12" name="editDPI" readonly="yes" title="0" type="edit" width="36" x="70" y="17" />
			<control height="34" name="group1" resize="w" title="Using" type="group" width="264" x="5" y="3" />
			<control halign="center" height="10" name="staticUnits" title="Units:" type="static" width="27" x="138" y="19" />
			<control halign="center" height="12" name="editUnits" readonly="yes" title="Centimetres" type="edit" width="58" x="171" y="17" />
		</dialog>
	</resource>
</resources>
4 Likes