通过选择预设和格式进行 Applescript 打印

Applescript print by selecting preset and format

我正在尝试使用 AppleScript 将打印参数定义为预设。 我设法用 acrobat 打开 pdf 文件,打开打印面板并定义份数,但随后我无法“单击”“Stampante...”(图 1)和后续(图 2)按钮。我没有找到对 window 执行的命令是什么。你有什么建议吗

#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

感谢您的回答。我听从了你的建议我很清楚使用问题的命令是 window 打开的名称!

示例

        if not (exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")) then

然后识别 window,现在使用新版本的消息,菜单已更改但有效

这行得通!!! link

我总是得到而且只有

System Events ha trovato un errore: Impossible ottenere window "201.188_001.pdf – 1 pagina" of window "201.188_001.pdf".

我下载了 ui 浏览器并找到了正确的路径,但我总是出错

感谢科拉多

示例 AppleScript 代码,如下所示,在[=132= macOS Monterey 下的 ]Script Editor 以及 System Preferences 中的 Language & Region 设置Italiano — principaleInglese (US) 并且毫无问题地为我工作1.

  • 1 系统偏好设置 > 安全与隐私[=] 中假定必要且适当的设置195=] > 隐私 已根据需要 set/addressed。

假设您是意大利人,并且没有不尊重的意思,我不确定您对我的回答的理解程度如何,也不确定我为什么编写 示例 AppleScript code 以我的方式,但是,我会尝试解释其中的一些内容。

如果您在 Adobe Acrobat Reader DC 中打开了一个 文档 并且调出 打印 对话框,它是一个模态window,因此可以简单地表示为window 1 并且通常不需要一定使用 window "Print" 。这同样适用于例如window "201.188_001.pdf",它可以简单地写成 window 1,但是只打印一个打开的 document document window 不需要与我的 example AppleScript code 相关,因为它是如何编码的。

因为有多个 pop up button 在第二个 |打印| 上被点击window 我将 code 包装在 handler 中,这样它们就可以被 numbernamenumbernamemenu item。请记住,通过 number 菜单分隔符 算作递增的 number

我正在使用 choose file 命令 到 select 目标 PDF 文档Adobe Acrobat Reader DC 中打开和打印。您当然可以选择不使用它,只需将 print thisFile 命令 设置为例如: print file "giungete:users:corrado:desktop:201.188_001.pdf"

我通过示例添加了评论 AppleScript 代码 帮助解释发生了什么。



示例 AppleScript 代码:

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


备注:

如前所述,我选择不单击 打印机...(Stampante...)按钮 因为点击会有大约 5 秒的延迟。我相信延迟是因为第二个 |Print| window 打开并且必须超时,因为 按钮 单击它来自第一个 |Print| window 现在实际上是 window 2,而不是 window 1,这就是为什么 tabbing 避免延迟更有意义。

我还在 macOS Catalina 中测试了 example AppleScript code 打印机... (Stampante...) 按钮[=17 制表位 =194=],而不是 macOS Monterey.

中的 18

请记住,如果您更改第一个 |Print| [=320 中的任何其他设置, 选项卡的数量 将会不同=] 并且它基于您的“图片 1”link,因为您更改的唯一默认值是份数。您始终可以选择单击 Printer... (Stampante...) 按钮 并等待约 5 秒的延迟。它将是 click button 2 而不是 repeat 循环 .

UI 脚本 笨拙的 并且容易失败并且很容易从一个版本的 中断macOS 作为 Appledevelopers 不断改变事物,例如分层UI元素结构。例如,macOS Catalina 中的 17 tabsmacOS 中的 18 tabs Monterey 在这个特定的用例中,第一个 |Print| window in Adobe Acrobat Reader DC.



附加到评论中的地址请求

尽管问题最初集中在 Adobe Acrobat Reader DC,但是,正如 OP 的作者在评论中询问的那样 Preview,这里有一些 example AppleScript code 用于 macOS MontereyPreview.

我认为这将与 Print 的不同 分层 UI 元素结构 形成有趣的对比=]两个应用程序之间的对话框到同一版本的macOS下多么脆弱UI 脚本 可以在两个不同的 应用程序 之间执行相同的基本任务。虽然这可以在 macOS Big SurmacOS Monterey 中与 Preview 一起使用,但它不会在 macOS Catalina作为分层UI元素结构用于打印对话框 略有不同,因为没有 splitter group 1 of sheet 1 of window 1.

示例 AppleScript 代码:

--  # 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


注意:示例 AppleScript code 就是这样,没有任何包含的 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