3 Replies Latest reply on May 28, 2015 4:16 AM by hhas01

    I cannot tell Applescript to do this block of code

    alexw6348634 Level 1

      This is a python function.

       

      The javascript works fine on it's own, I can run it as a unique script, but this needs to fit into a bunch of other stuff that I've done.

      I'm calling it via osascript, and I have another function that works perfectly fine.

       

      And like I said, this block of code runs perfectly fine on it's own and does exactly what I want. I've tried it with #target illustrator and without, same result.

       

          def _javascriptCreateCMYK(self):

       

       

              js = '''

              var SVGDoc = app.activeDocument;

             

              var newDocPreset = new DocumentPreset;

              newDocPreset.title = ("My Doc");

              newDocPreset.width = (1920);

              newDocPreset.height = (1080);

              newDocPreset.units = RulerUnits.Pixels;

              newDocPreset.colorMode = DocumentColorSpace.CMYK;

             

              var finalDoc = app.documents.addDocument(DocumentColorSpace.CMYK, newDocPreset);

              SVGDoc.activate();

              SVGDoc.selectObjectsOnActiveArtboard();

              app.copy();

              finalDoc.activate();

              app.paste();

              SVGDoc.activate();

              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

              finalDoc.activate();

              '''

              return js

       


      And this is the Applescript that runs via a shell process.


              script = '''

              on run

                  tell application "%s"

                      set aiOpen to "%s" as POSIX file

                      activate

                      open aiOpen

                      activate

                      set js to "%s"

                      do javascript js               

                      set newjs to "%s"

                      do javascript newjs               

                  end tell

              end run''' % (aiVersion, svgFile, js, newjs)

        • 1. Re: I cannot tell Applescript to do this block of code
          hhas01 Level 1

          Don't do string munging code generation - it is evil and wrong, and (as you've discovered) extremely easy to botch. Pass all variable data as arguments to the osascript command. Quick howto here:

           

          http://appscript.sourceforge.net/osascript.html

          • 2. Re: I cannot tell Applescript to do this block of code
            alexw6348634 Level 1

            That's what I'm doing, the only difference being that my function has the code embedded in the python code rather than as an external file, so similar to the last example on the page. And that I don't have arguments, I'm only operating on the current document that is opening.

            • 3. Re: I cannot tell Applescript to do this block of code
              hhas01 Level 1

              No, your Python script is generating AppleScript source code via string interpolation. Use this:

               

              #!/usr/bin/python

              import subprocess


              script = '''
                      on run {appName, filePath, js, newjs}
                          set aiOpen to filePath as POSIX file
                          using terms from application "Adobe Illustrator"
                              tell application appName
                                  open aiOpen
                                  activate
                                  do javascript js
                                  do javascript newjs             
                            end tell
                          end using terms from
                      end run'''

              aiVersion = '...'
              svgFile = '...'
              js = '...'
              newjs = '...'


              p = subprocess.Popen(
                      ['/usr/bin/osascript', '-', aiVersion, svgFile, js, newjs],
                      stdin=subprocess.PIPE, stderr=subprocess.PIPE)

              err = p.communicate(script)[1]

              if p.returncode:
                  print 'ERROR:', err