Skip to main content
Bridgette.Is.Enthusiastic
Inspiring
June 28, 2024
Answered

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. 

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

Yes, I saw this line - but it's set ONLY if imgDescription isn't empty - so this part of the code - else - should never be executed?

 

Yes, you should try to set its value in a try...catch() - but then you don't need to check if it's empty - because if it was not possible to set it - you'll get an error and then you can set your "missing" message in the catch part. 

 

 

1 reply

Robert at ID-Tasker
Legend
June 28, 2024

I'm not JS expert - but you are checking if "imgDescription" is not empty - but you are not applying a value to it before?

 

So it's always empty... A bit strange, that it isn't for the last item in the loop? 

 

 

Bridgette.Is.Enthusiastic
Inspiring
June 28, 2024

thanks for taking the time 🙂 

 

there is this bit of code in there:

var imgDescription = imgPath.linkXmp.description;

 are you saying the issue could be in the order of the code and that I'm asking to see if imgDescription is empty before it is defined?

Peter Kahrel
Community Expert
Community Expert
June 30, 2024

@Bridgette.Is.Enthusiastic

 

You are missing this part in your new code:

 

 

//remove figure tag from text					
foundItem.remove();					story.recompose();		

 

which I think might have been a problem before. 

 

I'm sorry but I'm not JS guy so I might be missing something about how try...catch works. 

 

@rob day, @Peter Kahrel, @brian_p_dts  - can you pitch in? 

 


Kasyan (@kas) would be best placed to deal with this.