Error 21: undefined is not an object.

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

general_knox

Error 21: undefined is not an object.

Post by general_knox »

Hi,

Ive got a script working on my computer that is based on the CS3's script "export layers to files". I modified some lines, etc...everything works 100% fine on my computer.

However, when I try moving it to another computer, it gives me this error:

Code: Select allError 21: undefined is not an object.
Line: 259
->      dlgMain.ddFileType.items[exportInfo.fileType].selected = true;


Im thinking its because in the script it saves settings to a registry key that the other computer doesnt have(?)

How would one go about fixing this issue? I cant even get the dialog menu to open when calling this script from another computer...it only works on my own.

PS: I can post the whole script if needed.

Thanks in advance for you help
Mike Hale

Error 21: undefined is not an object.

Post by Mike Hale »

It does save the settings to CustomOptions but if it can't find the settings it creates defaults.

My guess is that you made some change that the defaults can't handle. Take a look at the function initExportInfo(exportInfo) and see if you need to edit that as well.
general_knox

Error 21: undefined is not an object.

Post by general_knox »

Hi Mike,

Thanks for your quick reply. Here is the complete script. I looked at the initExportInfo funtion, tried debugging it without success. Basically I just removed options of file types you can save (limited to bmp and targa), and I added 2 features of being able to desaturate the files and/or scale them down by 50% upon running the script...I added a few lines here and there, you can see it all in my script below:

Code: Select all// Copyright 2007.  Adobe Systems, Incorporated.  All rights reserved.
// The original script exported each layer in the document to a separate file.
// Original script was written by Naoki Hada
// ZStrings and auto layout by Tom Ruark

//Modified for personal use by General_Knox


/*

// BEGIN__HARVEST_EXCEPTION_ZSTRING

<javascriptresource>
<name>$$$/JavaScripts/GN_ExportTextures/Menu=GN_ExportTextures...</name>
<about>$$$/JavaScripts/GN_ExportTextures/About=GN_ExportTextures ^r^rCopyright  2009 Artificial Mind & Movement. All rights reserved.^r^rExports each layer to an individual file.</about>
<category>layers</category>
<enableinfo>true</enableinfo>
</javascriptresource>

// END__HARVEST_EXCEPTION_ZSTRING

*/

// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop

// in case we double clicked the file
app.bringToFront();

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
// $.level = 0;
// debugger; // launch debugger on next line

// on localized builds we pull the $$$/Strings from a .dat file, see documentation for more details
$.localize = true;

//=================================================================
// Globals
//=================================================================

