17 Replies Latest reply on Mar 4, 2010 3:43 AM by Muppet Mark

    Clear Slices

    jaguar 0006 Level 1

      Hello,

      How to call the "Clear Slices" function of the View menu in an AppleScript?

      Thomas

        • 1. Re: Clear Slices
          Michael L Hale Level 5

          You will have to use Applescript's do javascript to run the javascript below.

           

              var desc = new ActionDescriptor();
                  var ref = new ActionReference();
                  ref.putEnumerated( stringIDToTypeID('slice'), charIDToTypeID('Ordn'), charIDToTypeID('Al  ') );
              desc.putReference( charIDToTypeID('null'), ref );
              executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
          
          1 person found this helpful
          • 2. Re: Clear Slices
            jaguar 0006 Level 1

            Hello Michael,

             

            Thanks for your answer!

             

            I have never used Applescript's do javascript to run javascript and I don't know how to do this.

             

            I found a way to do this in an other discussion (How to run ps cs4 js script from applescript) :

             

             

            set JavaScriptClearSlices to "myClearSlices();

             

            function myClearSlices() {

             

            var desc = new ActionDescriptor();

            var ref = new ActionReference();

            ref.putEnumerated( stringIDToTypeID('slice'), charIDToTypeID('Ordn'), charIDToTypeID('Al  ') );

            desc.putReference( charIDToTypeID('null'), ref );

            executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );

             

             

            }"


            tell application "Adobe Photoshop CS4"

            activate

            set Doc_Ref to the current document

            tell Doc_Ref

            do javascript JavaScriptClearSlices ¬

             

            show debugger on runtime error

            display dialog the result

            end tell

            end tell

             

             

            Is this the good method?

            • 3. Re: Clear Slices
              Muppet Mark Level 5

              Michael's scriptlistener function requires no arguments you code looks fine to me. 'show debugger on runtime error' will launch and bring ExtendToolkit to the foremost app if an error occurs. It should also highlight where in the Javascript code the error took place.

              1 person found this helpful
              • 4. Re: Clear Slices
                jaguar 0006 Level 1

                Hello,

                 

                Thanks Muppet for your answer.

                 

                I tried this:

                 

                 

                set JavaScriptClearSlices to "myClearSlices();

                 

                function myClearSlices() {

                var desc = new ActionDescriptor();

                var ref = new ActionReference();

                ref.putEnumerated( stringIDToTypeID('slice'), charIDToTypeID('Ordn'), charIDToTypeID('Al  ') );

                desc.putReference( charIDToTypeID('null'), ref );

                executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );

                }"

                 

                 

                tell current document

                do javascript JavaScriptClearSlices

                end tell

                 

                 

                 

                And I obtain:

                error-ps.jpg

                • 5. Re: Clear Slices
                  Muppet Mark Level 5

                  Sorry didn't test just looked at you AppleScript code. Tried the posted code and failed for me too at the line:

                   

                  executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );

                   

                  Tried re-recording the same thing with the same result.

                  • 6. Re: Clear Slices
                    Michael L Hale Level 5

                    I will let Mark help with the Applescript part as I don't use it, but I don't see anything wrong with the javascript part. The javascript should run without error even if the document doesn't have slices. However it will throw an error if there is no documents open.

                    • 7. Re: Clear Slices
                      Muppet Mark Level 5

                      Mike, slices are NOT something I've come across as most of my work is print related. I have now tried both your posted code and also recorded some new with the use of x's toolkit. Both of these error at the same point as that kicked back by AppleScript. Here is what I quickly put together…

                       

                      #target photoshop

                       

                      app.bringToFront();

                       

                      var docRef = app.activeDocument;

                       

                      with (docRef) {

                      myClearSlices();

                      //ftn1();

                      }

                       

                      // Mikes

                      function myClearSlices() {

                      var desc = new ActionDescriptor();

                      var ref = new ActionReference();

                      ref.putEnumerated( stringIDToTypeID('slice'), charIDToTypeID('Ordn'), charIDToTypeID('Al  ') );

                      desc.putReference( charIDToTypeID('null'), ref );

                      executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );

                      }

                       

                      // X's tool kit

                      function ftn1() {

                      function cTID(s) { return app.charIDToTypeID(s); };

                      function sTID(s) { return app.stringIDToTypeID(s); };

                      var desc21 = new ActionDescriptor();

                      var ref15 = new ActionReference();

                      ref15.putEnumerated( sTID('slice'), cTID('Ordn'), cTID('Al  ') );

                      desc21.putReference( cTID('null'), ref15 );

                      executeAction( cTID('Dlt '), desc21, DialogModes.NO );

                      };

                       

                      Both kick back

                       

                      General Photoshop error occurred.\n- The command “Delete” is not currently available.

                       

                      To ESTK's console? So I don't think its AppleScript

                      • 8. Re: Clear Slices
                        jaguar 0006 Level 1

                        Michael,

                        There is an open document. I tried with slices and without.

                         

                        In your Javascript code, there's some spaces (one or two) before quotes. For example:

                        charIDToTypeID('Dlt ') or charIDToTypeID('Al  ').

                        Is that correct?

                        • 9. Re: Clear Slices
                          Muppet Mark Level 5

                          Just to check I've even tried to record an action and have script play this out with NO go.

                           

                          "The command "Delete" is not currently available"

                          • 10. Re: Clear Slices
                            Michael L Hale Level 5

                            CharID need to have 4 chars, so they are sometimes padded with spaces so both of those are correct.

                            • 11. Re: Clear Slices
                              Michael L Hale Level 5

                              Mark, are you saying that clear slices doesn't work as an action for you? Or the script doesn't work played back in an action?

                               

                              I was going to ask you to run the javascript in ESTK so we will know if it's the javascript or the Applescript interface.

                              • 12. Re: Clear Slices
                                Muppet Mark Level 5

                                Mike, I can neither play back a recorded action via the GUI pallet or use Applescripts do action "X" from "Z" both create and app dialog about not being available.

                                • 13. Re: Clear Slices
                                  Muppet Mark Level 5

                                  Strangely enough it works fine using system events GUI scripting but thats a very low level way about the problem.

                                   

                                  tell application "Adobe Photoshop CS2"

                                  activate

                                  if exists current document then

                                  my Clear_Slices()

                                  end if

                                  end tell

                                   

                                  on Clear_Slices()

                                  tell application "System Events"

                                  tell application process "Adobe Photoshop CS2"

                                  tell menu bar 1

                                  tell menu bar item "View"

                                  tell menu 1

                                  click menu item "Clear Slices"

                                  end tell

                                  end tell

                                  end tell

                                  end tell

                                  end tell

                                  end Clear_Slices

                                  • 14. Re: Clear Slices
                                    Michael L Hale Level 5

                                    Strange, that sounds like a Photoshop/Mac bug

                                    • 15. Re: Clear Slices
                                      Muppet Mark Level 5

                                      Thats what I would suspect too…

                                      • 16. Re: Clear Slices
                                        jaguar 0006 Level 1

                                        Thanks Mark and Michael!

                                        Mark, a very low level way about the problem, but it works fine !

                                         

                                        I had to add this in my script to prevent a "Access for assistive devices is disabled" error:

                                         

                                        tell application "System Events"

                                           set UI elements enabled to true

                                        end tell

                                         

                                        Thomas

                                        • 17. Re: Clear Slices
                                          Muppet Mark Level 5

                                          Ooops forgot about that its been some time since I had to call on GUI scripting… Funny is that I gave this up in favor of scriptlistener with this app… Then you go find what is most likely a mac bug (oh well when all else fails)…