mdelatorre

Save PDF w/Security Options from MS Word

Sep 5th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #######################
  2. -->> ABOUT
  3. #######################
  4. (*
  5.  
  6.  Phil Stokes -- 2017
  7.  applehelpwriter.com
  8.  sqwarq.com
  9.  
  10. *)
  11. #######################
  12. -->> DESCRIPTION
  13. #######################
  14. (*
  15.  
  16. 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.
  17.  
  18. This script was tested on macOS 10.12.6 (‘Sierra’) using Word for Mac 2011. The mac's default language was English.
  19. Due to the requirements of GUI scripting, it is highly likely to fail if run in a different environment.
  20.  
  21. *)
  22. #######################
  23. -->> USAGE
  24. #######################
  25. (*
  26.  
  27. Change the password in the VARIABLES section below to something else.
  28. Open a Word document and run the script.
  29. Note the script runs on the frontmost document.
  30. It does not save or close the Word document itself.
  31.  
  32. *)
  33.  
  34. #######################
  35. -->> VARIABLES
  36. #######################
  37. (* example password *)
  38. set thePassword to "123456"
  39.  
  40.  
  41. #######################
  42. -->> COMMANDS
  43. #######################
  44.  
  45. tell application "Microsoft Word"
  46.     activate
  47. end tell
  48.  
  49. tell application id "com.apple.systemevents" -- System Events.app
  50.     tell its application process "Microsoft Word"
  51.         (* click the print menu item *)
  52.         tell its menu bar 1
  53.             tell its menu "File"
  54.                 tell its menu item "Print..." to perform action "AXPress"
  55.                 delay 0.5
  56.             end tell
  57.         end tell
  58.         (* click the PDF button *)
  59.         tell its window "Print"
  60.             tell its menu button "PDF"
  61.                 perform action "AXShowMenu"
  62.                 tell its menu "PDF"
  63.                     tell its menu item "Save as PDF…"
  64.                         perform action "AXPress"
  65.                     end tell
  66.                 end tell
  67.             end tell
  68.             delay 0.5
  69.             (* click the Security Options button *)
  70.             tell its sheet 1
  71.                 tell its group 1
  72.                     tell its button "Security Options..."
  73.                         perform action "AXPress"
  74.                     end tell
  75.                 end tell
  76.             end tell
  77.         end tell
  78.        
  79.         (* select the PDF Security options window *)
  80.         tell its window "PDF Security Options"
  81.             set selected to true
  82.             set focused to true
  83.             (* click the checkbox to on *)
  84.             -- NOTE: for some reason there is a delay of about 6 seconds here, I do not know why
  85.             tell its checkbox "Require password to copy text, images and other content"
  86.                 if (value is equal to 0) then
  87.                     perform action "AXPress"
  88.                 end if
  89.             end tell
  90.             (* add the password and confirm *)
  91.             set tf to 1
  92.             repeat 2 times
  93.                 tell text field tf
  94.                     set its value to thePassword
  95.                     perform action "AXConfirm"
  96.                 end tell
  97.                 set tf to 2
  98.             end repeat
  99.             tell its button "OK" to perform action "AXPress"
  100.         end tell
  101.         (* 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. *)
  102.         tell its window "Print"
  103.             tell its sheet 1
  104.                 tell its button "Save" to perform action "AXPress"
  105.             end tell
  106.         end tell
  107.     end tell
  108. end tell
  109.  
  110. #######################
  111. #EOF
Add Comment
Please, Sign In to add comment