// UI strings to be localized
var strTitle = localize("$$$/JavaScripts/GN_ExportTextures/Title=GN_ExportTextures");
var strButtonRun = localize("$$$/JavaScripts/GN_ExportTextures/Run=Run");
var strButtonCancel = localize("$$$/JavaScripts/GN_ExportTextures/Cancel=Cancel");
var strHelpText = localize("$$$/JavaScripts/GN_ExportTextures/Help=Specify format + location for exporting each LayerSet as a texture.");
var strLabelDestination = localize("$$$/JavaScripts/GN_ExportTextures/Destination=Export textures to this location:");
var strButtonBrowse = localize("$$$/JavaScripts/GN_ExportTextures/Browse=&Browse...");
var strLabelFileNamePrefix = localize("$$$/JavaScripts/GN_ExportTextures/FileNamePrefix=File Name Prefix:");
var strCheckboxVisibleOnly = localize("$$$/JavaScripts/GN_ExportTextures/VisibleOnly=&Process Visible Layers Only");
var strCheckboxDesaturate = localize("$$$/JavaScripts/GN_ExportTextures/Desaturate=&Desaturate with GN Settings");
var strCheckboxScale50 = localize("$$$/JavaScripts/GN_ExportTextures/Scale50=&Scale Down Texture Size by 50%");
var strLabelFileType = localize("$$$/JavaScripts/GN_ExportTextures/FileType=File Type:");
var strLabelEncoding = localize("$$$/JavaScripts/GN_ExportTextures/Encoding=Encoding:");
var strTargaOptions = localize("$$$/JavaScripts/GN_ExportTextures/TargaOptions=Targa Options:");
var strLabelDepth = localize("$$$/JavaScripts/GN_ExportTextures/Depth=Depth:");
var strRadiobutton16bit = localize("$$$/JavaScripts/GN_ExportTextures/Bit16=16bit");
var strRadiobutton24bit = localize("$$$/JavaScripts/GN_ExportTextures/Bit24=24bit");
var strRadiobutton32bit = localize("$$$/JavaScripts/GN_ExportTextures/Bit32=32bit");
var strBMPOptions = localize("$$$/JavaScripts/GN_ExportTextures/BMPOptions=BMP Options:");
var strAlertSpecifyDestination = localize("$$$/JavaScripts/GN_ExportTextures/SpecifyDestination=Please specify destination.");
var strAlertDestinationNotExist = localize("$$$/JavaScripts/GN_ExportTextures/DestionationDoesNotExist=Destination does not exist.");
var strTitleSelectDestination = localize("$$$/JavaScripts/GN_ExportTextures/SelectDestination=Select Destination");
var strAlertDocumentMustBeOpened = localize("$$$/JavaScripts/GN_ExportTextures/OneDocument=You must have a document open to export!");
var strAlertNeedMultipleLayers = localize("$$$/JavaScripts/GN_ExportTextures/NoLayers=You need a document with multiple layers to export!");
var strAlertWasSuccessful = localize("$$$/JavaScripts/GN_ExportTextures/Success= was successful.");
var strUnexpectedError = localize("$$$/JavaScripts/GN_ExportTextures/Unexpected=Unexpected error");
var strMessage = localize("$$$/JavaScripts/GN_ExportTextures/Message=GN_ExportTextures action settings");
var stretQuality = localize( "$$$/locale_specific/JavaScripts/GN_ExportTextures/ETQualityLength=30" );
var stretDestination = localize( "$$$/locale_specific/JavaScripts/GN_ExportTextures/ETDestinationLength=160" );
var strddFileType = localize( "$$$/locale_specific/JavaScripts/GN_ExportTextures/DDFileType=100" );
var strpnlOptions = localize( "$$$/locale_specific/JavaScripts/GN_ExportTextures/PNLOptions=100" );

// the drop down list indexes for file type
var bmpIndex = 0;
var targaIndex = 1;
//var pdfIndex = 2;
//var psdIndex = 3;
//var jpegIndex = 4;
//var tiffIndex = 5;

// ok and cancel button
var runButtonID = 1;
var cancelButtonID = 2;

main();

function main()
{
    if ( app.documents.length <= 0 )
   {
        if ( DialogModes.NO != app.playbackDisplayDialogs )
      {
            alert( strAlertDocumentMustBeOpened );
        }
       return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
    }

    var exportInfo = new Object();
   
    initExportInfo(exportInfo);
   
    // look for last used params via Photoshop registry, getCustomOptions will throw if none exist
   try {
      var d = app.getCustomOptions("4d633fbb-ed90-480d-8e03-cccb16131a34");
      descriptorToObject(exportInfo, d, postProcessExportInfo);
   }
   catch(e)
   {
      // it's ok if we don't have any options, continue with defaults
   }

   // see if I am getting descriptor parameters
    descriptorToObject(exportInfo, app.playbackParameters, postProcessExportInfo);
   
    if ( DialogModes.ALL == app.playbackDisplayDialogs )
   {
       if (cancelButtonID == settingDialog(exportInfo))
      {
          return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
       }
   }

    try
    {
        var docName = app.activeDocument.name;  // save the app.activeDocument name before duplicate.

        var layerCount = app.documents[docName].layers.length;
        var layerSetsCount = app.documents[docName].layerSets.length;

        if ((layerCount <= 1)&&(layerSetsCount <= 0))
      {
            if ( DialogModes.NO != app.playbackDisplayDialogs )
         {
                alert( strAlertNeedMultipleLayers );
          return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
            }
        }
        else
   {
           
            app.activeDocument = app.documents[docName];
            exportChildrenVisibleOnly(app.activeDocument, exportInfo, exportInfo.fileNamePrefix);
 
         var d = objectToDescriptor(exportInfo, preProcessExportInfo);
         d.putString( app.charIDToTypeID( 'Msge' ), strMessage );
            app.putCustomOptions("4d633fbb-ed90-480d-8e03-cccb16131a34", d);

         var dd = objectToDescriptor(exportInfo, preProcessExportInfo);
         dd.putString( app.charIDToTypeID( 'Msge' ), strMessage );
            app.playbackParameters = dd;
           
            if ( DialogModes.ALL == app.playbackDisplayDialogs )
         {
                alert(strTitle + strAlertWasSuccessful);
            }

            app.playbackDisplayDialogs = DialogModes.ALL;

        }
       
    }
   catch (e)
   {
        if ( DialogModes.NO != app.playbackDisplayDialogs )
      {
            alert(e);
        }
       return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
    }
}

