AppleScript to Automate Envelope Printing

I have a VBScript routine to print envelopes by automating Word. This works just fine.

Now I'm trying to do the same thing with AppleScript, also using the Word application. Here is what I have so far:

set recipientAddress to text returned of (display dialog "Enter the recipient's address:" default answer "")

-- Prompt for recipient city, state, and zip
set recipientCityStateZip to text returned of (display dialog "Enter the recipient's city, state, and zip:" default answer "")

-- Combine all address parts into a full address
set fullAddress to recipientName & return & recipientAddress & return & recipientCityStateZip

-- Create a new Word document and print the envelope
set dialogResult to display dialog "To print envelope for:" & return & return & recipientName & return & recipientAddress & return & recipientCityStateZip & return & return & "Center envelope upside-down in printer with flap on left" & return & return & "Continue?" buttons {"Yes", "No"} default button 2 with icon caution
if button returned of dialogResult is "Yes" then
	
	tell application "Microsoft Word"
		set wdDoc to make new document
		
		-- Print the envelope with the collected recipient address and hard-coded return address
		-- wdDoc's print out envelope(address:fullAddress, returnAddress:returnAddress)
		
		-- Close the document without saving
		close wdDoc saving no
		
	end tell
	
end if

What does NOT work is the commented line near the end of the script which starts with -- wdDoc's print out envelope...

Either I am doing it wrong, or Word for Mac can't be automated that way. Can anyone help with this script, or at least suggest a different method to print an envelope on demand?

Thanks...

Warning, while I'm familiar with AppleScript, not so with scripting Word.

I checked the dictionary and oh boy, the number of commands and classes is quite overwhelming. I'm impressed by the extent that Microsoft made Word scriptable. But for a beginner maybe the worst application to start scripting.

The command you use, print out, is deprecated and Microsoft recommends to use the Standard Suite command print, which is the standard print command for most applications.

Nonetheless I tried both commands. Only with the Standard Suite command 'print' I could print. But only after I saved the document first. With a new unsaved document it does not work.

That said, GUI scripting is a the rescue for such cases. Also for cases where the application is not scriptable and has no dictionary! GUI scripting is done through the application 'System Events'.

We have 2 commands availble, keystroke and click.

Keystroke, as the name implies, simulates a keystroke. The only requirement is to make sure the application is frontmost.

tell application "System Events"
	tell application process "Word"
		set frontmost to true -- to make sure the next command, keystroke, is send to Word
		keystroke "p" using {command down}
	end tell
end tell

Click a menu item, File > Print ...

tell application "System Events" to tell application process "Word" to click menu item "Print..." of menu 1 of menu bar item "File" of menu bar 1

hope this helps

AppleScript to Automate Envelope Printing
 
 
Q