卡在 AppleScript 中以在预览中向 PDF 添加密码

Stuck on AppleScript to add password to PDF in Preview

尝试在 AppleScript 中为 PDF 添加密码。下面的代码通过

   click menu item 23 -- Export Menu item

但在下一行失败并出现“无效索引”错误“无法获取 window 1...”

...然后猜测我的复选框的下一行最后需要类似的东西。

提前致谢

蒂姆

set myFolder to path to desktop as string
set myFile to myFolder & "test.pdf"

tell application "Preview"
    open myFile
    activate
end tell

tell application "System Events"
    tell process "Preview"
        tell menu bar 1
            tell menu bar item "File"
                tell menu "File"
                    click menu item 23 -- Export Menu item
                    click button "Permissions..."  of sheet 1 of window 1
                    click checkbox "Require Password To Open Document"
                    text field 1
                    keystroke "1234"
                    text field 2
                    keystroke "1234"
                    text field 3
                    keystroke "1234"
                    text field 4
                    keystroke "1234"
                    keystroke return
                    keystroke return
                end tell
            end tell
        end tell
    end tell
end tell

示例 AppleScript 代码,如下所示,在Script EditormacOS MontereyLanguage & Region 设置在 System Preferences 设置为 英语(美国)— 主要 并为我工作没有问题1.

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

example AppleScript code 被编码来检查 [] 使用键盘导航在控件之间移动焦点 复选框系统偏好设置 > 上选中Keyboard > Shortcuts 选项卡,因为这会影响使用 tab 键 以编程方式导航各种对话框的方式。它被编码为然后从一种方式分支到另一种方式来处理 checkbox.

的状态

这是我将如何编码的示例:

示例 AppleScript 代码:

property myPassword : "1234"

set myFolder to path to desktop as string
set myFile to myFolder & "test.pdf"

--  # Get the value of AppleKeyboardUIMode to determine if the
--  # 'Use keyboard navigation to move focus between controls'
--  # checkbox is checked on the System Preferences >  
--  # Keyboard > Shortcuts tab.

set keyboardNavigation to my checkAppleKeyboardUIMode()

--  # Open the target file in Preview.          

tell application "Preview"
    open myFile
    activate
end tell

--  # Use System Events to export the document 
--  # and navigate the ensuing dialog boxes. 

tell application "System Events"
    tell application process "Preview"
        
        tell its menu "File" of ¬
            menu bar item "File" of menu bar 1 to ¬
            click (its first menu item whose name is "Export…")
        
        repeat until exists button "Permissions…" of sheet 1 of window 1
            delay 0.1
        end repeat
        
        tell its sheet 1 of window 1 to click button "Permissions…"
        
        repeat until exists ¬
            checkbox "Require Password To Open Document" of ¬
            sheet 1 of sheet 1 of window 1
            delay 0.1
        end repeat
        
        tell its sheet 1 of sheet 1 of window 1
            
            click checkbox "Require Password To Open Document"
            delay 0.1
            
            if keyboardNavigation = 0 then
                --  # Use keyboard navigation to move focus
                --  # between controls is not checked.
                key code 48 -- # tab key
                delay 0.1
                key code 48 -- # tab key
                delay 0.1
                keystroke myPassword
                delay 0.1
                key code 48 -- # tab key
                delay 0.1
                keystroke myPassword
                delay 0.1
                key code 48 -- # tab key
                delay 0.1
                keystroke myPassword
                delay 0.1
                key code 48 -- # tab key
                delay 0.1
                keystroke myPassword
            else
                --  # Use keyboard navigation to move
                --  # focus between controls is checked.            
                key code 48 -- # tab key                
                delay 0.1
                keystroke myPassword
                delay 0.1
                key code 48 -- # tab key                
                delay 0.1
                keystroke myPassword
                delay 0.1
                repeat 6 times
                    key code 48 -- # tab key
                    delay 0.1
                end repeat
                keystroke myPassword
                delay 0.1
                key code 48 -- # tab key                
                delay 0.1
                keystroke myPassword
            end if
            
            delay 0.1
            click button "Apply"
            delay 0.1
            
        end tell
        
        click button "Save" of sheet 1 of window 1
        delay 0.1
        if exists button "Replace" of sheet 1 of sheet 1 of window 1 then ¬
            click button "Replace" of sheet 1 of sheet 1 of window 1
        delay 0.1
        
        click button 1 of window 1 -- # Close the document.
        
    end tell
end tell



--  ## Handler ##

on checkAppleKeyboardUIMode()
    
    --  # Get the fully qualified POSIX pathname of the target .plist file.
    
    set thePropertyListFilePath to ¬
        the POSIX path of ¬
            (path to preferences from user domain as string) & ¬
        ".GlobalPreferences.plist"
    
    --  # Get the value of AppleKeyboardUIMode to determine if the
    --  # 'Use keyboard navigation to move focus between controls'
    --  # checkbox is checked on the System Preferences >  
    --  # Keyboard > Shortcuts tab.
    
    try
        tell application "System Events" to ¬
            tell the property list file thePropertyListFilePath to ¬
                return the value of ¬
                    the property list item "AppleKeyboardUIMode"
    on error
        return 0
    end try
    
end checkAppleKeyboardUIMode

备注:

问题的 代码 中的一个问题是使用三个 . 而不是省略号 在:click button "Permissions..."

此外,剩余的 代码 不会执行,因为它包含在 tell menu "File" 语句 中。 example AppleScript code 着重于 events 与其在问题中的编码方式相比发生。

请注意,UI 脚本 最多 笨拙 ,并且由于 [=40= UI 元素 结构 OS 和/或 应用程序 正在编写脚本和/或时间问题,其中可能需要使用 delay 命令 和/或调整其

举个例子,我最初对此进行编码是为了避免使用 tabbingkeystroking,但是它有时会产生混合结果而不是其他时候,以及为什么我记录下来做那些事情。 (UI 脚本 呃!)

此外,由于导出过程中的第一个对话框没有更改文档[的名称 或保存的 位置 ,受密码保护的文档最终可能位于与打开时不同的 位置 。修改该可能的行为需要额外的编码。



注意:示例 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,适当设置延迟