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
set tagName to "Image"
assigns the name of the XML element (i.e. "Image") to the tagName
variable.
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.
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.
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.
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.