Word Add Shapes + Applescript

I would like to add autoshapes in word document something like a flowchart or arrows or some random shapes using the mac script.
set newShape to make new shape with properties {shape type:autoshape pentagon, width:100, height:100}

facing error while execution: “Can’t make autoshape pentagon into type constant. (-1700)”
Can anyone suggest a solution.

The correct syntax from Microsoft Word AppleScript reference:

make new shape at active document with properties {auto shape type:autoshape rectangle, left position:50, top:50, width:100, height:200}

Simply replace “autoshape rectangle” with “autoshape pentagon”

See pages 376-379

1 Like

If you create a shape manually in Words and run below script. To get the properties.

tell application "Microsoft Word"
	its properties of shapes of document 1
end tell

With the above properties of the shape you created manually. You could construct the below AppleScript. The make new shape need to be in document element. Also I used at front. I also tested at end… I believe its the order the shape is displayed in the document.

tell application "Microsoft Word"
	tell document 1
		set newShape to make new shape at front with properties {auto shape type:autoshape regular pentagon}
		tell newShape
			set width to 100.0
			set height to 100.0
			set left position to 180.0
			set top to 200.0
		end tell
	end tell
end tell

@KniazidisR @Fredrik71 Thanks for the help, I would like to know if its possible to set page color of word document using apple script. I have been trying out following code snippet
tell its active document set fore color of fill format of background shape to theColor set visible of fill format of background shape to true end tell
Any help would be appreciated. Thanks

The background shape of certain document is read only object. You can’t set or edit its colours. Make new shape over the background shape with bounds of the background shape (you can read them to some variable to use them for new shape). You can read/write (apply colours as well) on the new shape.

I think a better approach would be to build background style with shape objects and group it. This way your could select the object and change it manually.

To build a background layout is the same approach as any shape object.

Finally you set the group background to send back.

Snippet of code I tried,
> on run argv

    	set targetDoc to item 1 of argv
    	tell application "Microsoft Word"
    		tell its document targetDoc
    			set myPagesetup to page setup of document targetDoc
    			set myPagewidth to page width of myPagesetup
    			set myPageheight to page height of myPagesetup
    			# display dialog "Page width: " & myPagewidth & return & "Page height: " & myPageheight
    			set myShape to make new shape at document targetDoc with properties {auto shape type:autoshape rectangle, top:0, width:myPagewidth, height:myPageheight, left position:0}
    			set fore color of fill format of myShape to {fill type:fill solid, color:{255, 0, 0}}
    			set z order of myShape to send shape behind text
    		end tell
    	end tell
    end run

Currently facing an error that: “Microsoft Word got an error: Can’t set «class 3326» of shape “Shape_25” of document “Monte_Cristo_original_small.docx” to send shape behind text. (-10006)” Object is created on the document, but could not send it to behind text.

I’m not using a Microsoft Word, so I just have to guess that the send command that sends the text to the front doesn’t exist.

Do not send nothing, do not fill fore color. Use back color instead. Example:

set back color of fill format of myShape to {255, 0, 0} as RGB color

z order is a command, not a property. (Although, the command does have z order command parameter (or property)).

z order v : Moves the specified shape in front of or behind other shapes.
  z order shape
    z order command bring shape to front/‌send shape to back/‌bring shape forward/‌send shape backward/‌bring shape in front of text/‌send shape behind text

You should be able to change the colour of the background shape. The set command seems correct but I don’t know about the tell statement? Why the peculiar tell its…?

fore color (RGB color) : Returns or sets a RGB color that represents the foreground color for the fill, line, or shadow.

I would recommend simplifying your script until you get it working. Just open a single document and use the minimum of tell statements so that you know exactly what you (and your script) are referencing. And by the way, colours are 16-bit (i.e. 5 digits for each channel). Finally, both the shape and its fill format have a visible property.