Why is this giving a super messed up PDF with no images? What am I doing wrong?
use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "AppKit"
-- dimensions of print area:
property thePaperSize : {height:479, width:516}
property theLeft : 0
property theRight : 0
property theTop : 0
property theBottom : 0
on run argv
set course to "course1"
set theyear to "2023"
set semester to "Spring"
set exam to item 1 of argv
set session to item 2 of argv
set chapters to item 3 of argv
if semester="Spring" then
set sem_num to "1"
else if semester="Summer" then
set sem_num to "2"
else if semester="Fall" then
set sem_num to "3"
else
return "wrong semester"
end if
set inpath to "/Users/Dropbox/"& course &" - " & "Current/" & theyear & "-"& sem_num &"-"& semester &"/2." & " " & "Chapter" & " " & "Reviews/" & ¬
"Exam" & " " & exam & " " & "Session" & " " & session & "/"& course &" Exam " & exam & " Session " & session & " (Ch "& chapters &").docx"
-- choose Word document file, make URL and build destination path
set posixPath to inpath
set theURL to current application's |NSURL|'s fileURLWithPath:posixPath
set destPath to theURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingPathExtension:"pdf"
-- get doc's contents as styled text
set {styledText, theError} to current application's NSAttributedString's alloc()'s initWithURL:theURL options:(missing value) documentAttributes:(missing val$
if styledText = missing value then error (theError's localizedDescription() as text)
-- set up printing specs
set printInf to current application's NSPrintInfo's sharedPrintInfo()'s |copy|()
printInf's setJobDisposition:(current application's NSPrintSaveJob)
printInf's dictionary()'s setObject:destPath forKey:(current application's NSPrintSavePath)
-- make text view and add text
set theView to current application's NSTextView's alloc()'s initWithFrame:{{0, 0}, {(width of thePaperSize) - theLeft - theRight, (height of thePaperSize) - $
theView's textStorage()'s setAttributedString:styledText
-- set up and run print operation without showing dialog
set theOp to current application's NSPrintOperation's printOperationWithView:theView printInfo:printInf
theOp's setShowsPrintPanel:false
theOp's setShowsProgressPanel:false
theOp's runOperation()
end run
Post
Replies
Boosts
Views
Activity
Can anyone please help me find a solution for the error I'm getting? MacOS is Monterrey and this script used to work in Big Sur.
It seems it is unable to find splitter group 3 (which would the page style). And the same happens when it tries to find "PDF" splitter and save as PDF.
I would appreciate any help/suggestions you can provide.
-- Don't change this
set FixedPassword to "somePW"
--Change the following for each Review accordingly
set exam to "2"
set session to "5"
set VarPassword to "anotherPW"
set outpath to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬
"Exam" & " " & exam & " " & "Session" & " " & session & "/E" & exam & "S" & session & ".pdf"
set SaveFolder to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬
"Exam" & " " & exam & " " & "Session" & " " & session
set FileName to "E" & exam & "S" & session & " " & "(Password" & " - " & VarPassword & ").pdf"
--Saving PDF with password protection
tell application "Preview"
activate
open outpath
end tell
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 1
end repeat
tell sheet 1
tell splitter group 3
click pop up button 3
click menu item "Review 216 by 279 mm" of menu 1 of pop up button 3
end tell
tell splitter group 3
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 3
end repeat
click menu item "Save as PDF" of menu 1 of menu button "PDF"
end tell
end tell
end tell
-- Make sure the save dialog is visible
repeat until exists sheet 1 of sheet 1 of front window
delay 5
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
(* click the checkbox to on *)
-- NOTE: for some reason there is a delay of about 6 seconds here, I do not know why
tell checkbox "Require password to open document"
click
end tell
(* add the password and confirm *)
keystroke VarPassword
keystroke (ASCII character 9)
keystroke VarPassword
tell its checkbox "Require password to copy text, images and other content"
click
end tell
(* add the password and confirm *)
keystroke FixedPassword
keystroke (ASCII character 9)
keystroke FixedPassword
click button "OK"
end tell
repeat until exists sheet 1 of sheet 1 of front window
delay 0.2
end repeat
-- Press command+shift+g to show the "Go" drop down sheet
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
-- Now that we are in our desired folder, set the file name and save
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
But it gives the following error: