Corrado Polentes
Corrado Polentes

Reputation: 13

Applescript print by selecting preset and format

I am trying to define the print parameters as presets with AppleScript. I managed to open the pdf file with acrobat, open the print panel and define the number of copies, but then I was unable to "click" on the "Stampante..." (image 1) and subsequent (image 2) buttons. I do not find what are the commands to act on the window. Do you have any suggestions

#apro file con acrobat
#definire posizione la prima volta del programma
tell application "Adobe Acrobat Reader DC"
    activate application
    #apro file creare percorso per file su temp 
    open file "giungete:users:corrado:desktop:201.188_001.pdf"
    #uso pulsanti per eseguire comando
    tell application "System Events"
        tell window "201.188_001.pdf"
            #apro stampa
            keystroke "p" using command down
            delay 1
            #vado copie
            keystroke tab
            delay 0.1
            keystroke tab
            delay 0.1
            keystroke tab
            delay 0.1
            #definisco copie
            keystroke "120"
            delay 0.1
            keystroke tab
            delay 0.1   
            #do invio
            #keystroke return
        end tell
    end tell
end tell

image 1

image 2

Upvotes: 1

Views: 955

Answers (1)

user3439894
user3439894

Reputation: 7555

The example AppleScript code, shown below, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to Italiano — principale and Inglese (US) and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Assuming you are Italian, and with no disrespect intended, I am not sure how well you will understand my answer or why I coded the example AppleScript code in the manner I have, however, I will try to explain some of it.

If you have a document opened in Adobe Acrobat Reader DC and you bring up the Print dialog box, it is a modal window and as such can simply be addressed as window 1 and no need to necessarily use window "Print" in general. The same goes for e.g. window "201.188_001.pdf", it can simply be addressed as window 1, but to just print an opened document the document window does not need to be relevant in my example AppleScript code because of how it is coded.

As there are multiple pop up button being clicked on the second |Print| window I wrapped the code in a handler so they can be clicked by number or name and number or name of the menu item. Keep in mind that by number the menu separators count as an incremented number.

I am using a choose file command to select the target PDF document to open and print in Adobe Acrobat Reader DC. You can of course choose not to use it and just set the print thisFile command to e.g.: print file "giungete:users:corrado:desktop:201.188_001.pdf"

I have added comments through the example AppleScript code to help explain what is happening.



Example AppleScript code:

property |Copies| : "120"
property |Print| : "Stampa"
property |Show Details| : "Mostra dettagli"

set thisFile to ¬
    choose file of type "com.adobe.pdf" with prompt ¬
        "Choose a PDF document:" default location ¬
        (path to documents folder) without invisibles

tell application id "com.adobe.Reader"
    activate
    ignoring application responses
        print thisFile
    end ignoring
end tell

tell application id "com.apple.systemevents"
    tell window 1 of application process "AdobeReader"
        
        --  ## First |Print| window. ##
        
        repeat until its name is |Print|
            delay 0.01
        end repeat
        
        --  # Set the number of copies to print.
        
        set value of first text field of group 1 to |Copies|
        
        --  # Clicking the |Printer...| button  causes a
        --  # ~5 second delay, therefore tab to it instead.
        --  # Tested in macOS Monterey and it took 18 tabs
        --  # from the default opening of the |Print| window,
        --  # however, it needs to be checked manually in
        --  # other versions of macOS first.                
        
        repeat 18 times
            key code 48 --  # tab key
            delay 0.01
        end repeat
        key code 36 --  # enter key
        
        --  # Wait a moment for the second |Print| window.
        
        delay 1
        
        --  ## Second |Print| window. ##
        
        --  # Make sure second |Print| window is expanded.
        
        if (the name of its button 2) is |Show Details| then
            click button 2
            delay 1
        end if
        
        --  # Click and select on Preset:
        
        my clickPopUpButtonMenuItem(2, 1)
        
        delay 0.25
        
        --  # Click and select on Layout:
        
        my clickPopUpButtonMenuItem(3, 1)
        
        delay 0.25
        
        -- # Click the Print button on the second |Print| window.
        
        click button 4
        
        delay 0.25
        
        --  ## First |Print| window. ##
        
        --  # Click the Print button on the first |Print| window.
        --  # Leave commented until testing is finished.
        
        --click button 4
        
    end tell
end tell


--  # Handler #

on clickPopUpButtonMenuItem(PopUpButton, MenuItem)
    --  # PopUpButton - Can be either an integer or the name.
    --  # MenuItem - Can be either an integer or the name.
    --  # MenuItem - Menu separators count as an incremented number.
    tell application id "com.apple.systemevents"
        tell window 1 of application process "AdobeReader"
            click pop up button PopUpButton
            click menu item MenuItem of menu 1 of pop up button PopUpButton
        end tell
    end tell
end clickPopUpButtonMenuItem


Notes:

As previously mentioned I chose not to click the Printer... (Stampante...) button because of the ~5 second delay if clicked. I believe the delay is caused because the second |Print| window opens and it has to time out because the button clicked it is from the first |Print| window which is now actually window 2, not window 1, and this is why tabbing makes more sense to avoid the delay.

I also tested the example AppleScript code in macOS Catalina and it was 17 tab stops to the Printer... (Stampante...) button, not 18 like in macOS Monterey.

Keep in mind that the number of tabs will be different if you change any of the other setting in the first |Print| window and it was based on your "image 1" link in that the only default you changed was the number of copies. You can always choose to click the Printer... (Stampante...) button instead and wait the ~5 second delay. It would be click button 2 instead of the repeat loop.

UI Scripting is kludgy and prone to failure and can break easily from one version of macOS to another as Apple and developers continually changes things, e.g. the hierarchical UI element structure. Case in point, 17 tabs in macOS Catalina vs. 18 tabs in macOS Monterey in this particular use case, the first |Print| window in Adobe Acrobat Reader DC.



Add-on to address request in comment

Even though the question originally focused on Adobe Acrobat Reader DC, nonetheless, as the author of the OP asked in comments about doing it in Preview, here is some example AppleScript code for use in macOS Monterey and Preview.

I thought it would be an interesting contrast to the different hierarchical UI element structure of the Print dialog box between the two applications to under the same version of macOS and how fragile UI Scripting can be preforming the same basic task between two different applications. While this would work with Preview in macOS Big Sur and macOS Monterey, it will not in macOS Catalina as the hierarchical UI element structure for the Print dialog box differs slightly, as there is no splitter group 1 of sheet 1 of window 1.

Example AppleScript code:

--  # Example AppleScript code for use in macOS Monterey and Preview.

property |Copies| : "120"
property |Print| : "Stampa"
property |Show Details| : "Mostra dettagli"

set thisFile to ¬
    choose file of type "com.adobe.pdf" with prompt ¬
        "Choose a PDF document:" default location ¬
        (path to documents folder) without invisibles

tell application id "com.apple.Preview"
    activate
    open thisFile
end tell

tell application id "com.apple.systemevents"
    -- launch --    # Used in an attemp to workaround a bug.
    
    --  # Used to see the full large print dialog if Preview opens
    --  # the document too tight on the left side of the screen.
    
    tell window 1 of application process "Preview"
        set currentPosition to its position
        if item 1 of currentPosition is less than 70 then ¬
            set its position to {70, item 2 of currentPosition}
    end tell
    
    --  # Bring up the Print dialog box.
    
    keystroke "p" using command down
    
    --  # Wait until the Print dialog box is ready.
    
    repeat until exists button 1 of ¬
        splitter group 1 of sheet 1 of window 1 of ¬
        application process "Preview"
    end repeat
    
    tell splitter group 1 of sheet 1 of window 1 of application process "Preview"
        
        --  # Show Details if necessary. (Mostra dettagli)
        
        if exists button |Show Details| then
            click button |Show Details|
            delay 1
        end if
        
        --  # Printer: pop up button. (Stampante:)
        
        click pop up button 1
        click menu item 1 of menu 1 of pop up button 1
        
        --  # Preset: pop up button.
        
        click pop up button 2
        click menu item 1 of menu 1 of pop up button 2
        
        --  # Copies: text field. (Copie:)
        
        set value of text field 1 to |Copies|
        
        --  # Paper Size: pop up button. (Dimensioni pagina:)
        
        click pop up button 3
        click menu item 5 of menu 1 of pop up button 3
        click menu item 2 of menu 1 of menu item 5 of menu 1 of pop up button 3
        
        --  # Preview pop up button (By default.) (Anteprima)
        
        click pop up button 4
        click menu item 1 of menu 1 of pop up button 4
        
        delay 1
        
        --  # The following will vary based on menu selection of  pop up button 4.
        
        tell group 3
            click pop up button 1
            click menu item 1 of menu 1 of pop up button 1
            -- click pop up button 2
            -- click menu item 1 of menu 1 of pop up button 2
            -- click pop up button 3
            -- click menu item 1 of menu 1 of pop up button 3
        end tell
        
        --  # Print button. (Stampa)
        
        -- click button 4 --    # Or click by name.
        click button |Print|
        
    end tell
end tell


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Upvotes: 1

Related Questions