0

I have a strange issue. My short applescript is kind of working. I want to tag all the rectangles (these are what Adobe's library calls image frames) with the XML tag "Image".

set tagName to "Image"
set imageList to {}

tell application "Adobe InDesign CC 2018"
tell active document

    set x to make XML element of first XML element with properties {markup tag:tagName}

    set imageList to every rectangle //there are 4 rectangles
    repeat with i from 1 to number of items in imageList
        tell item i of imageList to markup using x

    end repeat

end tell

end tell

The log shows it is marking up each rectangle. But when I check the document, only the last rectangle in imageList actually shows that the XML tag is applied.

Moreover I can cancel or stop the script, and the last image before cancelling gets the tag. (That is, if I cancel when it is processing rectangle 2, rectangle 2 gets the image, but rectangles 1, 3, and 4 do not.

RobC
  • 22,977
  • 20
  • 73
  • 80
Paeon
  • 85
  • 1
  • 8

2 Answers2

2

Your current criterion:

Firstly, the criterion that you're currently using to infer an image is prone to error. A page item whose type is rectangle does not necessarily equal an image. For instance; an image may be placed inside a circle, in which case its type will be an oval (not a rectangle).

The following line of code in your question which reads:

set imageList to every rectangle

will also include any page item(s) which were created using the Rectangle Frame Tool - which in many .indd files will not be an image.


Recommended criterion:

To infer an image I recommend utilizing all graphics instead of rectangles to obtain a list images. InDesign's Applescript dictionary describes a graphic as:

graphic An imported graphic in any graphic file format (including vector and bitmap formats.)


Solution:

The following AppleScript gist demonstrates a way to automate the process of tagging all images with an XML tag named "Images". Each resultant tagged image will be added as a child XML element of the documents root XML element.

set tagName to "Image"

tell application "Adobe InDesign CC 2018"
  tell active document
    if (count of page items) = 0 then return

    set locked of every layer to false
    set locked of every page item to false

    repeat with currentImage in all graphics
      if associated XML element of currentImage is not equal to nothing then
        untag associated XML element of currentImage
      end if
      make XML element at XML element 1 with properties {markup tag:tagName, XML content:currentImage}
    end repeat

  end tell
end tell

Explanation

  1. set tagName to "Image" assigns the name of the XML element (i.e. "Image") to the tagName variable.

  2. The line which reads:

    if (count of page items) = 0 then return
    

    ensures we exit the script early if the document contains no page items.

  3. The lines which read:

    set locked of every layer to false
    set locked of every page item to false
    

    ensures all document layers and page items are unlocked. If an image was locked then it cannot be tagged.

  4. The lines reading:

    if associated XML element of currentImage is not equal to nothing then
      untag associated XML element of currentImage
    end if
    

    untags any existing tag the image may have as it may be incorrect.

  5. The line reading:

    make XML element at XML element 1 with properties {markup tag:tagName, XML content:currentImage}
    

    carries out the actual tagging of the image.

RobC
  • 22,977
  • 20
  • 73
  • 80
  • Thank you. The Indesign documents I'm working with have only images, so every rectangle is an image. I use InDesign to size and position thumbnails, hero images, slider images, etc, then output to HTML with the fixed size. But now we are changing the design of the website, so I need to export the documents as XML, which creates a folder and based on my selection will export the original image. So yes, I know a rectangle isn't necessarily an image, but in my case it will be. – Paeon Oct 04 '18 at 14:46
-1

Thanks to a suggestion from Mark Anthony on MacScripter.net. It works for my needs. All the docs I'm processing have only images in them (no other graphics, frames, etc.)

set tagName to "Image"
set imageList to {}

tell application "Adobe InDesign CC 2018"
tell active document

    set x to make XML element of first XML element with properties {markup tag:tagName}

    set imageList to every rectangle
    repeat with rect in imageList
        make XML element at XML element 1 with properties {markup tag:"Image", 
XML content:rect}
    end repeat

end tell

end tell
Paeon
  • 85
  • 1
  • 8