ID Script to place images w/ captions only returns metadata for last item
- June 28, 2024
- 1 reply
- 2832 views
Hello!
Quick background: I have modified a script created by Kasyan (found here) that finds tags and replaces them with images. The main changes I made now have the script create an anchored text frame where the tag is found, find and insert the image file inline in the new text frame, and then grab the "Description" from the xmp link and insert it as text after the image.
The problem: when I run the script it is only able to return the metadata for the last image that is tagged/created. The rest return the "Description not found in metadata" line I included in case the data was missing. All files have the necessary info, I can reorder the tags, and no matter what - the last tag in the document is the only one that returns any data from the image file. (Thought: the script does work bottom-up so this would be the first tag/image that the script encounters.)
The code: below is the meat of the script where I made most of the changes. Most of the script outside of this section is the same as the original script by Kasyan, linked above so you can compare. I will attach the full script in case the problem cannot be found in the excerpt below:
for (var i = 0; i < foundItems.length; i++) {
foundItem = foundItems[i];
figureTagID = foundItem.contents.replace(/@/g, "");
pageNumber = GetPageNumber(foundItem);
if (log) logArr.push((i +1) + ": \t" + figureTagID + "\tpage: " + ((pageNumber != null) ? pageNumber : "N/A") + "\tfile placed: " + fileName);
story = foundItem.parentStory;
if (set.addPages && story.overflows && foundItem.parent instanceof Cell == false) {
if (debug) $.writeln("The story overflows - AddPages");
AddPages(story);
}
insPtIndex = foundItem.insertionPoints[0].index;
file = new File(imgsFolder.fsName + "/" + figureTagID);
imgFile = GetFile(imgFiles, figureTagID);
fileName = imgFile.displayName
progressBar.value = (i + 1);
progressTxt.text = figureTagID;
if(imgFile != null) {
//create caption frame
imgCaption = story.insertionPoints[insPtIndex].textFrames.add();
//set position and size of the caption frame
imgCaption.geometricBounds = [imgCaption.geometricBounds[0],
imgCaption.geometricBounds[1],
imgCaption.geometricBounds[2],
imgCaption.geometricBounds[1] + maxWidth ];
//insert container for image file
container = imgCaption.insertionPoints[0].rectangles.add();
currentStyle = container.appliedObjectStyle;
//set position and size of image file container
gb = container.geometricBounds;
width = gb[3] - gb[1];
height = gb[2] - gb[0];
container.geometricBounds = [container.geometricBounds[0],
container.geometricBounds[1],
gb[0] + maxHeight,
gb[3] ];
container.geometricBounds = [container.geometricBounds[0],
container.geometricBounds[1],
container.geometricBounds[2],
container.geometricBounds[1] + maxWidth ];
//place figure image
img = container.place(imgFile)[0];
var imgPath = img.itemLink
//set frame fitting options for image
if (set.fitOption > 1) {
container.fit(fitOptions[set.fitOption - 2]);
if (set.fitFrameToContents && (set.fitOption == 6 || set.fitOption == 7)) {
container.fit(FitOptions.FRAME_TO_CONTENT);
}
}
//find and place metadata as caption
if (imgDescription != null) {
//placeholder if no metadata found
imgCaption.insertionPoints[-1].contents = "\r Description not found in metadata";
}
else {
//get XMP Description
var imgDescription = imgPath.linkXmp.description;
//add caption contents from XMP Description
imgCaption.insertionPoints[-1].contents = "\r" + imgDescription;
}
//finalize styles
imgCaption.applyObjectStyle(currentStyle);
imgCaption.clearObjectStyleOverrides ();
container.applyObjectStyle(app.activeDocument.objectStyleGroups.itemByName("Figures"));
container.clearObjectStyleOverrides ();
//remove figure tag from text
foundItem.remove();
story.recompose();
count++;
}
else {
logErrArr.push(figureTagID + " - the file doesn't exist." + " - page " + ((pageNumber != null) ? pageNumber : "N/A"));
}
} // end for
NOTE: also, infuriatingly, I did have the description loading for all images at one point, but broke it when putting together the code that places the image file in the new text frame.
Thank you all so much for any help you can offer.