function settingDialog(exportInfo)
{
    dlgMain = new Window("dialog", strTitle);

   dlgMain.orientation = 'column';
   dlgMain.alignChildren = 'left';
   
   // -- top of the dialog, first line
    dlgMain.add("statictext", undefined, strLabelDestination);

   // -- two groups, one for left and one for right ok, cancel
   dlgMain.grpTop = dlgMain.add("group");
   dlgMain.grpTop.orientation = 'row';
   dlgMain.grpTop.alignChildren = 'top';
   dlgMain.grpTop.alignment = 'fill';

   // -- group top left
   dlgMain.grpTopLeft = dlgMain.grpTop.add("group");
   dlgMain.grpTopLeft.orientation = 'column';
   dlgMain.grpTopLeft.alignChildren = 'left';
   dlgMain.grpTopLeft.alignment = 'fill';
   
   // -- the second line in the dialog
   dlgMain.grpSecondLine = dlgMain.grpTopLeft.add("group");
   dlgMain.grpSecondLine.orientation = 'row';
   dlgMain.grpSecondLine.alignChildren = 'center';

    dlgMain.etDestination = dlgMain.grpSecondLine.add("edittext", undefined, exportInfo.destination.toString());
    dlgMain.etDestination.preferredSize.width = StrToIntWithDefault( stretDestination, 160 );

    dlgMain.btnBrowse = dlgMain.grpSecondLine.add("button", undefined, strButtonBrowse);
    dlgMain.btnBrowse.onClick = function()
   {
      var defaultFolder = dlgMain.etDestination.text;
      var testFolder = new Folder(dlgMain.etDestination.text);
      if (!testFolder.exists)
      {
         defaultFolder = "~";
      }
      var selFolder = Folder.selectDialog(strTitleSelectDestination, defaultFolder);
      if ( selFolder != null )
      {
           dlgMain.etDestination.text = selFolder.fsName;
       }
      dlgMain.defaultElement.active = true;
   }

   // -- the third line in the dialog
    dlgMain.grpTopLeft.add("statictext", undefined, strLabelFileNamePrefix);

   // -- the fourth line in the dialog
    dlgMain.etFileNamePrefix = dlgMain.grpTopLeft.add("edittext", undefined, exportInfo.fileNamePrefix.toString());
    dlgMain.etFileNamePrefix.alignment = 'fill';
    dlgMain.etFileNamePrefix.preferredSize.width = StrToIntWithDefault( stretDestination, 160 );

   // -- the fifth line in the dialog
    dlgMain.cbVisible = dlgMain.grpTopLeft.add("checkbox", undefined, strCheckboxVisibleOnly);
    dlgMain.cbVisible.value = exportInfo.visibleOnly;

   // -- the sixth line is the panel
    dlgMain.pnlFileType = dlgMain.grpTopLeft.add("panel", undefined, strLabelFileType);
   dlgMain.pnlFileType.alignment = 'fill';
   
    // -- now a dropdown list
    dlgMain.ddFileType = dlgMain.pnlFileType.add("dropdownlist");
    dlgMain.ddFileType.preferredSize.width = StrToIntWithDefault( strddFileType, 100 );
    dlgMain.ddFileType.alignment = 'left';

    dlgMain.ddFileType.add("item", "BMP");
    dlgMain.ddFileType.add("item", "Targa");

   dlgMain.ddFileType.onChange = function()
   {
      hideAllFileTypePanel();
      switch(this.selection.index)
      {
         case bmpIndex:   
            dlgMain.pnlFileType.pnlOptions.text = strBMPOptions;
            dlgMain.pnlFileType.pnlOptions.grpBMPOptions.show();   
            break;
         case targaIndex:
            dlgMain.pnlFileType.pnlOptions.text = strTargaOptions;
            dlgMain.pnlFileType.pnlOptions.grpTargaOptions.show();   
            break;
      }
   }

   dlgMain.ddFileType.items[bmpIndex].selected = true; //force bmp as default selection for type of file to export
//    dlgMain.ddFileType.items[exportInfo.fileType].selected = true; //use this setting for personal use (contains the registry key on this computer only)

   // -- now the options panel that changes
    dlgMain.pnlFileType.pnlOptions = dlgMain.pnlFileType.add("panel", undefined, "Options");
    dlgMain.pnlFileType.pnlOptions.alignment = 'fill';
    dlgMain.pnlFileType.pnlOptions.orientation = 'stack';
    dlgMain.pnlFileType.pnlOptions.preferredSize.height = StrToIntWithDefault( strpnlOptions, 100 );

   // Targa options
    dlgMain.pnlFileType.pnlOptions.grpTargaOptions = dlgMain.pnlFileType.pnlOptions.add("group");
    dlgMain.pnlFileType.pnlOptions.grpTargaOptions.add("statictext", undefined, strLabelDepth);

    dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb16bit = dlgMain.pnlFileType.pnlOptions.grpTargaOptions.add( "radiobutton", undefined, strRadiobutton16bit);
    dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb24bit = dlgMain.pnlFileType.pnlOptions.grpTargaOptions.add( "radiobutton", undefined, strRadiobutton24bit);
    dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb32bit = dlgMain.pnlFileType.pnlOptions.grpTargaOptions.add( "radiobutton", undefined, strRadiobutton32bit);

    switch (exportInfo.targaDepth)
   {
        case TargaBitsPerPixels.SIXTEEN:     dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb16bit.value = true;   break;
        case TargaBitsPerPixels.TWENTYFOUR:  dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb24bit.value = true;   break;
        case TargaBitsPerPixels.THIRTYTWO:   dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb32bit.value = true;   break;
        default: dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb24bit.value = true;   break;
    }

   // BMP options
    dlgMain.pnlFileType.pnlOptions.grpBMPOptions = dlgMain.pnlFileType.pnlOptions.add("group");
    dlgMain.pnlFileType.pnlOptions.grpBMPOptions.add("statictext", undefined, strLabelDepth);

    dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb16bit = dlgMain.pnlFileType.pnlOptions.grpBMPOptions.add( "radiobutton", undefined, strRadiobutton16bit);
    dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb24bit = dlgMain.pnlFileType.pnlOptions.grpBMPOptions.add( "radiobutton", undefined, strRadiobutton24bit);
    dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb32bit = dlgMain.pnlFileType.pnlOptions.grpBMPOptions.add( "radiobutton", undefined, strRadiobutton32bit);

    switch (exportInfo.bmpDepth)
   {
        case BMPDepthType.SIXTEEN:   dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb16bit.value = true;   break;
        case BMPDepthType.TWENTYFOUR:dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb24bit.value = true;   break;
        case BMPDepthType.THIRTYTWO: dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb32bit.value = true;   break;
        default: dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb24bit.value = true;   break;
    }

   // -- Desaturate?
    dlgMain.cbDesaturate = dlgMain.grpTopLeft.add("checkbox", undefined, strCheckboxDesaturate);
    dlgMain.cbDesaturate.value = exportInfo.Desaturate;

   // -- Scale 50%?
    dlgMain.cbScale50 = dlgMain.grpTopLeft.add("checkbox", undefined, strCheckboxScale50);
    dlgMain.cbScale50.value = exportInfo.cbScale50;
       
   // the ok and cancel buttons
   dlgMain.grpBottom = dlgMain.add("group");
   dlgMain.grpBottom.orientation = 'row';
   dlgMain.grpBottom.alignChildren = 'center';
   dlgMain.grpBottom.alignment = 'fill';   
   
   dlgMain.btnRun = dlgMain.grpBottom.add("button", undefined, strButtonRun );

    dlgMain.btnRun.onClick = function()
   {
      // check if the setting is properly set
      var destination = dlgMain.etDestination.text;
      if (destination.length == 0)
      {
           alert(strAlertSpecifyDestination);
         return;
      }
      var testFolder = new Folder(destination);
      if (!testFolder.exists)
      {
           alert(strAlertDestinationNotExist);
         return;
      }
   
      dlgMain.close(runButtonID);
   }

   dlgMain.btnCancel = dlgMain.grpBottom.add("button", undefined, strButtonCancel );

    dlgMain.btnCancel.onClick = function()
   {
      dlgMain.close(cancelButtonID);
   }

   dlgMain.defaultElement = dlgMain.btnRun;
   dlgMain.cancelElement = dlgMain.btnCancel;

   dlgMain.onShow = function()
   {
      dlgMain.ddFileType.onChange();
   }
   
    // give the hosting app the focus before showing the dialog
    app.bringToFront();

    dlgMain.center();
   
    var result = dlgMain.show();
   
    if (cancelButtonID == result)
   {
      return result;  // close to quit
   }
   
    // get setting from dialog
    exportInfo.destination = dlgMain.etDestination.text;
    exportInfo.fileNamePrefix = dlgMain.etFileNamePrefix.text;
    exportInfo.visibleOnly = dlgMain.cbVisible.value;
    exportInfo.Desaturate = dlgMain.cbDesaturate.value;   
    exportInfo.Scale50 = dlgMain.cbScale50.value;     
    exportInfo.fileType = dlgMain.ddFileType.selection.index;

    if (dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb16bit.value)
   {
      exportInfo.targaDepth = TargaBitsPerPixels.SIXTEEN;
   }
    if (dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb24bit.value)
   {
      exportInfo.targaDepth = TargaBitsPerPixels.TWENTYFOUR;
   }
    if (dlgMain.pnlFileType.pnlOptions.grpTargaOptions.rb32bit.value)
   {
      exportInfo.targaDepth = TargaBitsPerPixels.THIRTYTWO;
   }
    if (dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb16bit.value)
   {
      exportInfo.bmpDepth = BMPDepthType.SIXTEEN;
   }
    if (dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb24bit.value)
   {
      exportInfo.bmpDepth = BMPDepthType.TWENTYFOUR;
   }
    if (dlgMain.pnlFileType.pnlOptions.grpBMPOptions.rb32bit.value)
   {
      exportInfo.bmpDepth = BMPDepthType.THIRTYTWO;
   }

    return result;
}

