eww eww - 4 months ago 14
AppleScript Question

Word, AppleScript - "update link format" working with inline shapes?

Has anyone had any success issuing an "update" in AppleScript when interacting with inline shapes Word 2011?

I've got the following chunk of code (mostly swiped from the Mac Office 2004 AppleScript reference):

tell application "Microsoft Word"
repeat with aShape in (get inline shapes of active document)
if auto update of link format of aShape is false then
update link format of aShape
end if
end repeat
end tell


As far as I gather, this just gets the inline shapes and for those where auto-update is set to false, it says "update the shape from the link."

I insert an image, making sure to link it to the file (let's say
"/Users/me/Documents/myfile.png"
). If I ask Word to "give me a list of inline shapes and their paths" in AppleScript I see
"/Users/me/Documents/myfile.png"
. OK, cool.

Then I run the code.

Couple of things happen:


  1. auto update
    says its value is missing - I have to check
    missing
    value
    which seems really wrong. I gather it's supposed to be a BOOL, but... it's just nil.

  2. If I check against missing value
    and just ignore it (which may part of my problem) and I run
    update link format
    on the shape, the shape's path is completely removed.
    So at this point my shape path is totally gone - forever. The image is still in the document, but the path / link has been nulled out.



I have to assume I'm doing something wrong here.

Does anyone have any suggestions on how to
update link format
correctly?

Thank you in advance

Note: I'm writing a Cocoa app and am opting to try to incorporate the AppleScript approach because the general "update" method for the Obj-C application object seemed to accept an enum and not the actual object... and it also didn't seem to do anything at all.

Answer

Actually, I do not think you are doing anything wrong but I believe it takes a trick to make this stuff work, and I am not completely sure about that. The explanation is in any case too long for a comment.

As a general observation, in Word you tend to get missing value in cases where the property is not relevant in that case. So I think if you have a picture which does not have any link, link format of aShape will return missing value.

Any shape which has a link should have a link format that is not a missing value. But AFAIK auto update is only applicable to inline shapes that are links to OLE Objects, in which case the value of auto update can be true or false, but for images that are not OLE links, auto update will be missing value.

(In earlier versions of Word, roughly speaking an image that was linked was represented by a field code. If it was linked via OLE, the field type would be { LINK }. Otherwise, if it was just a link to a picture file, the field type would be { INCLUDEPICTURE }. In recent versions of Word, the handling of graphics has changed, more than once, and you typically only see these field codes if you insert them yourself, or if you save a document as a .doc rather than a .docx)

So perhaps one thing you need to do is check the inline shape type of the inline shape and adjust the processing depending on the type.

AFAIK if you have a linked OLE object, then using update link format should work. But if it's a link to a picture file, it doesn't. what does appear to work is to set the full name of the picture to itself, e.g.

set source full name of link format of aShape to source full name of link format of aShape

Except that the result of that is actually to replace the picture by a box with a message. But repeating the command does appear actually to update the link, i.e.

set source full name of link format of aShape to source full name of link format of aShape
set source full name of link format of aShape to source full name of link format of aShape