The following is my script for creating Password protected files, but I do not know why it takes around 12 seconds for the first checkbox to get clicked!
The second click works fine though.
I have tried several things including this: Applescript delay issue with no success.
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 0.5
end repeat
tell sheet 1
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF" of menu 1 of menu button "PDF"
end tell
end tell
repeat until exists sheet 1 of sheet 1 of front window
delay 0.2
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
-- NOTE: for some reason there is a delay of about 12 seconds here, I do not know why
try
tell checkbox "Require password to open document"
click
end tell
keystroke VarPassword
keystroke (ASCII character 9)
keystroke VarPassword
tell its checkbox "Require password to copy text, images and other content"
click
end tell
keystroke FixedPassword
keystroke (ASCII character 9)
keystroke FixedPassword
click button "OK"
end try
end tell
repeat until exists sheet 1 of sheet 1 of front window
delay 0.2
end repeat
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
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