Jscript code for Iconset Maker v2

This the JScript button code for Iconmaker v2 which can be found in the Icons section of the forum.

//    _____                         _     __  __       _                    ___  
//   |_   _|                       | |   |  \/  |     | |                  |__ \ 
//     | |  ___ ___  _ __  ___  ___| |_  | \  / | __ _| | _____ _ __  __   __ ) |
//     | | / __/ _ \| '_ \/ __|/ _ | __| | |\/| |/ _` | |/ / _ | '__| \ \ / // / 
//    _| || (_| (_) | | | \__ |  __| |_  | |  | | (_| |   |  __| |     \ V // /_ 
//   |_____\___\___/|_| |_|___/\___|\__| |_|  |_|\__,_|_|\_\___|_|      \_/|____|
//                                                                               
//                                                                               
//
// Button for making Directory Opus icon sets see post in the 'Icons' forum.
//                                                                               
//

// LOGGING FOR Log() - SET TO FALSE TO TURN OFF LOGGING
var log = false;

// duplicateCheck, when true will prevent another dialog being displayed every time
// the button is pressed after the first time. In rare circumstances, for instance, when playing
// around with the code and putting in something that causes the program to abort due to syntax error
// the session variable 'duplicatedialog' might not be reset to false and clicking the button for the 
// first time will do nothing. Session variables will be reset after a proper close of Directory Opus.
// You can turn this feature on or off :
var duplicateCheck = false;

