I am trying to automatize the process of creating password-protected PDF files using Preview.
- I first save as PDF
- I select Security Options
But there is a strange long delay when ticking the following checkbox:
tell sheet 1 of sheet 1 of front window
click button "Security Options..."
end tell
tell window "PDF Security Options"
-- delay of 12 seconds here
tell checkbox "Require password to open document"
click
end tell
...
This strange delay seems to be a problem other people have encountered and I wanted to get the opinion of an AppleScript expert on this issue.
This is another person who has the same problem:
https://forum.latenightsw.com/t/scripting-pdf-security-options/739
This is the entire code:
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
To, be able to test, I was forced to edit your script.
If I have some time available I will add instructions able to auto-localize it.
set VarPassword to "VarPassword"
set FixedPassword to "FixedPassword"
set FileName to "protected.pdf"
set SaveFolder to POSIX path of (path to desktop)
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"
click menu item "Enregistrer au format 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 "Options de sécurité…" --"Security Options..."
end tell
tell window "Options de sécurité PDF" --"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 "Exiger un mot de passe pour ouvrir le document" --"Require password to open document"
click
end tell
keystroke VarPassword
keystroke (ASCII character 9)
keystroke VarPassword
tell its checkbox "Demander un mot de passe pour copier du texte, des
images et tout autre contenu" --"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
click button "Aller" 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
click button "Enregistrer" of sheet 1 of sheet 1 of front window
end tell
end tell
There is really the described delay.
I guess that if somebody may give you a serious answer, it’s some engineer in Cupertino.
Send a bug report then be patient.
Maybe, one day, without any kind of warning, you will see that the described behavior has been changed.
Don’t forget, I wrote : Maybe
I won’t bet a penny on that.
At least, I learnt something.
You wrote :
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
click button "Aller" of sheet of sheet 1 of sheet 1 of front window
I always thought that we had to code:
repeat until exists sheet 1 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 1 of sheet 1 of sheet 1 of front window
click button "Aller" of sheet 1 of sheet 1 of sheet 1 of front window
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 27 mai 2020 12:42:01
Thanks for helping me out!
Do you know who should I send the bug report to?
As reporting bugs striking in 10.13.6 is perfectly wasting time, I deleted all datas related to bug reporting on my machine.
You may look at :
https://beta.apple.com/sp/en/betaprogram/
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 27 mai 2020 17:28:03