• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

ID Script to place images w/ captions only returns metadata for last item

Explorer ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

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. 

TOPICS
Scripting

Views

875

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 28, 2024 Jun 28, 2024

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. 

 

 

Votes

Translate

Translate
Community Expert ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

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? 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

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. 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

thank you so much! so the new code there would look like this?

 

//find and place metadata as caption
try {

//get XMP Description
var imgDescription = imgPath.linkXmp.description;
			
//add caption contents from XMP Description
imgCaption.insertionPoints[-1].contents = "\r" + imgDescription;
}

catch (imgDescription != null) {

//placeholder if no metadata found
imgCaption.insertionPoints[-1].contents = "\r Description not found in metadata";
}

 

I'll give it a shot and let you know how it goes!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

@Bridgette.Is.Enthusiastic

 

I don't think you need to check it for being empty - catch part is executed only when try fails - so catch(e) {...} would be enough. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

PROGRESS!!

 

description !=null broke the script, it had to be "err" as you suggested.

 

It failed on a bunch of tags but then I limited it to just three tags and it worked for both! so this did solve the "only the last guy" issue. THANK YOU SO MUCH!!!

 

but now the "catch" part is throwing the error "undefined is not an object" for this line which worked (too well) before:

 

imgCaption.insertionPoints[-1].contents = "\r Description not found in metadata";

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

@Bridgette.Is.Enthusiastic

 

You are welcome. 

 

 

	if(imgFile != null) {
		//create caption frame
		imgCaption = story.insertionPoints[insPtIndex].textFrames.add();

 

 

Maybe because this part isn't executed? 

 

You should move your try...catch code inside - at the end - of this if - so it will be executed ONLY when file has been imported correctly? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

hmmmmmm moving it didn't help - I also don't understand why the same code would work in the "try" section but then not in the "catch" section (the only difference is the content, and the "catch" content is just text). Any additinal insight would be great - I'm going to keep fiddling.

 

I've marked your answer up above as correct, since that part solved the original issue.  Thank you so much once again for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

@Bridgette.Is.Enthusiastic

 

Can you post your new code? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

full script attached and here's a larger exerpt:

		if (imgsFolder != null) {
			imgFiles = GetFiles(imgsFolder);

			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);
						}
					}
					
					//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();		
					
					//find and place metadata as caption
					try {

						//get XMP Description
						var imgDescription = imgPath.linkXmp.description;
			
						//add caption contents from XMP Description
						imgCaption.insertionPoints[-1].contents = "\r" + imgDescription;
					}

					catch (err) {

						//placeholder if no metadata found
						imgCaption.insertionPoints[-1].contents = "\r Description not found in metadata";
					}					

					count++;
				}
				else {
					logErrArr.push(figureTagID + " - the file doesn't exist." + " - page " + ((pageNumber != null) ?  pageNumber : "N/A"));
				}
				
			} // end for
			
			progressWin.close(); 
			
			var endTime = new Date(),
			duration = GetDuration(startTime, endTime);

			var report = "Finished. " + count + " image" + ((count == 1) ? " was" : "s were") + " placed.";
			if (logErrArr.length > 0) report += " " + logErrArr.length + " error"+ ((logErrArr.length == 1) ? "" : "s") + " occurred."
			if (log && logErrArr.length > 0) report += " See the error log on the desktop.";
			report += "\rTime elapsed: " + duration;
			
			if (log) {
				logArr.push("=========================================================");
				logArr.push(count + " image" + ((count == 1) ? " was" : "s were") + " placed.");
				logArr.push("Time elapsed: " + duration);
				logArr.push("=========================================================");

				if (logErrArr.length > 0) {
					logArr.push("ERRORS:");
					logArr = logArr.concat(logErrArr);
				}
			
				logArr.push("\n");
				
				var logTxt = logArr.join("\r");
				Log(logTxt, logFilePath, false);
			}

			if (!debug) {
				alert(report, scriptName, false);
			}
			else {
				$.writeln(report);
			}
		}
	}
	catch(err) {
		if (debug) $.writeln(err.message + ", line: " + err.line);
		alert("Critical error: " + err.message + ", line: " + err.line, scriptName, true);
	}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

@Bridgette.Is.Enthusiastic

 

Can you confirm that it is working at all? 

 

Does it work for some found results and not for others? 

 

Because I don't think the new version should work at all... 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

the script only works when it doesn't encounter any images that are missing the metadata. if it's all there it runs perfectly and places all the images and captions the way I want them to.

 

If the script runs on something where the image is missing the description data, then instead of placing the text "Description not found in metadata" then it crashes the script with the error "undefined is not an object" for the same line as before (now just in a different place)

 

same error occured before I moved that section around and that script looked like this (also see attached):

 

			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
					try {

						//get XMP Description
						var imgDescription = imgPath.linkXmp.description;
			
						//add caption contents from XMP Description
						imgCaption.insertionPoints[-1].contents = "\r" + imgDescription;
					}

					catch (err) {

						//placeholder if no metadata found
						imgCaption.insertionPoints[-1].contents = "\r Description not found in metadata";
					}
					
					//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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2024 Jun 29, 2024

Copy link to clipboard

Copied

@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? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2024 Jun 30, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2024 Jun 30, 2024

Copy link to clipboard

Copied

quote

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


By @Peter Kahrel

 

Thanks, but I think @Kasyan Servetsky would be better? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2024 Jun 30, 2024

Copy link to clipboard

Copied

LATEST

Absolutely, haha! Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines