Word to PDF with osascript

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 value) |error|:(reference)
	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) - theTop - theBottom}}
	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

I might be wrong but you’re unlikely to get any kind of usable results by trying to extract Word doc contents as NSAttributedString.

I assume you don’t have Word installed that’s why you’re not using its own AppleScript facilities.

You may have better luck trying to find a command line utility that can print Word docs in combination with a virtual PDF printer.

Here’s one such suggestion:

I have a GUI Apple script that does this for me too. Problem is that it breaks with every single OS update (when buttons and overall layout change). I wanted to have a script that I could use from the command line without any GUI involved. Is your suggestion like that?

@scienception
In addition to excellent suggestions from @Fredrik71: If you do have Word then you don’t need GUI scripting - you can use Word’s own AppleScript commands (if I understand your question correctly).

Thank you! But how can I set up a specific page size/margins for printing?

I need to automatize converting Word files to PDF files with a specific layout and password protection.

for password protection you’ll need to create PDF first, then use PDFKit to assign security settings including password.

as to specific layout: normally, all layout options should be covered by Word’s AppleScript dictionary (although some of them you may need to apply to the document itself first rather then set them up as print settings).

How could I set up something like this? Could you provide me a link to an example using Word’s AppleScript dictionary?

you’ll probably need to set your Word document size to your custom page size.

i don’t have specific examples but you’ll need to use document’s page setup property.

Thanks again! But in this script where is the layout defined?

You can find these in the Word dictionary. It is expansive so you might search for ‘page setup’.

This may give you something to start with but you should be able to work with more than just margins:

tell application "Microsoft Word"
	set top margin of page setup of document 1 to 24
	-- to return the page setup object…
	-- properties of page setup of document 1
	
	left margin of page setup of document 1
	--> 90.0
	top margin of page setup of document 1
	--> 24.0
	right margin of page setup of document 1
	--> 90.0
	bottom margin of page setup of document 1
	--> 72	
	
	set left margin of page setup of document 1 to 36
	left margin of page setup of document 1
	--> 36.0
	
	-- you can use inches like so
	set left margin of page setup of document 1 to (inches to points inches 0.75)
	left margin of page setup of document 1
	--> 54.0
	
	-- and metric like so but note the spelling
	set right margin of page setup of document 1 to (centimeters to points centimeters 2)
	right margin of page setup of document 1
	--> 56.70000076294

end tell

You can also use millimeters, lines —and best of all— picas (i.e. 12 points).

@zevrix… by the way, that’s not a custom size… that’s Letter in millimetres.

yeah i thought it looks like a standard size…

but all i know is that it’s defined as “custom” paper:

who knows maybe OP works in an environment where Letter page should be defined as custom size.

Seems weird for Word but I guess that’s the case.