Word file 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 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

It seems some lines are incomplete. This is the complete code:

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

NSAttributedString can handle RTF or RTFD. I don't think it can handle Word documents (.docx). Check it at first.

And...You can not debug it with Terminal.app (osascript).

You have to use LateNight Software's Script Debugger.

https://latenightsw.com/

Word file to PDF with osascript
 
 
Q