9 Replies Latest reply on Jan 6, 2017 10:40 AM by Tom Balev

    creating snapshots

    oscar060384

      In javascript, what's the command to add snapshots of a document's current history state? 

        • 1. Re: creating snapshots
          Paul Riggott Level 6
          takeSnapshot();
          function takeSnapshot () { 
             var desc = new ActionDescriptor();
             var sref = new ActionReference();
             sref.putClass(charIDToTypeID("SnpS")); 
             desc.putReference(charIDToTypeID("null"), sref); 
             var fref = new ActionReference(); 
             fref.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH")); 
             desc.putReference(charIDToTypeID("From"), fref ); 
             executeAction(charIDToTypeID("Mk  "), desc, DialogModes.NO ); 
          } 
          function revertToLastSnapshot() { 
             var docRef = app.activeDocument; 
             var hsObj = docRef.historyStates; 
             var hsLength = hsObj.length; 
             for (var i=hsLength - 1;i>-1;i--) { 
               if (hsObj[i].snapshot) { 
                 docRef.activeHistoryState = 
          docRef.historyStates.getByName('Snapshot ' + i); 
                 break; 
               } 
             } 
          }
          
          • 2. Re: creating snapshots
            oscar060384 Level 1

            Is the function revertToLastSnapshot needed since it doesn't seem to be called anywhere?

             

            Also, where can I find the complete set of Event ID Codes?  I was looking at the Photoshop CS4 JavaScript Ref and they don't seem to have all of them listed.

            • 3. Re: creating snapshots
              oscar060384 Level 1

              Is it possible to rewrite the snapshot function without using charIDToTypeIDs everywhere?

               

              I've been looking at some script examples posted on Trevor Morris' site and the code seems so clean and easy to understand.  But when I look at the ScriptListener log, it's filled with ActionDescriptor(), charIDToTypeID(), and ActionReference(). 

              • 4. Re: creating snapshots
                Paul Riggott Level 6

                revertToLastSnapshot is not used in the example, it's there to show how to revert if needed.

                There isn't a complete list of event id's but if you install the ScriptListner plugin you can look at the output and see the event id's that it has generated.

                • 5. Re: creating snapshots
                  xbytor2 Level 4

                  The best reference for type ids is xtools/xlib/PSConstants.js. I generate it from header files in the SDK.

                  http://ps-scripts.cvs.sourceforge.net/viewvc/*checkout*/ps-scripts/xtools/xlib/PSConstants .js

                   

                   

                  The Script Listener log file format is less than optimal. For instance this:

                   

                  var idsetd = charIDToTypeID( "setd" );
                      var desc7 = new ActionDescriptor();
                      var idnull = charIDToTypeID( "null" );
                          var ref4 = new ActionReference();
                          var idPrpr = charIDToTypeID( "Prpr" );
                          var idLefx = charIDToTypeID( "Lefx" );
                          ref4.putProperty( idPrpr, idLefx );
                          var idLyr = charIDToTypeID( "Lyr " );
                          var idOrdn = charIDToTypeID( "Ordn" );
                          var idTrgt = charIDToTypeID( "Trgt" );
                          ref4.putEnumerated( idLyr, idOrdn, idTrgt );
                      desc7.putReference( idnull, ref4 );
                      var idT = charIDToTypeID( "T   " );
                          var desc8 = new ActionDescriptor();
                          var idScl = charIDToTypeID( "Scl " );
                          var idPrc = charIDToTypeID( "#Prc" );
                          desc8.putUnitDouble( idScl, idPrc, 100.000000 );
                          var idDrSh = charIDToTypeID( "DrSh" );
                              var desc9 = new ActionDescriptor();
                              var idenab = charIDToTypeID( "enab" );
                              desc9.putBoolean( idenab, true );
                              var idMd = charIDToTypeID( "Md  " );
                              var idBlnM = charIDToTypeID( "BlnM" );
                              var idMltp = charIDToTypeID( "Mltp" );
                              desc9.putEnumerated( idMd, idBlnM, idMltp );
                              var idClr = charIDToTypeID( "Clr " );
                                  var desc10 = new ActionDescriptor();
                                  var idRd = charIDToTypeID( "Rd  " );
                                  desc10.putDouble( idRd, 0.000000 );
                                  var idGrn = charIDToTypeID( "Grn " );
                                  desc10.putDouble( idGrn, 0.000000 );
                                  var idBl = charIDToTypeID( "Bl  " );
                                  desc10.putDouble( idBl, 0.000000 );
                              var idRGBC = charIDToTypeID( "RGBC" );
                              desc9.putObject( idClr, idRGBC, desc10 );
                              var idOpct = charIDToTypeID( "Opct" );
                              var idPrc = charIDToTypeID( "#Prc" );
                              desc9.putUnitDouble( idOpct, idPrc, 75.000000 );
                              var iduglg = charIDToTypeID( "uglg" );
                              desc9.putBoolean( iduglg, true );
                              var idlagl = charIDToTypeID( "lagl" );
                              var idAng = charIDToTypeID( "#Ang" );
                              desc9.putUnitDouble( idlagl, idAng, 120.000000 );
                              var idDstn = charIDToTypeID( "Dstn" );
                              var idPxl = charIDToTypeID( "#Pxl" );
                              desc9.putUnitDouble( idDstn, idPxl, 5.000000 );
                              var idCkmt = charIDToTypeID( "Ckmt" );
                              var idPxl = charIDToTypeID( "#Pxl" );
                              desc9.putUnitDouble( idCkmt, idPxl, 0.000000 );
                              var idblur = charIDToTypeID( "blur" );
                              var idPxl = charIDToTypeID( "#Pxl" );
                              desc9.putUnitDouble( idblur, idPxl, 5.000000 );
                              var idNose = charIDToTypeID( "Nose" );
                              var idPrc = charIDToTypeID( "#Prc" );
                              desc9.putUnitDouble( idNose, idPrc, 0.000000 );
                              var idAntA = charIDToTypeID( "AntA" );
                              desc9.putBoolean( idAntA, false );
                              var idTrnS = charIDToTypeID( "TrnS" );
                                  var desc11 = new ActionDescriptor();
                                  var idNm = charIDToTypeID( "Nm  " );
                                  desc11.putString( idNm, "Linear" );
                              var idShpC = charIDToTypeID( "ShpC" );
                              desc9.putObject( idTrnS, idShpC, desc11 );
                              var idlayerConceals = stringIDToTypeID( "layerConceals" );
                              desc9.putBoolean( idlayerConceals, true );
                          var idDrSh = charIDToTypeID( "DrSh" );
                          desc8.putObject( idDrSh, idDrSh, desc9 );
                      var idLefx = charIDToTypeID( "Lefx" );
                      desc7.putObject( idT, idLefx, desc8 );
                  executeAction( idsetd, desc7, DialogModes.NO );

                   

                   

                  can be more clearly written as this

                   

                    function cTID(s) { return app.charIDToTypeID(s); };
                    function sTID(s) { return app.stringIDToTypeID(s); };
                  
                      var desc7 = new ActionDescriptor();
                          var ref4 = new ActionReference();
                          ref4.putProperty( cTID('Prpr'), cTID('Lefx') );
                          ref4.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
                      desc7.putReference( cTID('null'), ref4 );
                          var desc8 = new ActionDescriptor();
                          desc8.putUnitDouble( cTID('Scl '), cTID('#Prc'), 100.000000 );
                              var desc9 = new ActionDescriptor();
                              desc9.putBoolean( cTID('enab'), true );
                              desc9.putEnumerated( cTID('Md  '), cTID('BlnM'), cTID('Mltp') );
                                  var desc10 = new ActionDescriptor();
                                  desc10.putDouble( cTID('Rd  '), 0.000000 );
                                  desc10.putDouble( cTID('Grn '), 0.000000 );
                                  desc10.putDouble( cTID('Bl  '), 0.000000 );
                              desc9.putObject( cTID('Clr '), cTID('RGBC'), desc10 );
                              desc9.putUnitDouble( cTID('Opct'), cTID('#Prc'), 75.000000 );
                              desc9.putBoolean( cTID('uglg'), true );
                              desc9.putUnitDouble( cTID('lagl'), cTID('#Ang'), 120.000000 );
                              desc9.putUnitDouble( cTID('Dstn'), cTID('#Pxl'), 5.000000 );
                              desc9.putUnitDouble( cTID('Ckmt'), cTID('#Pxl'), 0.000000 );
                              desc9.putUnitDouble( cTID('blur'), cTID('#Pxl'), 5.000000 );
                              desc9.putUnitDouble( cTID('Nose'), cTID('#Prc'), 0.000000 );
                              desc9.putBoolean( cTID('AntA'), false );
                                  var desc11 = new ActionDescriptor();
                                  desc11.putString( cTID('Nm  '), "Linear" );
                              desc9.putObject( cTID('TrnS'), cTID('ShpC'), desc11 );
                              desc9.putBoolean( sTID('layerConceals'), true );
                          desc8.putObject( cTID('DrSh'), cTID('DrSh'), desc9 );
                      desc7.putObject( cTID('T   '), cTID('Lefx'), desc8 );
                      executeAction( cTID('setd'), desc7, DialogModes.NO );
                  
                  

                   

                   

                  You could also do this if you include PSConstants.js

                      var desc7 = new ActionDescriptor();
                          var ref4 = new ActionReference();
                          ref4.putProperty( PSClass.Property, PSClass.LayerEffects );
                          ref4.putEnumerated( PSClass.Layer, PSType.Ordinal, PSEnum.Target );
                      desc7.putReference( PSString.Null, ref4 );
                          var desc8 = new ActionDescriptor();
                          desc8.putUnitDouble( PSEnum.Scale, PSUnit.Percent, 100.000000 );
                              var desc9 = new ActionDescriptor();
                              desc9.putBoolean( PSKey.Enabled, true );
                              desc9.putEnumerated( PSClass.Mode, PSType.BlendMode, PSEnum.Multiply );
                                  var desc10 = new ActionDescriptor();
                                  desc10.putDouble( PSEnum.Red, 0.000000 );
                                  desc10.putDouble( PSEnum.Green, 0.000000 );
                                  desc10.putDouble( PSEnum.Blue, 0.000000 );
                              desc9.putObject( PSClass.Color, PSClass.RGBColor, desc10 );
                              desc9.putUnitDouble( PSKey.Opacity, PSUnit.Percent, 75.000000 );
                              desc9.putBoolean( PSKey.UseGlobalAngle, true );
                              desc9.putUnitDouble( PSKey.LocalLightingAngle, PSUnit.Angle, 120.000000 );
                              desc9.putUnitDouble( PSKey.Distance, PSUnit.Pixels, 5.000000 );
                              desc9.putUnitDouble( PSKey.ChokeMatte, PSUnit.Pixels, 0.000000 );
                              desc9.putUnitDouble( PSKey.Blur, PSUnit.Pixels, 5.000000 );
                              desc9.putUnitDouble( PSKey.Noise, PSUnit.Percent, 0.000000 );
                              desc9.putBoolean( PSClass.AntiAliasedPICTAcquire, false );
                                  var desc11 = new ActionDescriptor();
                                  desc11.putString( PSKey.Name, "Linear" );
                              desc9.putObject( PSClass.TransparencyStop, PSClass.ShapingCurve, desc11 );
                              desc9.putBoolean( PSString.layerConceals, true );
                          desc8.putObject( PSClass.DropShadow, PSClass.DropShadow, desc9 );
                      desc7.putObject( PSKey.To, PSClass.LayerEffects, desc8 );
                      executeAction( PSEvent.Set, desc7, DialogModes.NO );
                  

                   

                  -X

                  • 6. Re: creating snapshots
                    Michael L Hale Level 5

                    oscar060384 wrote:

                     

                    I've been looking at some script examples posted on Trevor Morris' site and the code seems so clean and easy to understand.  But when I look at the ScriptListener log, it's filled with ActionDescriptor(), charIDToTypeID(), and ActionReference(). 

                    I have not seen the scripts that Trevor's web site, are you saying he has scripts that work with snapshots that don't use scriptlistner code?

                     

                    At any rate here are some more functions( mostly scriptlistner ) that might be helpful when working with snapshots.

                     

                    function createNamedSnapshot(name) {
                        var desc = new ActionDescriptor();
                            var ref = new ActionReference();
                            ref.putClass( charIDToTypeID('SnpS') );
                        desc.putReference( charIDToTypeID('null'), ref );
                            var ref1 = new ActionReference();
                            ref1.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );
                        desc.putReference( cTID('From'), ref1 );
                        desc.putString( charIDToTypeID('Nm  '), name );
                        desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('HstS'), charIDToTypeID('FllD') );
                        executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );
                    }
                    function removeNamedSnapshot(name) {
                        var desc = new ActionDescriptor();
                            var ref = new ActionReference();
                            ref.putName( charIDToTypeID('SnpS'), name );
                        desc.putReference( charIDToTypeID('null'), ref );
                        executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
                    }
                    
                    function revertNamedSnapshot(name) {
                        var desc = new ActionDescriptor();
                            var ref = new ActionReference();
                            ref.putName( charIDToTypeID('SnpS'), name );
                        desc.putReference( charIDToTypeID('null'), ref );
                        executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
                    }
                    app.activeDocument.activeHistoryState = app.activeDocument.historyStates[0];// revert 
                    1 person found this helpful
                    • 7. Re: creating snapshots
                      oscar060384 Level 1

                      Is it possible to write everything without using ScriptListeners? 

                       

                      Or are there certain things that MUST be written with ScriptListeners?

                      • 8. Re: creating snapshots
                        Michael L Hale Level 5

                        There are lots of things in Photoshop that you can only do with scriptlistner. One reason Photoshop ships with that plug-in is Adobe knows that the normal scripting DOM does not cover everything. Scriptlistner helps fill in the gaps.

                        • 9. Re: creating snapshots
                          Tom Balev

                          just for anybody trying to do copypasta, there is a typo on line 8, change

                           

                          desc.putReference( cTID('From'), ref1 );

                          to

                          desc.putReference( charIDToTypeID('From'), ref1 );