2 Replies Latest reply on Apr 26, 2010 5:59 PM by StrongBeaver

    Changes to a Script

    StrongBeaver Level 3

      Hello, I have a script that I'm hoping that someone could help me change a few problem spots with it ?

        • 1. Re: Changes to a Script
          jugenjury Level 2

          It would help if we could see the script and know what the problems are that you are having with it.

          • 2. Re: Changes to a Script
            StrongBeaver Level 3

            It's the last script I need to get sorted out so I can use.  One of the few problems is the script needs to save the LayerGroup based on a Keyboard Shortcut, for example GrapeA LayerSet would be a keyboard shortcut etc, but it must only save based on the LayerGroup name in other words each LayerGroup has it's only Keyboard short-cut and there are (5) LayerGroups that the script knows to only find.

             

            The second problem is the file name must be saved out as <FileName>_<LayerGroupName>.PSD only.

             

             

            //

            main();

            // It calls the other functions to change the visibility of the layer, duplicate it
            // another document in PSD format and save the document.
            function main(){
               
            if(documents.length > 0 && app.activeDocument.activeLayer.typename == "LayerSet") {
            // define the layer names;
            var theArray = ["GrapeA", "GrapeB", "GrapeC", "GrapeD", "GrapeE"];
            var savePath = decodeURI(activeDocument.path);
            var theFileName = app.activeDocument.name;
            // thanks to xbytor for the regexp;
            var baseName = theFileName.match(/(.*)\.[^\.]+$/)[1];
            // work through the array;
            for (var m = 0; m < theArray.length; m++) {
            var layerName = theArray[m];
            // set the active layer to the layer of the name from the array;
            // works only if the layersets are op level in the document;
            app.activeDocument.activeLayer = app.activeDocument.layers.getByName(layerName);
            var theFile = dupLayers();
            var SaveFile= File(savePath+"/"+baseName+"_"+layerName+".psd");
            SavePSD(SaveFile, theFile);
            theFile.close(SaveOptions.DONOTSAVECHANGES);
            }
            else {alert ("please open a document and select a layer group")}
            };

            //changes the view from visible to hidden and vice-versa
            function toggleLayerView(name){
                var actionTaken = charIDToTypeID( "Shw " );
                var actDescription = new ActionDescriptor();
                var id22 = charIDToTypeID( "null" );
                    var actList = new ActionList();
                        var actReference = new ActionReference();
                        var objectType = charIDToTypeID( "Lyr " );
                        actReference.putName( objectType, name );
                    actList.putReference( actReference );
                actDescription.putList( id22, actList );
                var id24 = charIDToTypeID( "TglO" );
                actDescription.putBoolean( id24, true );
            executeAction( actionTaken, actDescription, DialogModes.NO );
            var al = activeDocument.layers.getByName(name);
               if(al.typename == 'LayerSet'){
                   for(i=0;i<al.layers.length;i++){
                            al.layers[i].visible=!al.layers[i].visible;
                       }
                   }

            }

            //copies the layers in a new document
            function dupLayers() {
                    var desc143 = new ActionDescriptor();
                    var ref73 = new ActionReference();
                    ref73.putClass( charIDToTypeID('Dcmn') );
                    desc143.putReference( charIDToTypeID('null'), ref73 );
                    desc143.putString( charIDToTypeID('Nm  '), "_"+activeDocument.activeLayer.name+".psd" );
                    var ref74 = new ActionReference();
                    ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                desc143.putReference( charIDToTypeID('Usng'), ref74 );
                executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
            };


            //selects the layer
            function selectLayerByName(name) {
                    var desc = new ActionDescriptor();
                    var ref = new ActionReference();
                    ref.putName( charIDToTypeID('Lyr '), name );
                   desc.putReference( charIDToTypeID('null'), ref );
                   desc.putBoolean( charIDToTypeID('MkVs'), false );
                   executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
            };

            //saves the file in PSD format
            function SavePSD(saveFile){
                      psdSaveOptions = new PhotoshopSaveOptions();
                      psdSaveOptions.embedColorProfile = true;
                     psdSaveOptions.alphaChannels = true;
                     activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
            }