Apple Script 不再 运行 Big Sur

Apple Script no longer running with Big Sur

我有这个脚本来更改打印纸尺寸并保存受密码保护的文档。在软件更新到 BigSur 之前,它运行良好 问题似乎出在“单击弹出按钮 3”这一行。即使打开具有我要单击的按钮的正确 window,它也无法识别该按钮。你能帮我弄清楚为什么它不能识别弹出按钮吗?

set FixedPassword to "nocopy"

set VarPassword to "fibonacci"

set outpath to "/Users/E1S0.pdf"

set SaveFolder to "/Users/Exam1"

set FileName to "E1S0" & " " & "(Password" & " - " & VarPassword & ").pdf"


tell application "Preview"
    activate
    open outpath
end tell


activate application "Preview"
tell application "System Events"
    tell process "Preview"
        keystroke "p" using command down
        delay 0.5
        tell front window
            repeat until exists sheet 1
                delay 10
            end repeat
            tell sheet 1
                click pop up button 3
                click menu item "My Customized Size" of menu 1 of pop up button 3
                click menu button "PDF"
                repeat until exists menu 1 of menu button "PDF"
                    delay 0.02
                end repeat
                click menu item "Save as PDF" of menu 1 of menu button "PDF"
                
            end tell
        end tell
        
        -- Make sure the save dialog is visible
        repeat until exists sheet 1 of sheet 1 of front window
            delay 0.2
        end repeat
        
        tell sheet 1 of sheet 1 of front window
            click button "Security Options..."
        end tell
        
        tell window "PDF Security Options"
            set selected to true
            set focused to true
            (* click the checkbox to on *)
            -- NOTE: for some reason there is a delay of about 6 seconds here, I do not know why
            tell checkbox "Require password to open document"
                click
            end tell
            (* add the password and confirm *)
            keystroke VarPassword
            keystroke (ASCII character 9)
            keystroke VarPassword
            
            tell its checkbox "Require password to copy text, images and other content"
                click
            end tell
            (* add the password and confirm *)
            keystroke FixedPassword
            keystroke (ASCII character 9)
            keystroke FixedPassword
            
            click button "OK"
        end tell
        
        repeat until exists sheet 1 of sheet 1 of front window
            delay 0.2
        end repeat
        
        -- Press command+shift+g to show the "Go" drop down sheet
        keystroke "g" using {command down, shift down}
        repeat until exists sheet of sheet 1 of sheet 1 of front window
            delay 0.2
        end repeat
        
        delay 0.5
        keystroke SaveFolder
        delay 0.5
        
        click button "Go" of sheet of sheet 1 of sheet 1 of front window
        
        -- Now that we are in our desired folder, set the file name and save
        set value of text field 1 of sheet 1 of sheet 1 of front window to FileName
        
        click button "Save" of sheet 1 of sheet 1 of front window
        
    end tell
end tell

AppleScript codemacOS 的主要版本之间被破坏的情况并不常见。

如果您尝试单击按钮恰好是纸张大小:,默认情况下不会显示在目标 sheet 上,然后它的 层次结构 在例如macOS CatalinamacOS Big Sur.

macOS Catalina 中,其 层次结构 是:

AXApplication > AXWindow:AXStandardWindow > AXSheet > AXPopUpButton

或者:

tell application "System Events" to ¬
    click pop up button 3 of ¬
        sheet 1 of ¬
        window 1 of ¬
        application process "Preview"

macOS Big Sur 中,其 层次结构 是:

AXApplication > AXWindow:AXStandardWindow > AXSheet > AXSplitGroup > AXPopUpButton

或者:

tell application "System Events" to ¬
    click pop up button 3 of ¬
        splitter group 1 of ¬
        sheet 1 of ¬
        window 1 of ¬
        application process "Preview"

也就是说,如前所述,默认情况下该按钮不会显示在目标 sheet 上,您应该包括 code 检查 sheet 是否已展开以显示详细信息,如果没有,请单击 显示详细信息 按钮第一。