17 Replies Latest reply on Sep 24, 2010 11:52 AM by Anatoly Paraev

    Document::saveAs doesn't work in Photoshop

    Anatoly Paraev Level 1

      Hello,

       

      I am having a problem trying to save the current document in Photoshop to the PNG format.

      I am trying to use the Document class from the com.adobe.photoshop package.

       

      The code that I have looks like this:

       

      public function saveAsPNG(filePath:String):Boolean

      {

            var saveOpts:PNGSaveOptions = new PNGSaveOptions;

            saveOpts.interlaced = false;

            var file:File = new File(filePath);

            var currentDoc:Document = Photoshop.app.activeDocument;

            currentDoc.saveAs(file,saveOpts,true);

            return true;

      }

       

      I call this function in response to a button click in the UI. However when the execution gets to , I get the following error:

      Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

       

      This error looks like the one that occurs when I try to access Photoshop DOM from a system event handler (as discussed here), so I tried to apply the workaround recommended for system events in my function:

      public function saveAsPNG(filePath:String):Boolean

      {

            var obj: * = PsEventScrubber.getInstance().forceClean( this, saveAsPNG, arguments );

            if ( obj != null )

            {

                  return (obj as Boolean);

       

            }

            var saveOpts:PNGSaveOptions = new PNGSaveOptions;

            saveOpts.interlaced = false;

            var file:File = new File(filePath);

            var currentDoc:Document = Photoshop.app.activeDocument;

            currentDoc.saveAs(file,saveOpts,true);

            return true;

      }

       

      Now I get an exception, and I can see that the logger is called with the following message:

      [RequestResult after parsing result="[RequestResult status="PlugPlugRequestFailed" data=null]" ]

       

      I also tried to use ActionDescriptors to do the same job, but I got the same problems as with the CSAW API. The code for the ActionDescriptor approach is below:

       

      public function saveAsPNG1(filePath:String):Boolean

      {

            var obj: * = PsEventScrubber.getInstance().forceClean( this, saveAsPNG1, arguments );

            if ( obj != null )

           {

                return (obj as Boolean);

           }

       

           var key_in:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("In  ");

           var key_png_interlaced:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("PGIT");

           var enum_png_interlaced_none:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("PGIN");

           var key_png_filter:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("PNGf");

           var enum_png_filter_adaptive:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("PGAd");

           var key_copy:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("Cpy ");

           var key_extn:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("Extn");

           var key_as:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("As  ");

           var class_png_format:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("PNGF");

           var event_save:int = com.adobe.csawlib.photoshop.Photoshop.app.charIDToTypeID("save");

       

          

           var result:ActionDescriptor = null;

           var desc:ActionDescriptor = new ActionDescriptor;

           var PNGDescriptor:ActionDescriptor = new ActionDescriptor;

       

       

           var file:File = new File(filePath);

           desc.putPath(key_in,file);

           desc.putBoolean(key_copy,true);

           desc.putBoolean(key_extn,true);

       

           PNGDescriptor.putEnumerated( key_png_interlaced, key_png_interlaced, enum_png_interlaced_none);

           PNGDescriptor.putEnumerated(key_png_filter, key_png_filter, enum_png_filter_adaptive);

       

           desc.putObject(key_as,class_png_format,PNGDescriptor);

           result = com.adobe.csawlib.photoshop.Photoshop.app.executeAction(event_save,desc);

       

           return true;

      }

       

      With this approach the error occurs then the execution gets to the line

      ...

      desc.putPath(key_in,file);

      ...

       

      Am I doing something wrong here?

      Has anyone tried to use the Document::saveAs API in Photoshop?

       

      Thank you in advance,

      Anatoly