0 Replies Latest reply on May 6, 2011 2:47 PM by DBarranca

    PS suspendHistory() workaround

    DBarranca Level 4

      Hello,

       

      CS SDK 1.5 are out, and while I really like them a huge lot, the Photoshop document's method suspendHistory() in ActionScript is still bugged... sigh!

      Waiting for an official fix (please, please ), I've come up with a (maybe obvious) workaround, which involves HostObject (as described by Zak Nelson in this CookBook entry) and a little tweak. Basically I bounce from AS3 to JSX and back to AS3 writing my own suspendHistory function.

      If someone else is having troubles as I've had, this is possibly a viable solution to adopt.

      Any suggestion to make it better: obviously welcome.

       

      In a Controller.as class I embed a Javascript file

       

      [ Embed (source="jsx/Bounce.jsx" , mimeType=  "application/octet-stream" )]
      private static var myScriptClass:Class;
      

       

      Then I declare my own SuspendHistory function:

       

       

      public function SuspendHistory(title:String, myFunction:String):void
      {
         var  jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);
         jsxInterface.eval( new myScriptClass ().toString());
         jsxInterface.init( this );
         jsxInterface.Bounce(title,myFunction);
      }
      

       

       

      where title is the only History tag that will appear in the history states list, and myFunction is... the function that I'd like to call.

       

      On the JSX side, I've a Bounce.jsx file

       

      var asInterface = {};
      
      function init(wrapper) 
      {
         asInterface = wrapper;
      } 
      
      function Bounce(title, myFunction) 
      {
         var foo = "asInterface." + myFunction;
         app.activeDocument.suspendHistory (title, foo)
      }
      
      

       

      The Bounce function takes the very same two parameters and bounces them back to ActionScript, calling the requested function wrapped inside a (natural working) ExtendScript suspendHistory call.

       

      Using it in the AS3 class is straightforward, and you can even forget that there's some JSX action behind the scenes:

       

      public function run():void
      {
         SuspendHistory("Stuff Happening", "myASFunction()");
      }
      public function myASFunction():void 
      {
         app.activeDocument.activeLayer.duplicate(); 
         app.activeDocument.activeLayer.duplicate();  // really fancy stuff...
      }
      

       

       

      The nice part is that the function called from within suspendHistory can have parameters as well (obviously, uh)

       

      public function run():void
      {
         var loops:uint = 5
         SuspendHistory("Stuff Happening", "myASFunction("+loops+")");
      }
      
      public function myASFunction(loops:uint)
      {
         for (var i:uint = 0; i<loops; i++)
         {
            app.activeDocument.activeLayer.duplicate(); 
         }
      }
      

       

      even though to split with quotes the function code to add the parameter isn't that elegant... but as long as it works, I'm happy - and it's better than nothing.

      Cheers,

       

      Davide

       

      Nevertheless a fix in the CSAW libraries would be better