Search code examples
pdfapplescript

How do I open a password-protected PDF with applescript?


I need a little applescript to open a password-protected pdf.

I can't figure out how to access the password text field.

The code I've got :

set elPdf to ("Path:to:my:file.pdf")
activate application "Preview"

tell application "Preview"
    open elPdf
    delay 1
    tell application "System Events"
        set value of text field 1 to "password"
        keystroke return
    end tell
end tell

Applescript is not finding the text/password field, even when I add "sheet 1" and "window 1"

How to I access the password field of a preview.app pdf with applescript?


Solution

  • I'd try to access password input via menu bar:

    set myFolder to path to desktop as string
    set myFile to myFolder & "trial.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 13
              keystroke "34ue8XD5mM"
              keystroke return
            end tell
          end tell
        end tell
      end tell
    end tell