You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AppleScript在Preview中为PDF添加密码遇到问题

Fixing AppleScript Password Protection for PDFs in Preview

Hi Tim, let’s work through that script issue you’re having. The "invalid index" error happens because your script is trying to access the Export sheet before it’s fully loaded, plus some of the UI element paths are off. Here’s how to fix it and make the script more reliable:

Key Issues in Your Original Script

  • Timing mismatch: The script runs faster than Preview can pop up dialogs—when it tries to click "Permissions…", the Export sheet hasn’t appeared yet.
  • Fragile menu item index: Using menu item 23 breaks if menu positions change (from updates, custom menus, or different languages).
  • Incorrect UI references: The Permissions dialog is a separate window, not nested under the original sheet, so your path to checkboxes/text fields wasn’t targeting the right elements.

Revised Script with Fixes

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

tell application "Preview"
    open myFile
    activate
    -- Give Preview time to fully load the document
    delay 1
end tell

tell application "System Events"
    tell process "Preview"
        -- Navigate to the File menu and click Export (using name instead of index for reliability)
        tell menu bar 1
            tell menu bar item "File"
                click
                click menu item "Export…" of menu "File"
            end tell
        end tell
        
        -- Wait for the Export sheet to appear
        delay 0.5
        
        -- Click the Permissions button on the Export sheet
        click button "Permissions…" of sheet 1 of window 1
        
        -- Wait for the Permissions dialog to load
        delay 0.5
        
        -- Configure password settings in the Permissions dialog
        tell window "Permissions"
            -- Enable the "Require Password To Open Document" checkbox
            click checkbox "Require Password To Open Document"
            
            -- Set password values directly (more reliable than keystroke)
            set value of text field "Open Password:" to "1234"
            set value of text field "Verify:" to "1234"
            set value of text field "Permissions Password:" to "1234"
            set value of text field "Verify:" to "1234"
            
            -- Close the Permissions dialog
            click button "OK"
        end tell
        
        -- Wait a moment, then save the protected PDF
        delay 0.5
        click button "Save" of sheet 1 of window 1
    end tell
end tell

What Changed & Why

  • Added intentional delays: Short pauses give Preview time to render dialogs and sheets—adjust the delay lengths if your Mac runs slower/faster.
  • Used menu item names: Instead of relying on index numbers, we use "Export…" which stays consistent regardless of menu changes.
  • Direct text field value setting: Using set value of text field is way more reliable than keystroke, since it doesn’t depend on the cursor being in the right place.
  • Targeted the Permissions window correctly: We reference it by name (window "Permissions") instead of trying to nest it under the original sheet.
  • Explicit button clicks: Instead of keystroke return, we click named buttons ("OK", "Save") to avoid accidentally triggering the wrong action.

Important Notes

  • Make sure System Events has accessibility access: Go to System Settings > Privacy & Security > Accessibility, then check the box for your script editor (like Script Editor) or terminal if you’re running it from there.
  • Adjust text strings for your language: If your macOS uses a non-English language, replace terms like "Export…" or "Permissions…" with the localized names from your Preview menu.

内容的提问来源于stack exchange,提问作者Tim G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:42:28