I have a user that wants to automate creating PDFs that prevent copying content with a password. In the UI from any Print dialog, you’d hit PDF > Save as PDF… then ‘Security Options’. From there you’d click the checkbox to require password to copy as shown here:
Note that this is NOT the same as encrypting the pdf to require a password to open, which he specifically does not want.
I can do this with gui scripting (below), but aside from the yeuk! factor its far too fragile (user’s language is not English, for a start off, so all the ui element names are probably wrong for his mac).
Since the user is managing part of his workflow in Automator, I thought I’d look into whether I could whip up a quick custom Automator action in Xcode (something novel to try…), but I’m stuck trying to find a public API for that check box option.
I did find
@property(nonatomic, readonly) BOOL allowsCopying;
The ability to copy content from a PDF document is an attribute unrelated to whether the document is locked or unlocked. It depends on the PDF permissions set by the document’s author.
This method only determines the desired permissions setting in the PDF document; it is up to the application to enforce (or ignore) the permissions.
This method always returns YES if the document is not encrypted. Note that in many cases an encrypted document may still be readable by all users due to the standard empty string password. For more details about user and owner passwords, see the Adobe PDF specification.
in Quartz > PDFDocument, but that method is marked as available in 10.12+, whereas this option has been available on macOS before that, so I’m guessing it’s not the same.
Anyone have any insights?
nb. Of course, I’m aware that once his PDF is open on someone else’s machine, copying the content is always going to be possible, even if it’s just taking a screenshot, but this is what the user’s asked for.
TIA
Phil
#######################
-->> DESCRIPTION
#######################
(*
An example of GUI scripting to save a Word.doc as a PDF with security option 'Require password to copy text, images and other content' set to 'ON'. A default password of '123456' is set in the VARIABLES section below.
This script was tested on macOS 10.12.6 (‘Sierra’) using Word for Mac 2011. The mac's default language was English.
Due to the requirements of GUI scripting, it is highly likely to fail if run in a different environment.
*)
#######################
-->> USAGE
#######################
(*
Change the password in the VARIABLES section below to something else.
Open a Word document and run the script.
Note the script runs on the frontmost document.
It does not save or close the Word document itself.
*)
#######################
-->> VARIABLES
#######################
(* example password *)
set thePassword to "123456"
#######################
-->> COMMANDS
#######################
tell application "Microsoft Word"
activate
end tell
tell application id "com.apple.systemevents" -- System Events.app
tell its application process "Microsoft Word"
(* click the print menu item *)
tell its menu bar 1
tell its menu "File"
tell its menu item "Print..." to perform action "AXPress"
delay 0.5
end tell
end tell
(* click the PDF button *)
tell its window "Print"
tell its menu button "PDF"
perform action "AXShowMenu"
tell its menu "PDF"
tell its menu item "Save as PDF…"
perform action "AXPress"
end tell
end tell
end tell
delay 0.5
(* click the Security Options button *)
tell its sheet 1
tell its group 1
tell its button "Security Options..."
perform action "AXPress"
end tell
end tell
end tell
end tell
(* select the PDF Security options window *)
tell its 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 its checkbox "Require password to copy text, images and other content"
if (value is equal to 0) then
perform action "AXPress"
end if
end tell
(* add the password and confirm *)
set tf to 1
repeat 2 times
tell text field tf
set its value to thePassword
perform action "AXConfirm"
end tell
set tf to 2
end repeat
tell its button "OK" to perform action "AXPress"
end tell
(* save the pdf with the default name and location - it's possible to set this in the script, but I didn't bother for this test - if the document already exists it will halt the script here and ask if you want to replace it. *)
tell its window "Print"
tell its sheet 1
tell its button "Save" to perform action "AXPress"
end tell
end tell
end tell
end tell
#######################