保存带密码保护PDF的AppleScript报错求助(Monterrey系统)
macOS Monterey下Preview密码保护PDF脚本失效问题
(原脚本在Big Sur系统可正常运行,升级后无法定位UI元素)
问题描述
这段用于保存带密码保护PDF的AppleScript,在Big Sur系统中工作正常,但升级到macOS Monterey后出现报错:无法找到splitter group 3(对应页面样式设置),同时无法定位“PDF”按钮执行“Save as PDF”操作。
原脚本代码
-- Don't change this set FixedPassword to "somePW" --Change the following for each Review accordingly set exam to "2" set session to "5" set VarPassword to "barcelona" set outpath to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬ "Exam" & " " & exam & " " & "Session" & " " & session & "/E" & exam & "S" & session & ".pdf" set SaveFolder to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬ "Exam" & " " & exam & " " & "Session" & " " & session set FileName to "E" & exam & "S" & session & " " & "(Password" & " - " & VarPassword & ").pdf" --Saving PDF with password protection 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 1 end repeat tell sheet 1 tell splitter group 3 click pop up button 3 click menu item "Review 216 by 279 mm" of menu 1 of pop up button 3 end tell tell splitter group 3 click menu button "PDF" repeat until exists menu 1 of menu button "PDF" delay 3 end repeat click menu item "Save as PDF" of menu 1 of menu button "PDF" end tell end tell end tell -- Make sure the save dialog is visible repeat until exists sheet 1 of sheet 1 of front window delay 5 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
报错截图

解决思路与建议
1. 重新定位UI元素(修复原脚本核心问题)
macOS Monterey修改了Preview打印窗口的UI层级结构,原脚本依赖的元素索引(如splitter group 3)已失效:
- 使用Xcode Accessibility Inspector(需安装Xcode开发者工具)或第三方工具
UI Browser,查看Monterey下Preview打印窗口的UI树,找到页面样式弹窗和PDF按钮的新层级路径或标识符。 - 优先通过控件名称而非索引定位元素,比如将原脚本中定位PDF按钮的代码改为:
tell sheet 1 of front window click menu button "PDF" repeat until exists menu 1 of menu button "PDF" delay 0.5 end repeat click menu item "Save as PDF" of menu 1 of menu button "PDF" end tell
2. 优化等待逻辑
原脚本的固定延迟在不同系统环境下稳定性差,替换为基于元素存在的判断:
-- 等待打印窗口出现 repeat until exists sheet 1 of front window delay 0.5 end repeat
3. 更稳定的替代方案:使用命令行工具
避开Preview的UI操作,用qpdf直接给PDF加密,跨版本兼容性更强:
- 先通过Homebrew安装qpdf:
brew install qpdf - 在AppleScript中调用命令行执行加密:
这种方法无需操作Preview界面,完全避免UI结构变化带来的问题。set encryptCommand to "qpdf --encrypt " & quoted form of VarPassword & " " & quoted form of FixedPassword & " 256 -- " & quoted form of outpath & " " & quoted form of (SaveFolder & "/" & FileName) do shell script encryptCommand
内容的提问来源于stack exchange,提问作者Caterina
相关产品推荐
相关产品推荐

