8 Replies Latest reply on Jul 8, 2013 1:13 AM by Ghoulfool

    Snap with scripting

    Ghoulfool Level 1

      Is it possible to switch the snap (VIew --> Snap) on and off using scripting? Something along the lines of

       

       

      app.activeDocument.snap = true;
      

       

      Cheers

        • 1. Re: Snap with scripting
          Michael L Hale Level 5

          Here is how you can toggle snapping

           

          app.runMenuItem(stringIDToTypeID('snap'));

           

          Not sure how you could determine the current state.

          • 2. Re: Snap with scripting
            Ghoulfool Level 1

            Unfortunately in CS2, the following gives us the 8800 error "The <object unknown> is not currently available"

             

            function runMenuItem(item)
            { 
               cTID = function(s)
               {
                  return app.charIDToTypeID(s);
               }
               var desc = new ActionDescriptor(); 
               var ref = new ActionReference(); 
               ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( item ) ); 
               desc.putReference( cTID( "null" ), ref ); 
               executeAction( cTID( "slct" ), desc, DialogModes.NO ); 
            }
            
            runMenuItem("snap")
            

             

            even though

            runMenuItem("ActP"); //actual size 100%
            

            works fine.

            • 3. Re: Snap with scripting
              Michael L Hale Level 5

              Can you use the flyout menu in the actions panel to insert a menu item for snap? If so I can tell you how to get the code from the action.

              • 4. Re: Snap with scripting
                Ghoulfool Level 1

                Yes, I was a ble to record an action using insert "menu item". The script listener came back with:

                var id1466 = charIDToTypeID( "slct" );
                    var desc338 = new ActionDescriptor();
                    var id1467 = charIDToTypeID( "null" );
                        var ref245 = new ActionReference();
                        var id1468 = charIDToTypeID( "Mn  " );
                        var id1469 = charIDToTypeID( "MnIt" );
                        var id1470 = stringIDToTypeID( "snap" );
                        ref245.putEnumerated( id1468, id1469, id1470 );
                    desc338.putReference( id1467, ref245 );
                executeAction( id1466, desc338, DialogModes.NO );
                
                • 5. Re: Snap with scripting
                  Michael L Hale Level 5

                  Sorry, I should have caught that you were using charIDToTypeID. It's a good bet if the string starts with a lowercase letter it is a stringID even if it is 4 chars.

                   

                  I assume that the last code you posted works with CS2. If so you could change that runMenuItem to something like this so it will work with both char and string IDs.

                   

                  function runMenuItem(item)
                  {
                     cTID = function(s)
                     {
                        return app.charIDToTypeID(s);
                     }
                     var desc = new ActionDescriptor();
                     var ref = new ActionReference();
                     ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), item  );
                     desc.putReference( cTID( "null" ), ref );
                     executeAction( cTID( "slct" ), desc, DialogModes.NO );
                  }

                  runMenuItem(stringIDToTypeID("snap"));

                  • 6. Re: Snap with scripting
                    Ghoulfool Level 1

                    Mike, that's brilliant; I've got what I wanted now an alert that returns the state of the snap.

                    (Which was getting to be a pain working with many layers and blindly hoping that they would snap to either non-existent guides or other layers) Oh, I got around the currentstate problem by writing out a text file.

                    • 7. Re: Snap with scripting
                      Michael L Hale Level 5

                      Would you share how you are getting the current state of the snap toggle? I couldn't find it in the app descriptor.

                       

                      Or are you just assuming a starting state and keeping track of the number of toggles?

                      • 8. Re: Snap with scripting
                        Ghoulfool Level 1

                        Nothing so fancy; I'm working on the assumption that the state is off. and it'll get switched on in which case the mini text file which could be "true", "false" or "undefined" gets set.