23 Replies Latest reply on Jan 12, 2009 2:59 PM by (KevinStauffer)

    Is there a way to trigger batch process via Applescript or Javascript?

      Based on what I've found so far on the internet, the answer is no. The Applescripts I've found so far that attempt to run a batch process don't work for me in either Acrobat Pro 8 or Pro 9.

      I've been using Applescript to automate a process that starts in InDesign (create PDFs), then goes to Acrobat to set open options (I've got a batch process for that but can't find a way to trigger it). If I can get that to work, I'll attempt to automate the task of using a Photoshop droplet to create JPEGs of a specific size from these PDFs.

      I've settled on InDesign CS3 for creating single-page PDFs from a multiple-page document, partly because I could not find a scriptable way to do this in Acrobat. I know just enough about Applescript to be dangerous. I know much less about Javascript.

      Any help would be appreciated.

      Thanks,
      Kevin Stauffer
        • 1. Re: Is there a way to trigger batch process via Applescript or Javascript?
          Patrick Leckey Level 3
          There is no way to programatically trigger a batch process in Acrobat.

          > I've settled on InDesign CS3 for creating single-page PDFs from a multiple-page document, partly because I could not find a scriptable way to do this in Acrobat.

          Doing this can be easily scripted from Acrobat.

          Perhaps if you gave us a description of your workflow we could better help you.
          • 2. Re: Is there a way to trigger batch process via Applescript or Javascript?
            Level 1
            PDL,

            Here's the workflow I'm trying to automate:

            1) Create PDFs from InDesign. One needs to be the entire document, but then we also need single pages, one PDF per page. The Applescript I'm using (basically a slightly tweaked script that was part of a collection of InDesign scripts I located via this forum) calls for an export preset we have for InDesign. Our file naming structure calls for two-digit page numbers, and my current Applescript pads numbers below 10 (e.g., 01, 02).

            2) Setting open options for all PDFs (Page only, single page layout, magnification = fit page ).

            3) Creating JPEG images of each page. These JPEGs end up on the Web. We have used a Photoshop Action/Droplet that resizes, adjusts levels, sharpens, etc.

            That's the basics. We also need to combine PDF "spreads" (e.g., take page 04 and combine it with page 05 in one PDF file, setting the page layout to "continuous facing" rather than "single page"). The location of spreads is random (04 and 05 won't always be a spread), so I was envisioning either manual work or a separate script for this step.

            The files also need to be created in or moved to specific folders, but the Applescripting for that should be somewhat less daunting, I imagine.

            Again, thanks for any help.
            • 3. Re: Is there a way to trigger batch process via Applescript or Javascript?
              Patrick Leckey Level 3
              I don't work with InDesign so I can't comment on scripting for that, but 2 and 3 can be done with JavaScript inside Acrobat.

              2) Doc.layout = "SinglePage"; and Doc.zoom = zoomtype.fitP; Set these as an open action on the first page.

              3) The Doc.saveAs method support JPG, and will generate 1 JPG per page of a document by default.

              How to do this with Applescript is for someone else to answer. Applescript is not my forté.
              • 4. Re: Is there a way to trigger batch process via Applescript or Javascript?
                try67 MVP & Adobe Community Professional
                Making single-pages PDFs from a multiple-pages one is quite simple in JavaScript. You make a loop over the pages you want to extract and execute the extractPages method of the Document object on each of them.
                If you only need to do this on a single PDF then there's no need for scripting at all. Simply go to Document -> Extract Pages, and click the option to extract each page as a separate file.
                • 5. Re: Is there a way to trigger batch process via Applescript or Javascript?
                  Level 1
                  Im not a big user of ID but am sure there are scripts about for outputting single page PDF's. That a side Im presuming you are utilizing the PDF's for something else as the initial view will have no effect on Photoshop's opening of the file to rasterize it. You can also have Photoshop on multipage PDF's you have Acrobat open the file first store a page count to a variable for Photoshop to use in a loop or Spotlight could do the same using the number of pages in the PDF's metadata. Batch functions in Acrobat can be got at by using system events but it gets a bit messy and I try avoid that. BTW fit page can also be set directly via AppleScript too. You can't use an action in Photoshop to deal with multipage PDF's because of the page count variable. Everything your action does can be done with Applescript too
                  • 6. Re: Is there a way to trigger batch process via Applescript or Javascript?
                    Level 1
                    Thanks for all of the responses. In general, it would be great to have some sample scripts to refer to. Applescript is preferred, but if Javascripts do the job better I need help implementing them within Applescript.

                    PDL mentions Javascript ways to set Open Options. I need help implementing those in Applescript. I was guessing that I could use "do script," but my attempts thus far aren't working. I know nothing of Javascript.

                    try67 notes that you can make single pages from a multipage PDF via Javascript. That would be great, but considering my total lack of knowledge in Javascript, I am likely to stick with my current Applescript, which creates single-page PDFs and names/numbers them in the way called for in our workflow.

                    Mark is correct in that the PDFs are used elsewhere; in our workflow, they also give us the single-page files we need to convert to JPEGs of those single pages (which are then displayed on the Web). I'd love to see an example of how to use a Photoshop action within Applescript.

                    Thanks all!
                    Kevin
                    • 7. Re: Is there a way to trigger batch process via Applescript or Javascript?
                      Level 1
                      Kevin some thing like this for Photoshop should aid you

                      set Todays_Date to do shell script "date \"+%d-%m-%y\""
                      -- Create new folder to save to
                      tell application "Finder"
                      set Raster_Images to make new folder at desktop with properties ¬
                      {name:"Rasterized PDF's " & Todays_Date}
                      end tell
                      -- Set Photoshop settings
                      tell application "Adobe Photoshop CS2"
                      activate
                      set display dialogs to never
                      set User_Rulers to ruler units of settings
                      set ruler units of settings to pixel units
                      -- set background color to {class:CMYK color, cyan:0, magenta:0, yellow:0, black:0}
                      -- set foreground color to {class:CMYK color, cyan:0, magenta:0, yellow:0, black:100}
                      end tell
                      -- Get list of PDF's
                      set The_Question to "Do you want to include all the subfolders" & return & "within your folder selection?"
                      set The_Dialog to display dialog The_Question buttons {"No", "Yes"} default button 1 with icon note
                      if button returned of The_Dialog is "Yes" then
                      set Input_Folder to choose folder with prompt "Where is the top level folder of PFD's?" without invisibles
                      tell application "Finder"
                      set File_List to (files of entire contents of Input_Folder whose name extension is "pdf")
                      end tell
                      else
                      tell application "Finder"
                      set Input_Folder to choose folder with prompt "Where is the folder of PFD's?" without invisibles
                      set File_List to (files of Input_Folder whose name extension is "pdf")
                      end tell
                      end if
                      set File_Count to count of File_List
                      if File_Count = 0 then
                      display dialog "This folder contains no PDF files to rasterize!" giving up after 2
                      end if
                      -- Loop through the files in list
                      repeat with This_File in File_List
                      tell application "Finder"
                      set The_File to This_File as alias
                      end tell
                      -- Tiger (OSX.4) shell call to count the pages
                      set Page_Count to my PDF_Pages(POSIX path of The_File)
                      if the result is not false then
                      -- Loop Photoshop through the page count
                      repeat with i from 1 to Page_Count
                      tell application "Adobe Photoshop CS2"
                      activate
                      open The_File as PDF with options ¬
                      {class:PDF open options, bits per channel:eight, constrain proportions:true, crop page:trim box, mode:CMYK, page:i, resolution:300, suppress warnings:true, use antialias:true, use page number:true}
                      set Doc_Ref to the current document
                      tell Doc_Ref
                      flatten
                      -- Enter your name strings into two enties below
                      -- Case sensitive stings
                      -- do action "My Action" from "My Action Set"
                      -- New file naming options
                      set Doc_Name to name
                      set ASTID to AppleScript's text item delimiters
                      set AppleScript's text item delimiters to " "
                      set Doc_Name to text items of Doc_Name
                      set AppleScript's text item delimiters to "_"
                      set Doc_Name to Doc_Name as string
                      set AppleScript's text item delimiters to "-"
                      set Doc_Name to text item 1 of Doc_Name
                      set AppleScript's text item delimiters to ASTID
                      if Page_Count = 1 then
                      set New_File_Name to (Raster_Images as string) & Doc_Name & ".tif"
                      else
                      set File_Number to ""
                      repeat until (length of (File_Number as text)) = (length of (Page_Count as text))
                      if File_Number = "" then
                      set File_Number to i
                      else
                      set File_Number to "0" & File_Number
                      end if
                      end repeat
                      set New_File_Name to (Raster_Images as string) & Doc_Name & "_" & File_Number & ".tif"
                      end if
                      end tell
                      save Doc_Ref in file New_File_Name as TIFF with options {byte order:Mac OS, embed color profile:false, image compression:LZW, save alpha channels:false, save layers:false}
                      close current document without saving
                      end tell
                      end repeat
                      end if
                      end repeat
                      -- Set ruler units back to user prefs
                      tell application "Adobe Photoshop CS2"
                      set ruler units of settings to User_Rulers
                      end tell
                      --
                      beep 3
                      -- OSX Tiger shell handler
                      on PDF_Pages(This_PDF)
                      try
                      do shell script "/usr/bin/mdls -name kMDItemNumberOfPages" & space & quoted form of This_PDF & " | /usr/bin/grep -o '[0-9]\\+$'"
                      on error Error_Message number Error_Number
                      if Error_Number is 1 then
                      display alert "Page Count Unavailable" message "The page count for " & This_PDF & " is unavailable." giving up after 3
                      return false
                      else
                      display alert "Error " & Error_Number message Error_Message giving up after 3
                      return false
                      end if
                      end try
                      end PDF_Pages

                      and something like this to perform JavaScript from within AppleScript for Acrobat
                      You would be better talking to the JavaScript Experts on how to use addScript() to get your doc level scripts in.

                      property Default_Path : (path to desktop folder as Unicode text) as alias
                      property JavaScript : "var re = /.*\\/|\\.pdf$/ig; var filename = this.path.replace(re,''); try { for (var i = 0; i < this.numPages; i++) this.extractPages( { nStart: i, cPath: filename+'_' + (i+1) +'.pdf' }); } catch (e) { console.println('Aborted: '+e) }" as text
                      --
                      set The_PDF to choose file default location Default_Path ¬
                      with prompt "Where is the multi-page PDF?" without invisibles
                      --
                      tell application "Adobe Acrobat 7.0 Professional"
                      activate
                      open The_PDF
                      tell active doc
                      do script JavaScript
                      close saving yes
                      end tell
                      end tell
                      • 8. Re: Is there a way to trigger batch process via Applescript or Javascript?
                        Level 1
                        Mark, your code is a gold mine. It has so much of what I've been looking for. I'm looking forward to working with it, combining it with what I had thus far and hopefully coming up with a workflow automation solution that will make our designers happy.

                        Thanks again Mark and everyone else. I'm probably not done posting, but I definitely believe that you've put me on the right track.

                        Kevin
                        • 9. Re: Is there a way to trigger batch process via Applescript or Javascript?
                          Level 1
                          I don't know how much you know about AppleScript but just in case. Your Action in Photoshop is in the line:

                          -- do action "My Action" from "My Action Set"

                          This is commented out as this won't exist at your end and will cause error. To make this work for you remove the two hyphens at the beginning of the line and replace "My Action" with the name string of your action ditto for "My Action Set" the name string of the set its in. If you are to distribute this the action will also need to be distributed too. I would be in favor of changing this and using AppleScript or JavaScript code to perform your actions command steps. Hope this helps, if you need any more help about Photoshop post in the correct forum won't be upsetting anybody that way
                          • 10. Re: Is there a way to trigger batch process via Applescript or Javascript?
                            Level 1
                            In response to triggering a batch process in Acrobat via AppleScript, it's possible using an Event based script to achieve the desired result. However, there are focus issues involved and if something goes awry like the Apple Software Update dialog popping up then the script will fail.

                            Sanctus
                            • 11. Re: Is there a way to trigger batch process via Applescript or Javascript?
                              Level 1
                              Mark, thanks for the follow-up. I did catch where the action reference was in your script, tweaked our existing action to eliminate the "open" and "close" parts of it (we have been using it as a droplet) and it works very well. We typically install that action on all designer's Macs, but I hope to put the code in the Applescript itself to eliminate the need for distributing the action and droplet.

                              My goal is to take the various scripts I have now and put them together in one single script that creates a complete "final PDF" along with single-page PDFs and JPEGs of those pages. The code you wrote is a huge part of that, so again, thanks.

                              My biggest remaining question is how to have the script set the open options for those PDFs.

                              Sanctus, I've experimented with event-based scripts and encountered problems along the same lines as you've mentioned. Thanks, though.

                              Kevin
                              • 12. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                Level 1
                                Thanks to the help of Mark Larsen and others, I now have an Applescript that automates one of the stages of our company's workflow. Starting with an InDesign CS3 document, the script creates a complete PDF (all pages), then creates single-page PDF files and, finally, JPEGs of those single pages. The script also creates job folders in two different locations and names those folders in accordance with our workflow, and moves the created files to those locations as appropriate.

                                To make this script complete, I need a way to script the process of setting the open options for the created PDF files (page only, single-page layout, magnification set to fit page). I'd love to keep it all in Applescript but that doesn't look likely. I could rely on a "do script" and use Javascript, but I know nothing of JS.

                                Once again, any help to put the finishing touches on my Applescript would be greatly appreciated.

                                Kevin

                                P.S. Just to clarify, my script works only because I've "recycled" (stolen, pilfered, nicked, etc.) the work of Mark and others, and tweaked it to fit my specific needs.
                                • 13. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                  Level 1
                                  Kevin, do you mind resorting to a system events GUI driven AppleScript?
                                  • 14. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                    Level 1
                                    Mark, I'd certainly give it a shot if you've got any ideas. I have tried a bit of (recycled) scripting starting with something like:

                                    <<tell application "Acrobat" /
                                    tell menu bar 1 /
                                    tell menu "Advanced" of menu bar item "Advanced" /
                                    click menu item "Batch Processing..." /
                                    tell application "System Events" /
                                    tell window "Batch Sequences" of process "Acrobat" /
                                    tell button -5 to perform action "YourProcess" />>

                                    But I haven't had much luck. Acrobat would skip ahead to the next window of batch processing rather than actually run the process.

                                    Thanks as always,
                                    Kevin
                                    • 15. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                      Level 1
                                      Kevin, this is not for the batch process but to call and change the doc info window close and save. You could wrap this in a sub routine with your choice of settings. Then put the handler call inside a loop thru a doc list. Should give you the idea on how to click check boxes and some other stuff.

                                      tell application "Adobe Acrobat 7.0 Professional"
                                      activate
                                      tell active doc
                                      tell application "System Events"
                                      tell process "Acrobat"
                                      try
                                      -- If the PDF document has menubar hidden or open full screen this won't work
                                      -- click menu item "Document Properties..." of menu 1 of menu bar item "File" of menu bar 1
                                      -- so we do this instead
                                      keystroke "d" using command down
                                      delay 2
                                      -- target the "Initial View" tab
                                      tell tab group 1 of group 1 of window 1
                                      click radio button "Initial View"
                                      delay 1
                                      -- User Interface Options
                                      tell group 1 of group 1 of group 1 of group 1 of group 1
                                      set a to get value of checkbox 3 -- "Hide menu bar"
                                      if a = 0 then click checkbox 3
                                      set b to get value of checkbox 2 -- "Hide tool bars"
                                      if b = 0 then click checkbox 2
                                      set c to get value of checkbox 1 -- "Hide window controls"
                                      if c = 0 then click checkbox 1
                                      end tell
                                      -- Window Options
                                      tell group 1 of group 2 of group 1 of group 1 of group 1
                                      set d to get value of checkbox 3 -- "Resize window to initial page"
                                      if d = 0 then click checkbox 3
                                      set e to get value of checkbox 2 -- "Center window on screen"
                                      if e = 0 then click checkbox 2
                                      -- This needs to be off to be able to set "Page layout"
                                      set f to get value of checkbox 1 --"Open in Full Screen mode"
                                      if f ≠ 0 then click checkbox 1
                                      end tell
                                      -- Document Options
                                      tell group 1 of group 3 of group 1 of group 1 of group 1
                                      -- Show Pages panel and Page
                                      set g to get value of pop up button 2
                                      if g = "Page Only" then
                                      click pop up button 2
                                      keystroke (ASCII character 31) -- Down arrow
                                      keystroke (ASCII character 31) -- Down arrow
                                      keystroke return
                                      else if g = "Bookmarks Panel and Page" then
                                      click pop up button 2
                                      keystroke (ASCII character 31) -- Down arrow
                                      keystroke return
                                      else if g = "Attachments Panel and Page" then
                                      click pop up button 2
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke return
                                      else if g = "Layers Panel and Page" then
                                      click pop up button 2
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke return
                                      end if
                                      delay 1
                                      -- Page layout Single Page
                                      set h to get value of pop up button 1
                                      if h = "Default" then
                                      click pop up button 1
                                      keystroke (ASCII character 31) -- Down arrow
                                      keystroke return
                                      else if h = "Continuous" then
                                      click pop up button 1
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke return
                                      else if h = "Facing" then
                                      click pop up button 1
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke return
                                      else if h = "Continuous - Facing" then
                                      click pop up button 1
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke (ASCII character 30) -- Up arrow
                                      keystroke return
                                      end if
                                      tell combo box 1
                                      -- Magnification Fit Page
                                      click button 1
                                      tell group 1
                                      keystroke tab
                                      keystroke tab
                                      keystroke tab
                                      keystroke "Fit Page"
                                      end tell
                                      end tell
                                      end tell
                                      end tell
                                      -- OK these settings
                                      click button 1 of group 1 of window 1
                                      end try
                                      end tell
                                      end tell
                                      close saving yes
                                      end tell
                                      end tell
                                      • 16. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                        Level 1
                                        Mark, your "doc properties" Applescript seems like a good idea, but it doesn't work in Acrobat 8 or Acrobat 9 (the two versions I'm writing for; we're moving everyone to 9 after the first of the year).

                                        Specifically, Acrobat 9 stops after opening the Document Properties window and moving to the Initial View screen, with no parameters changed. Acrobat 8 stops at the same point, except 8 eventually crashes.

                                        Any ideas? My script is basically just missing this step. I've put in a step that opens all the files in Acrobat and asks the user to set the open options manually, but it would be great if the script could complete the entire process.

                                        Thanks once again,
                                        Kevin
                                        • 17. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                          Level 1
                                          Kevin, for doing GUI based scripting (always a last resort if needs must) I use a utility "UI Browser" that lets me inspect all the UI elements. The problem with this is it can be very specific when it comes to app versions. The only way for me to check would be to install v8 and inspect what has changed in the way that this dialog window is constructed. It could then be different again for v9. The principle of how to check boxes and scroll listings should all be there it will just be one of the groups or tabs but Im not in a position to help you with that. I get a new mac next week (for at home) which I will probably put v8 on. Can't do this at work as I need all macs to be running the same versions of all apps.
                                          • 18. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                            Level 1
                                            I have completed an Applescript that creates a complete PDF file from an InDesign file, creates single-page PDF files from that same InDesign file and then creates small JPEG files from those single-page PDF files. Along the way, the script creates folders for these files and sets the open options for the PDF files.

                                            Mark (or anyone else): If you'd like me to send you the script let me know (I'm assuming that e-mailing the script itself would be the best). The script is specific to our company's particular scenario (lots of specific file-naming conventions, etc.) but still might make sense. The script is also rather long-winded and there are likely better ways to do it.

                                            try67: You wrote that Javascripting the extraction of pages from a PDF would be fairly straightforward (<<You make a loop over the pages you want to extract and execute the extractPages method of the Document object on each of them.>>) Would you mind sending an example? I know next to nothing about Javascript, but am still thinking my Applescript would work better if I had a "do script" step and used Javascript to extract (and rename) the pages.

                                            Thanks everyone!
                                            Kevin
                                            • 19. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                              try67 MVP & Adobe Community Professional
                                              Hi Kevin,<br /><br />This script will extract all pages from a PDF to the same folder:<br /><br />var re = /\.pdf$/i;<br />var filename = this.documentFileName.replace(re,"");<br />for (i=0; i<this.numPages; i++) {<br />     this.extractPages({nStart: i, cPath: filename+"_Page" + (i+1) + ".pdf"});<br />}
                                              • 20. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                                Level 1
                                                try67, thanks for the Javascript. However, I cannot figure out how to run this from inside Applescript using "do script." I've tried setting the code to a variable (set myScript to "the code") but all attempts have failed. I need the JS code to be contained within the Applescript itself; referring to a text file with the code would be problematic (at best) in our workflow.

                                                It would be great if you could provide some insight/examples re. how to get this Javascript to run within Applescript. For what it may be worth, I'm using Acrobat 9.

                                                Thanks once more,
                                                Kevin
                                                • 21. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                                  try67 MVP & Adobe Community Professional
                                                  Sorry, I don't use AppleScript and can't help you there.
                                                  • 22. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                                    Level 1
                                                    Kevin, here is an example of using JavaScript from within AppleScript. Unfortunately Im not anywhere near good enough with the JavaScript to get this working exactly how I would like.
                                                    ----------------
                                                    property Default_Path : (path to desktop folder as Unicode text) as alias
                                                    property JavaScript : "var re = /.*\\/|\\.pdf$/ig; var filename = this.path.replace(re,''); try { for (var i = 0; i < this.numPages; i++) this.extractPages( { nStart: i, cPath: filename+'_' + (i+1) +'.pdf' }); } catch (e) { console.println('Aborted: '+e) }" as text
                                                    --
                                                    set The_PDF to choose file default location Default_Path ¬
                                                    with prompt "Where is the multi-page PDF?" without invisibles
                                                    --
                                                    tell application "Adobe Acrobat 7.0 Professional"
                                                    activate
                                                    open The_PDF
                                                    tell active doc
                                                    do script JavaScript
                                                    close saving no
                                                    end tell
                                                    end tell
                                                    • 23. Re: Is there a way to trigger batch process via Applescript or Javascript?
                                                      Level 1
                                                      Mark and try67, thanks! Mark, the "Javascript in Applescript" example you provided has helped make my AppleScript much more efficient (at least from what I can tell so far). The script runs faster and helps avoid some Acrobat crashes that were occurring.

                                                      Unfortunately, from what I can tell after looking through this forum, the AcroJSGuide and the JS API Reference, I can't use Javascript to set the initial view of the single-page PDF files that are being extracted. The JS layout props include "this.layout = SinglePage" and the magnification I'm after can be set via "this.zoomtype = zoomtype.fitP," but these apparently only affect the view of the open document and can't be saved as Initial View properties.

                                                      So, yes, all your much-appreciated help just makes me ask for more help. If the Initial View properties can't be set via Javascript, I'm back to GUI Applescript for that part of it. Not the end of the world.

                                                      I had hoped that extracting pages from a PDF in which the initial view had been set to "Single Page" would create PDF files with the "Single Page" initial view, but instead the extracted pages revert to "Default" settings for Page Layout and Magnification.

                                                      Thanks,
                                                      Kevin