function hideAllFileTypePanel()
{
    dlgMain.pnlFileType.pnlOptions.grpTargaOptions.hide();
    dlgMain.pnlFileType.pnlOptions.grpBMPOptions.hide();
}

function initExportInfo(exportInfo)
{
    exportInfo.destination = new String("");
    exportInfo.fileNamePrefix = new String("untitled_");
    exportInfo.visibleOnly = false;
    exportInfo.Desaturate = false; 
    exportInfo.Scale50 = false;
    exportInfo.targaDepth = TargaBitsPerPixels.TWENTYFOUR;
    exportInfo.bmpDepth = BMPDepthType.TWENTYFOUR;

    try {
        exportInfo.destination = Folder(app.activeDocument.fullName.parent).fsName; // destination folder
        var tmp = app.activeDocument.fullName.name;
        exportInfo.fileNamePrefix = decodeURI(tmp.substring(0, tmp.indexOf("."))); // filename body part
    } catch(someError)
{
        exportInfo.destination = new String("");
        exportInfo.fileNamePrefix = app.activeDocument.name; // filename body part
    }
}

function saveFile( docRef, fileNameBody, exportInfo)
{
    switch (exportInfo.fileType)
   {
        case targaIndex:
       
       docRef.bitsPerChannel = BitsPerChannelType.EIGHT;
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".tga");
            targaSaveOptions = new TargaSaveOptions();
            targaSaveOptions.resolution = exportInfo.targaDepth;
            docRef.saveAs(saveFile, targaSaveOptions, true, Extension.LOWERCASE);
            break;
           
        case bmpIndex:
       
       docRef.bitsPerChannel = BitsPerChannelType.EIGHT;
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".bmp");
            bmpSaveOptions = new BMPSaveOptions();
            bmpSaveOptions.depth = exportInfo.bmpDepth;
            docRef.saveAs(saveFile, bmpSaveOptions, true, Extension.LOWERCASE);
            break;
           
        default:
            if ( DialogModes.NO != app.playbackDisplayDialogs )
         {
                alert(strUnexpectedError);
            }
            break;
    }
}