function OnClick(clickData)
{
    Log("Iconset Maker v2", "#4455ff", 1);

    // OPEN THE 'OTHER LOGS' CONSOLE AND CLEAR IT
    // if (log) {
    //     clickData.func.command.RunCommand("Set UTILITY=otherlog")
    //     DOpus.ClearOutput();
    // }


    // GLOBAL VARIABLES
    var iconName;
    var iconArtist;
    var iconCopy;
    var iconLarge;
    var iconSmall;
    var xmlArray = [];
    var pngArray = [];
    var folderPath = clickData.func.sourcetab.path;
    var montagePath = folderPath + "\\montage.exe";


    // SESSION VARIABLES (Ends on DO exit)
    // duplicate dialog check, return if dialog active
    if (duplicateCheck) {
        if (DOpus.vars.Exists("duplicatedialog")) {
            Log("----> duplicatedialog variable exists (deleted on DO exit)");
            if (DOpus.vars("duplicatedialog").value)  {
                Log("----> duplicate dialog - returning");
                return;
            }
        } else Log("----> no duplicatedialog session variable present");
        DOpus.vars.Set("duplicatedialog", true);
    }


    // CHECK MONTAGE IN CURRENT FOLDER ***************************************************************************

    if (!DOpus.FSUtil.Exists(montagePath)) {

        // Montage dialog init
        var dlgMontage = DOpus.Dlg;
        dlgMontage.template = "Montage dialog";
        dlgMontage.LoadPosition("Foo_Dialog_Position");
        dlgMontage.title = "Directory Opus";
        dlgMontage.detach = true;
        dlgMontage.icon = "info"
        dlgMontage.Create();

        dlgMontage.Control("static1").fg = "#00961e";
        dlgMontage.Control("static1").label = "montage.exe is not in the current folder";
        dlgMontage.Control("static2").fg = "#0000ff";
        dlgMontage.Control("static2").label = "Click here to goto the imagemagick website to download it";
        dlgMontage.Control("static3").fg = "#404040";
        dlgMontage.Control("static3").label = "ImageMagick, invoked from the command line as magick, is a free and open-source cross-platform software suite for displaying, creating, converting, modifying, and editing raster images. \nIt includes a program called montage which can scale and stitch images together to make the two icon sheets Directory Opus uses.";
        dlgMontage.Control("static4").fg = "#404040";
        dlgMontage.Control("static4").label = "Download and Extract one of the portable versions of ImageMagick, \nthey are at the bottom of the downloads page.";
        dlgMontage.Control("static5").fg = "#404040";
        dlgMontage.Control("static5").label = "Make a folder somewhere and put montage.exe and the individual square PNG images of any size in it.";
        dlgMontage.Control("static6").fg = "#404040";
        dlgMontage.Control("static6").label = "and then use this button again.";

        while (true) {
            var msg = dlgMontage.GetMsg();
            if (!msg.result) break;
            if (msg.event == "click" && msg.control == "static2") {
                DOpus.create.command.RunCommand("https://imagemagick.org/script/download.php");
            }
        }
        dlgMontage.SavePosition("Foo_Dialog_Position");
        if (duplicateCheck) DOpus.vars.Set("duplicatedialog", false);
        return;
    }
    // montage exists continue ....


    // CHECK THERE ARE PNG'S IN FOLDER ***************************************************************************

    var regex = /\.png/i;
    var collection = DOpus.FSUtil.ReadDir(folderPath);
    while (!collection.complete)
    {
        var item = collection.Next();
        // Read png names into pngArray[]
        if (item.ext.match(regex)) {
            pngArray.push(item.name_stem);
        }
    }

        if (!pngArray.length) {
        Log("NO PNG's IN THIS FOLDER", "#cc4952");

        // PNG dialog init
        var dlgPNG = DOpus.Dlg;
        dlgPNG.template = "PNG dialog";
        dlgPNG.LoadPosition("Foo_Dialog_Position");
        dlgPNG.title = "Directory Opus";
        dlgPNG.detach = true;
        dlgPNG.icon = "info"
        dlgPNG.Create();

        dlgPNG.Control("static1").fg = "#00961e";
        dlgPNG.Control("static1").label = "THERE ARE NO PNG IMAGES IN THIS FOLDER";
        dlgPNG.Control("static2").fg = "#0000ff";
        dlgPNG.Control("static2").label = "Copy images that will be converted to icons into this folder.";
        dlgPNG.Control("static3").fg = "#0000ff";
        dlgPNG.Control("static3").label = "\nAny images that are not PNG format will not be processed, they can be converted using the 'Convert' button on the 'Images' toolbar. Also, there is usually a convert image option in the context menu";
        dlgPNG.Control("static4").fg = "#0000ff";
        dlgPNG.Control("static4").label = "The images can be of any size.\nSquare images are best for icons though not necessary.";

        while (true) {
            var msg = dlgPNG.GetMsg();
            if (!msg.result) break;
            if (msg.event == "click") {
                DOpus.create.command.RunCommand("https://en.wikipedia.org/wiki/Portable_Network_Graphics");
            }
        }
        dlgPNG.SavePosition("Foo_Dialog_Position");
        if (duplicateCheck) DOpus.vars.Set("duplicatedialog", false);
        return;
    } 
    // png's present continue ....
    

    // MAIN DIALOG INIT ******************************************************************************************

    var dialog = DOpus.Dlg;
    dialog.template = "Main Dialog";
    dialog.LoadPosition("Foo_Dialog_Position");
    dialog.title = "Directory Opus";
    dialog.detach = true;
    dialog.Create();

    dialog.Control("static1").fg = "#00339e";
    dialog.Control("static1").label = "Enter a unique name for the iconset";
    dialog.Control("static2").fg = "#00339e";
    dialog.Control("static2").label = "Large Icon Size";
    dialog.Control("static3").fg = "#00339e";
    dialog.Control("static3").label = "Small Icon Size";
    dialog.Control("static7").fg = "#00339e";
    dialog.Control("static7").label = "Artist";
    dialog.Control("static8").fg = "#00339e";
    dialog.Control("static8").label = "Copyright";
    dialog.Control("static9").bg = "#ff0000";
    dialog.Control("static9").fg = "#ffffff";
    dialog.Control("static10").fg = "#3377be";
    dialog.Control("static10").label = "PNG images only. Square ones. Any size.";

    // sets some vars from Directory Opus persistant variables
    function SetVars() {
        if (DOpus.vars.Exists("session_iconName")) iconName = DOpus.vars.Get("session_iconName");
        else iconName = "";
        dialog.Control("edit1").value = iconName;

        if (DOpus.vars.Exists("session_iconLarge")) iconLarge = DOpus.vars.Get("session_iconLarge");
        else iconLarge = 32;
        dialog.Control("edit2").value = iconLarge;

        if (DOpus.vars.Exists("session_iconSmall")) iconSmall = DOpus.vars.Get("session_iconSmall");
        else iconSmall = 24;
        dialog.Control("edit3").value = iconSmall;
    }

    // init name with selected item if an 'iconset Maker' sub-folder is selected in source tab
    var source = clickData.func.sourcetab;
    if (source.stats.selitems > 0) {
        // an item is selected in folder
        var item = source.selected(0).name_stem;
        var regex = /(.+) \((\d+)px & (\d+)px/;  // 'folder name (22px & 33px)' captures folder name and two px values

        if (item.match(regex) == null) {
            // item does not match regex
            SetVars();
        } else {
            // item matches regex
            dialog.Control("edit1").value = item.match(regex)[1];
            dialog.Control("edit2").value = item.match(regex)[2];
            dialog.Control("edit3").value = item.match(regex)[3];
        }
    } else {
        // item not selected call SetVars
        SetVars();
    }
    dialog.Control("edit1").SelectRange(0,-1);

    // sets artist and copyright from Directory Opus persistant variables
    if (DOpus.vars.Exists("session_iconArtist")) iconArtist = DOpus.vars.Get("session_iconArtist");
    else iconArtist = "Various";
    dialog.Control("edit4").value = iconArtist;

    if (DOpus.vars.Exists("session_iconCopy")) iconCopy = DOpus.vars.Get("session_iconCopy");
    else iconCopy = "© 2022";
    dialog.Control("edit5").value = iconCopy;

    // MAIN DIALOG MESSAGE LOOP **********************************************************************************
    while (true) {
        var msg = dialog.GetMsg();
        if (!msg.result) break;

        var err = 0;
        var text = dialog.Control("static9");
        iconName = dialog.Control("edit1").value;
        iconLarge = +dialog.Control("edit2").value;
        iconSmall = +dialog.Control("edit3").value;
        iconArtist = dialog.Control("edit4").value;
        iconCopy = dialog.Control("edit5").value;

        if (msg.event == "click" && dialog.Control("button1").focus == true) {
            Log("OK pressed");

            if (iconName == "") err = 1;
            else if (iconLarge == 0) err = 2;
            else if (iconLarge < 12) err = 3;
            else if (iconLarge > 256) err = 4;
            else if (iconSmall == 0) err = 5;
            else if (iconSmall < 12) err = 6;
            else if (iconSmall > 256) err = 7;
            else if (iconSmall >  iconLarge) err = 8;
            else if (iconSmall == iconLarge) err = 9;
            else if (iconArtist == "") err = 10;
            else if (iconCopy == "") err = 11;

            switch(err) {
                case 0 :    Log("No err");
                            break;
                case 1 :    text.label = "Enter a name";
                            break;
                case 2 :    text.label = "Enter a size for Large icons";
                            break;
                case 3 :    text.label = "Large icons are less than 12px";
                            break;
                case 4 :    text.label = "Large icons are greater than 256px";
                            break;
                case 5 :    text.label = "Enter a size for Small icons";
                            break;
                case 6 :    text.label = "Small icons are less than 12px";
                            break;
                case 7 :    text.label = "Small icons are greater than 256px";
                            break;
                case 8 :    text.label = "Small icons are bigger than Large";
                            break;
                case 9 :    text.label = "Small and Large icons are the same";
                            break;
                case 10 :   text.label = "Enter a name for the Artist";
                            break;
                case 11 :   text.label = "Enter a Copyright";
                            break;
                default :   Log("default");
            }
            
            if (err > 0) {
                text.visible = 1;
                continue;
            } else {
                break;
            }
        } else text.visible = 0;
    }
    // ********************* END MAIN DIALOG MESSAGE LOOP ********************************************************
    dialog.SavePosition("Foo_Dialog_Position");

    if (dialog.result == 0) {
        Log("Cancelled - closing dialog");
        if (duplicateCheck) DOpus.vars.Set("duplicatedialog", false);
        return;
    }

    // OK, CONTINUE .... ******************************************************************************************
    var folderName = iconName + " (" + iconLarge + "px " + "& " + iconSmall + "px)";
    var newSubFolder = folderPath + "\\" + folderName;
    var fileNameLarge = "Large_" + iconLarge + "px.png";
    var fileNameSmall = "Small_" + iconSmall + "px.png";

    dialog.Control("static10").fg = "#009e33";
    dialog.Control("static10").label = "Takes a few seconds, please wait ....";

    Log("****************************");
    Log("name = \t\t" + iconName, "#00861e");
    Log("Large = \t\t" + iconLarge, "#00861e");
    Log("Small = \t\t" + iconSmall, "#00861e");
    Log("artist = \t\t" + iconArtist, "#00861e");
    Log("copyright = \t" + iconCopy, "#00861e");
    Log("****************************");
    Log('Sub-Folder = \t' + (DOpus.FSUtil.Exists(newSubFolder) ? 'EXISTS' : 'NOT FOUND'));
    Log('folderpath = \t' + folderPath);
    Log('foldername = \t' + folderName);
    Log("****************************");

    
    // CREATE FOLDER, DELETE FIRST IF IT EXISTS ******************************************************************
    if (DOpus.FSUtil.Exists(newSubFolder)) {
        var command = "";
        command += 'Delete ';
        command += '"' + newSubFolder + '"';
        command += " QUIET";
        Log(command, "#f509e8");
        clickData.func.command.SetModifier("noprogress");
        clickData.func.command.RunCommand(command);
    }
    var command = "";
    command += 'CreateFolder NAME=';
    command += '"' + folderPath + '//' + folderName + '"';
    Log(command, "#f509e8");

    clickData.func.command.SetModifier("noprogress");
    clickData.func.command.RunCommand(command);



    // MONTAGE ***************************************************************************************************

    function CallMontage(size, filename) {
        var imagefilter = "*.png";
        var montage = "";
        montage += (folderPath + "\\montage ");
        montage += '"' + folderPath + "\\" + imagefilter + '"' + " " ;
        montage += "-geometry " + size + "x" + size + "+0+0 ";
        montage += "-tile 16 -background transparent -filter Lanczos -quiet ";
        montage += '"' + newSubFolder;
        montage += "\\" + filename + '"';

        clickData.func.command.AddLine("@runmode:hide");
        clickData.func.command.AddLine("@sync:" + montage);
        clickData.func.command.Run();
    }

    // Large icons
    CallMontage(iconLarge, fileNameLarge);

    // Small icons
    CallMontage(iconSmall, fileNameSmall);

    
    // CREATE XML ************************************************************************************************

    function WriteIconList() {
        var row=1;
        var col=1;
        for (var i in pngArray) {
            xmlArray.push('     <icon name="' + pngArray[i] + '" row="' + row +'" col="' + col + '" />');
            col++;
            if (col > 16) {
                col = 1;
                row++;
            }
        }
    }

    // WRITE XML TO ARRAY
    xmlArray.push('<?xml version="1.0" encoding="UTF-8"?>');
    xmlArray.push('<iconset name="' + folderName.replace(/ /g, "_") + '">');
    xmlArray.push(' <display_name>' + iconName + '</display_name>');
    xmlArray.push(' <copyright>' + iconCopy + '</copyright>');
    xmlArray.push(' <artist>' + iconArtist + '</artist>');
    xmlArray.push(' <set size="small" width="' + iconSmall + '" height="' + iconSmall + '" filename="' + fileNameSmall + '">');
    WriteIconList();
    xmlArray.push(' </set>');
    xmlArray.push(' <set size="large" width="' + iconLarge + '" height="' + iconLarge + '" filename="' + fileNameLarge + '">');
    WriteIconList();
    xmlArray.push(' </set>');
    xmlArray.push('</iconset>');

    // CONVERT XML ARRAY TO STRING
    xmlArray = xmlArray.join("\n");
    Log("   ");
    Log(xmlArray);
    Log("   ");
    
    // WRITE XML TO FILE
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var a = fso.CreateTextFile(newSubFolder + "\\Iconset.xml", true, true);
    a.WriteLine(xmlArray);
    a.Close();


    // CREATE ARCHIVED .DIS FILE *********************************************************************************
    var command1 = "";
    command1 += 'Copy ';
    command1 += '"' + folderName + '"';
    command1 += " ARCHIVE TO ";
    command1 += '"' + folderPath + '"';
    command1 += ' CREATEFOLDER=' + '"' + folderName + '.dis"';
    Log(command1, "#f509e8");

    // MOVE TO FOLDER
    var command2 = "";
    command2 += 'Copy ';
    command2 += '"' + folderName + '.dis"';
    command2 += ' MOVE TO ';
    command2 += '"' + newSubFolder + '"';
    Log(command2, "#f509e8");

    clickData.func.command.SetModifier("noprogress");
    clickData.func.command.AddLine(command1);
    clickData.func.command.AddLine(command2);
    clickData.func.command.Run();


    // ******************************************** END **********************************************************


    // PERSISTANT VARIABLES
    DOpus.vars.Set("session_iconName", iconName);
    DOpus.vars("session_iconName").persist = true;

    DOpus.vars.Set("session_iconLarge", iconLarge);
    DOpus.vars("session_iconLarge").persist = true;

    DOpus.vars.Set("session_iconSmall", iconSmall);
    DOpus.vars("session_iconSmall").persist = true;

    DOpus.vars.Set("session_iconArtist", iconArtist);
    DOpus.vars("session_iconArtist").persist = true;

    DOpus.vars.Set("session_iconCopy", iconCopy);
    DOpus.vars("session_iconCopy").persist = true;

    // SESSION VARIABLES
    if (duplicateCheck) DOpus.vars.Set("duplicatedialog", false);

	Log("END");



    function Log(str, color, init) {
        if (color === undefined) color = "#000000";
        if (log) {
            if (init) {
                clickData.func.command.RunCommand("Set UTILITY=otherlog")
                DOpus.ClearOutput();
                }
            }
            DOpus.Output("<font color=" + color + ">" + str + "</font>");
    }

}
<resources>
	<resource name="Main Dialog" type="dialog">
		<dialog fontface="Arial Bold" fontsize="12" height="180" lang="english" width="252">
			<control halign="center" height="8" name="static1" title="static1" type="static" valign="top" width="116" x="63" y="12" />
			<control halign="left" height="12" max="31" name="edit1" type="edit" width="112" x="66" y="24" />
			<control halign="right" height="8" name="static2" title="static2" type="static" valign="top" width="47" x="64" y="51" />
			<control halign="right" height="8" name="static3" title="static3" type="static" valign="top" width="47" x="64" y="64" />
			<control halign="left" height="12" max="3" name="edit2" number="yes" title="32" type="edit" width="19" x="114" y="50" />
			<control halign="left" height="12" max="3" name="edit3" number="yes" title="24" type="edit" width="19" x="114" y="63" />
			<control default="yes" height="14" name="button1" title="OK" type="button" width="50" x="68" y="156" />
			<control close="0" height="14" name="button2" title="Cancel" type="button" width="50" x="134" y="156" />
			<control halign="left" height="8" name="static5" title="px" type="static" valign="top" width="12" x="135" y="52" />
			<control halign="left" height="8" name="static6" title="px" type="static" valign="top" width="12" x="135" y="65" />
			<control halign="right" height="8" name="static7" title="static7" type="static" valign="top" width="19" x="44" y="91" />
			<control halign="left" height="12" max="31" name="edit4" type="edit" width="112" x="66" y="90" />
			<control halign="right" height="8" name="static8" title="static8" type="static" valign="top" width="33" x="30" y="109" />
			<control halign="left" height="12" max="31" name="edit5" type="edit" width="112" x="66" y="108" />
			<control halign="center" height="8" name="static9" title="static9" type="static" valign="top" visible="no" width="112" x="66" y="39" />
			<control halign="center" height="8" name="static10" title="static10" type="static" valign="top" width="224" x="14" y="133" />
		</dialog>
	</resource>
	<resource name="Montage dialog" type="dialog">
		<dialog fontface="Arial Bold" fontsize="12" height="180" lang="english" width="252">
			<control close="0" default="yes" height="14" name="button1" title="OK" type="button" width="50" x="101" y="155" />
			<control halign="center" height="8" name="static1" title="Static" type="static" valign="top" width="224" x="14" y="10" />
			<control halign="center" height="8" name="static2" notify="yes" title="Static" type="static" valign="top" width="224" x="14" y="30" />
			<control halign="center" height="40" name="static3" title="Static" type="static" valign="top" width="224" x="14" y="43" />
			<control halign="center" height="19" name="static4" title="Static" type="static" valign="top" width="224" x="14" y="93" />
			<control halign="center" height="19" name="static5" title="Static" type="static" valign="top" width="224" x="14" y="116" />
			<control halign="center" height="12" name="static6" title="Static" type="static" valign="top" width="224" x="14" y="139" />
		</dialog>
	</resource>
	<resource name="PNG dialog" type="dialog">
		<dialog fontface="Arial Bold" fontsize="12" height="139" lang="english" width="252">
			<control close="0" default="yes" height="14" name="button1" title="OK" type="button" width="50" x="101" y="117" />
			<control halign="center" height="8" name="static1" notify="yes" title="Static" type="static" valign="top" width="224" x="14" y="10" />
			<control halign="center" height="8" name="static2" notify="yes" title="Static" type="static" valign="top" width="224" x="14" y="30" />
			<control halign="center" height="40" name="static3" notify="yes" title="Static" type="static" valign="top" width="224" x="14" y="43" />
			<control halign="center" height="19" name="static4" notify="yes" title="Static" type="static" valign="top" width="224" x="14" y="88" />
		</dialog>
	</resource>
</resources>
1 Like