卡在AppleScript上,无法在预览中为PDF添加密码



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

click menu item 23 -- Export Menu item

但是在下一行上失败;无效索引";错误";无法获取窗口1…";

然后猜测我的下一行复选框末尾需要类似的内容。

提前感谢

Tim

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代码是在macOSMonterey下的脚本编辑器中用语言测试的;系统首选项中的区域设置设置为英语(美国(-主要,适用于我,没有问题1

  • 1 系统首选项>安全性&隐私>隐私已根据需要设置/解决

示例AppleScript代码进行编码,以检查[]使用键盘导航在控件之间移动焦点复选框是否在系统首选项>键盘>快捷键选项卡,因为这将影响如何使用选项卡键以编程方式导航各个对话框。它被编码为从一种方式分支到另一种方式,以处理复选框的状态。

以下是我如何编码的示例:

示例 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"语句中。本文提供的示例AppleScript代码与问题中的编码方式相比,集中在事件发生的适当位置。

请注意,UI脚本充其量是kludgy,并且由于OS层次结构UI元素的更改和/或正在编写脚本的application以及可能需要使用delay命令和/或调整其时的时间问题,很容易出现故障。

举个例子,我最初对其进行编码是为了避免使用选项卡键控,但它的结果喜忧参半,因为它有时有效,有时无效,以及为什么我录制来做这些事情。(UI脚本Ugh!(

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



注意:示例AppleScript代码仅此而已,在不包含任何错误处理的情况下,不包含任何额外的错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript语言指南中的try语句error语句。另请参阅处理错误。此外,在适当的事件之间(例如delay 0.5(可能需要使用延迟命令,并适当设置延迟

最新更新