mdelatorre icon

Save PDF w/Security Options from MS Word

mdelatorre | PRO | 09/05/20 05:17:59 AM UTC | 0 ⭐ | 678 👁️ | Never ⏰ | []
AppleScript |

3.05 KB

|

None

|

0 👍

/

0 👎

#######################
-->> ABOUT
#######################
(*
 
 Phil Stokes -- 2017
 applehelpwriter.com 
 sqwarq.com 
 
*)
#######################
-->> 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
 
#######################
#EOF

Comments