function removeAllEmptyLayerSets(obj)
{
    var foundEmpty = true;
    for( var i = obj.layerSets.length-1; 0 <= i; i--)
   {
        if( removeAllEmptyLayerSets(obj.layerSets))
      {
            obj.layerSets.remove();
        } else {
            foundEmpty = false;
        }
    }
    if (obj.artLayers.length > 0)
   {
      foundEmpty = false;
   }
    return foundEmpty;
}

function GN_Scale50()
{
   // =======================================================
   var id3 = charIDToTypeID( "ImgS" );
       var desc2 = new ActionDescriptor();
       var id4 = charIDToTypeID( "Wdth" );
       var id5 = charIDToTypeID( "#Prc" );
       desc2.putUnitDouble( id4, id5, 50.000000 );
       var id6 = charIDToTypeID( "Hght" );
       var id7 = charIDToTypeID( "#Prc" );
       desc2.putUnitDouble( id6, id7, 50.000000 );
       var id8 = charIDToTypeID( "Intr" );
       var id9 = charIDToTypeID( "Intp" );
       var id10 = charIDToTypeID( "Bcbc" );
       desc2.putEnumerated( id8, id9, id10 );
   executeAction( id3, desc2, DialogModes.NO );
   
   // =======================================================
   var id11 = charIDToTypeID( "Shrp" );
   executeAction( id11, undefined, DialogModes.NO );
   activeDocument.flatten();   
}
function GN_Desaturate()
{
         var id13 = charIDToTypeID( "Mk  " );
         var desc4 = new ActionDescriptor();
         var id14 = charIDToTypeID( "null" );
         var ref3 = new ActionReference();
         var id15 = charIDToTypeID( "AdjL" );
         ref3.putClass( id15 );
         desc4.putReference( id14, ref3 );
         var id16 = charIDToTypeID( "Usng" );
         var desc5 = new ActionDescriptor();
         var id17 = charIDToTypeID( "Type" );
         var desc6 = new ActionDescriptor();
         var id18 = charIDToTypeID( "Clrz" );
         desc6.putBoolean( id18, false );
         var id19 = charIDToTypeID( "Adjs" )
         var list2 = new ActionList();
         var desc7 = new ActionDescriptor();
         var id20 = charIDToTypeID( "H   " );
         desc7.putInteger( id20, 0 );
         var id21 = charIDToTypeID( "Strt" );
                     
         desc7.putInteger( id21, -60 );    //desaturate Level value
                     
         var id22 = charIDToTypeID( "Lght" );
         desc7.putInteger( id22, 0 );
         var id23 = charIDToTypeID( "Hst2" );
         list2.putObject( id23, desc7 );
         desc6.putList( id19, list2 );
         var id24 = charIDToTypeID( "HStr" );
         desc5.putObject( id17, id24, desc6 );
         var id25 = charIDToTypeID( "AdjL" );
         desc4.putObject( id16, id25, desc5 );
         
         executeAction( id13, desc4, DialogModes.NO );
      
//Selective Color Change: White-->add Black 100%
      
            var id3 = charIDToTypeID( "Mk  " );
            var desc2 = new ActionDescriptor();
            var id4 = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var id5 = charIDToTypeID( "AdjL" );
            ref1.putClass( id5 );
            desc2.putReference( id4, ref1 );
            var id6 = charIDToTypeID( "Usng" );
            var desc3 = new ActionDescriptor();
            var id7 = charIDToTypeID( "Type" );
            var desc4 = new ActionDescriptor()
            var id8 = charIDToTypeID( "Mthd" );
            var id9 = charIDToTypeID( "CrcM" );
            var id10 = charIDToTypeID( "Rltv" );
            desc4.putEnumerated( id8, id9, id10 );
            var id11 = charIDToTypeID( "ClrC" );
            var list1 = new ActionList();
            var desc5 = new ActionDescriptor();
            var id12 = charIDToTypeID( "Clrs" );
            var id13 = charIDToTypeID( "Clrs" );
            
            //Modifies Whites in Black %
            var id14 = charIDToTypeID( "Whts" );
            desc5.putEnumerated( id12, id13, id14 );
            var id15 = charIDToTypeID( "Blck" );
            var id16 = charIDToTypeID( "#Prc" );
            
            desc5.putUnitDouble( id15, id16, 100.000000 );//Black value
            
            var id17 = charIDToTypeID( "ClrC" );
            list1.putObject( id17, desc5 );
            desc4.putList( id11, list1 );
            var id18 = charIDToTypeID( "SlcC" );
            desc3.putObject( id7, id18, desc4 );
            var id19 = charIDToTypeID( "AdjL" );
            desc2.putObject( id6, id19, desc3 );
            
            executeAction( id3, desc2, DialogModes.NO );
            activeDocument.flatten();
}
            
