Tuesday, June 27, 2017

#1 2017-06-05 10:39:24 am

ewhitley
Member
Registered: 2016-09-15
Posts: 5

Word + AppleScript - How to insert shape at location?

Hello -

TLDR; How do I insert a shape at a specific position within a block of text (a text range with the same start/end)?

Something like the Windows equivalent (https://msdn.microsoft.com/en-us/librar … 2147217396)

I've been struggling with this one for several hours and I must be missing something.

OS: macOS 10.12.5
App: Word 2016 (15.33, 170409)

I'm trying to use AppleScript to insert a shape at a cursor position, but am unable to do so correctly. I've reviewed the 2004 AppleScript guide for Word, the Word scripting dictionary, and also Googled the heck out of samples.

As I gather, you should be able to "make new shape at [location]", but I'm struggling with the [location] portion. I understand that there's an anchor and you should be able to specify the anchor for the shape, then give the shape a position relative to the anchor.

Attempt 1: Specify "active document" (places box, but [as expected] not at selection position)
=============

Applescript:


set shapeName to "my shape"
set theRange to text object of selection

set myShape to make new shape at active document with properties {shape type:shape type text box, anchor:theRange, top:0, left position:0, width:300, height:100, name:shapeName, relative horizontal position character, relative vertical position:relative vertical position paragraph}

This inserts the text box, but, it's at the start of the document.

I thought/hoped that specifying relative position would let me say "basing the position on the anchor, make this box at (relative top) and (relative left)". For the above code if I do specify a "top" (ex: 100), the box will be created 100 points below the start of the active _page_ of the document. It doesn't seem to be honoring the anchor as the reference starting point.

Attempt 2: Specify range (fails with error)
=============
I thought that maybe by specifying "at active document" I was telling it to position the element relative to the page. I tried to tell it to position at the anchor (text range).

Applescript:


set shapeName to "my shape"
set theRange to text object of selection

set myShape to make new shape at end of theRange with properties {shape type:shape type text box, anchor:theRange, top:0, left position:0, width:300, height:100, name:shapeName, relative horizontal position:relative horizontal position page, relative vertical position:relative vertical position page}

This fails with the error: "Microsoft Word got an error: Can’t make shape type text box into type constant."

I've tried a bunch of variations of positions of "theRange", but I've not yet been successful at doing this. I've also tried specifying the selection instead of the range, but that also fails with the same error. I'm guessing you just can't approach the shape this way.


Any insights very much appreciated.
Thank you!


Filed under: Word

Offline

 

#2 2017-06-15 12:56:29 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: Word + AppleScript - How to insert shape at location?

I think that if you want it positioned at a certain point within text, rather than a certain position on the page, you want "inline shape" not "shape."

Take a look at the dictionary for inline shape and see if that does what you're looking for.

Also, page 365 of this:
https://applescriptlibrary.files.wordpr … erence.pdf

May be useful.

Last edited by t.spoon (2017-06-15 12:59:52 pm)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.084 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)