function exportChildrenVisibleOnly(orgObj, exportInfo, fileNamePrefix)
{
   if (exportInfo.visibleOnly)
   {
         //apply all changes to a duplicate of the original psd file
         var tempDoc = app.activeDocument.duplicate();
         app.activeDocument = tempDoc;
        
         //place hidden layer sets inside an array
         var hiddenLayerSet = [];
             
         for (var i = 0; i < orgObj.layerSets.length; i++)
         {
            var set = tempDoc.layerSets;
            if (set.visible == false)
            {
               hiddenLayerSet.push(set);
            }
         }
   
         while (hiddenLayerSet.length)
         {     
            hiddenLayerSet.pop().remove();
         }
       //in the duplicated doc, remove all the hidden layer sets
      for (var i = tempDoc.layerSets.length - 1; i > 1; i--)
      {
         if(tempDoc.layerSets.visible == false)
         {
            var Temp = tempDoc.layerSets;
            confirm(Temp + "is not visible, so remove");
            tempDoc.layerSets.remove();
         }
         
         }
   
       for( var i = 0; i < tempDoc.layerSets.length; i++)
      {
         for (var j = 0; j < tempDoc.layerSets.length; j++)
         {
      
            tempDoc.layerSets[j].visible = false;
   
              }
      
              tempDoc.layerSets.visible = true;
                
              var duppedDocument = app.activeDocument.duplicate();
         
              var layerSetsName = activeDocument.layerSets.name;  // store layer name before change doc
                   
              activeDocument.flatten();
                   
              //name of the exported texture
              var fileNameBody = fileNamePrefix;
              fileNameBody += "_" + layerSetsName;
              fileNameBody = fileNameBody.replace(/[:\/\\*\?\"\<\>\|]/g, "_"); 
                   
              if (fileNameBody.length > 120)
         {
            fileNameBody = fileNameBody.substring(0,120);
         }
         
         //desaturate the file
         if (exportInfo.Desaturate)
         {   
            GN_Desaturate();
         }

         //desaturate the file
         if (exportInfo.Scale50)
         {   
            GN_Scale50();
         }            
                        
              saveFile(app.activeDocument, fileNameBody, exportInfo);
              activeDocument.close(SaveOptions.DONOTSAVECHANGES);
      
              //return active document to original document
              app.activeDocument = tempDoc;
                
      }
      
      tempDoc.close(SaveOptions.DONOTSAVECHANGES);
       }
       else
       {
       //this will export all textures from all layerSets regardless of wether they are visible or not
    var tempDoc = app.activeDocument.duplicate();
    app.activeDocument = tempDoc;
   
       for( var i = 0; i < tempDoc.layerSets.length; i++)
      {
         for (var j = 0; j < tempDoc.layerSets.length; j++)
         {
      
            tempDoc.layerSets[j].visible = false;
   
              }
      
              tempDoc.layerSets.visible = true;
                
              var duppedDocument = app.activeDocument.duplicate();
         
              var layerSetsName = activeDocument.layerSets.name;  // store layer name before change doc
                   
              activeDocument.flatten();
                   
              //name of the exported texture
              var fileNameBody = fileNamePrefix;
              fileNameBody += "_" + layerSetsName;
              fileNameBody = fileNameBody.replace(/[:\/\\*\?\"\<\>\|]/g, "_"); 
                   
              if (fileNameBody.length > 120)
         {
            fileNameBody = fileNameBody.substring(0,120);
         }


            GN_Desaturate();   

              saveFile(app.activeDocument, fileNameBody, exportInfo);
              activeDocument.close(SaveOptions.DONOTSAVECHANGES);
      
              //return active document to original document
              app.activeDocument = tempDoc;
                
      }
      
      tempDoc.close(SaveOptions.DONOTSAVECHANGES);
       }
}

function objectToDescriptor (o, f)
{
   if (undefined != f)
   {
      o = f(o);
   }
   var d = new ActionDescriptor;
   var l = o.reflect.properties.length;
   for (var i = 0; i < l; i++ )
   {
      var k = o.reflect.properties[i].toString();
      if (k == "__proto__" || k == "__count__" || k == "__class__" || k == "reflect")
         continue;
      var v = o[ k ];
      k = app.stringIDToTypeID(k);
      switch ( typeof(v) ) {
         case "boolean":
            d.putBoolean(k, v);
            break;
         case "string":
            d.putString(k, v);
            break;
         case "number":
            d.putDouble(k, v);
            break;
         default:
            throw( new Error("Unsupported type in objectToDescriptor " + typeof(v) ) );
      }
   }
    return d;
}

function descriptorToObject (o, d, f)
{
   var l = d.count;
   for (var i = 0; i < l; i++ )
   {
      var k = d.getKey(i); // i + 1 ?
      var t = d.getType(k);
      strk = app.typeIDToStringID(k);
      switch (t) {
         case DescValueType.BOOLEANTYPE:
            o[strk] = d.getBoolean(k);
            break;
         case DescValueType.STRINGTYPE:
            o[strk] = d.getString(k);
            break;
         case DescValueType.DOUBLETYPE:
            o[strk] = d.getDouble(k);
            break;
         case DescValueType.INTEGERTYPE:
         case DescValueType.ALIASTYPE:
         case DescValueType.CLASSTYPE:
         case DescValueType.ENUMERATEDTYPE:
         case DescValueType.LISTTYPE:
         case DescValueType.OBJECTTYPE:
         case DescValueType.RAWTYPE:
         case DescValueType.REFERENCETYPE:
         case DescValueType.UNITDOUBLE:
         default:
            throw( new Error("Unsupported type in descriptorToObject " + t ) );
      }
   }
   if (undefined != f)
   {
      o = f(o);
   }
}

function preProcessExportInfo(o)
{
   o.targaDepth = o.targaDepth.toString();
   o.bmpDepth = o.bmpDepth.toString();
   return o;
}

function postProcessExportInfo(o)
{
   o.targaDepth = eval(o.targaDepth);
   o.bmpDepth = eval(o.bmpDepth);
   return o;
}

function StrToIntWithDefault( s, n )
{
    var onlyNumbers = /[^0-9]/g;
    var t = s.replace( onlyNumbers, "" );
   t = parseInt( t );
   if ( ! isNaN( t ) ) {
        n = t;
    }
    return n;
}
// EndGN_ExportTextures.jsx


Im sure I must have forgotten something silly, I just cant seem to pinpoint the problem
general_knox

Error 21: undefined is not an object.

Post by general_knox »

Ok, I think Im getting somewhere...

I tried this, and I can get the UI to open on another computer, but I had to do this...so its opens and works on another computer, BUT...I cant save the last-used options cause it doesnt find the key:

Code: Select all    // look for last used params via Photoshop registry, getCustomOptions will throw if none exist
   try {
      var d = app.getCustomOptions("4d633fbb-ed90-480d-8e03-cccb16131a34");//not a function in CS2
      descriptorToObject(exportInfo, d, postProcessExportInfo);
   }
   catch(e)
   {
      //alert(e);
      // it's ok if we don't have any options, continue with defaults
   }


And:

Code: Select all            app.activeDocument = app.documents[docName];
            exportChildrenVisibleOnly(app.activeDocument, exportInfo, exportInfo.fileNamePrefix);
 
         var d = objectToDescriptor(exportInfo, preProcessExportInfo);
         d.putString( app.charIDToTypeID( 'Msge' ), strMessage );
      try
      {
                 app.putCustomOptions("4d633fbb-ed90-480d-8e03-cccb16131a34", d);//this is not a funtion in CS2
              }
              catch(g)
              {
                 //alert(g);
              }


finally
Code: Select all   try
   {
      dlgMain.ddFileType.items[bmpIndex].selected = true; //force bmp as default selection for type of file to export
//      dlgMain.ddFileType.items[exportInfo.fileType].selected = true;
   }
   catch(h)
   {
         alert(h);
   }


Thats where I am so far in my debugging...

How can I set + save options with putCustomOptions in a registry key for all computers (not just mine)?

Also, the only way to get this script to work in CS2 is to comment out that function cause it looks like it doesnt exist in CS2...what is a similar funtion for CS2 to save the last used options in the UI?
Mike Hale

Error 21: undefined is not an object.

Post by Mike Hale »

general_knox wrote:... How can I set + save options with putCustomOptions in a registry key for all computers (not just mine)?

Also, the only way to get this script to work in CS2 is to comment out that function cause it looks like it doesnt exist in CS2...what is a similar funtion for CS2 to save the last used options in the UI?

CustomOptions only works with CS3 or higher as you have discovered.

The first time the script is run on any computer that has the correct version of Photoshop, the script is not going to find any settings. However unless there is some other error that is stopping the script from saving the options during the first run the next time the script is run it should be able to read the options that where saved.

The options string, in your case "4d633fbb-ed90-480d-8e03-cccb16131a34", needs to be unique to the script. It's not unique to the system and should work on any system( that supports customOptions )

For Photoshop versions that don't support customOptions you need to create functions that read and write the settings to a file. A forum search for ini should give you lots of examples.