JS script how do I correctly use try...catch() for error handling?
- July 1, 2024
- 1 reply
- 2099 views
Hello again! I made a similar post a few days ago that helped me solve one problem, but unfortunately it created another, so I'm back.
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: The script runs beautifully so long as it is able to find the metadata it is looking for. I have tried to use try...catch() to look for the linkXmp.description, but I think I fundementally don't understand what to put in the "catch" area. I want it to just tell the script that the description is null so I can later use "else" to put a placeholder in the caption text frame.
(This might not be the best way to handle this - the image files that are returning empty definitely have the metadata there when I look at them in Bridge. Would you recommend a different way of grabbing the metadata, or error handling if it can't be found?)
What I'm trying: here is the small snippet that is causing problems for me:
try {
//get XMP Description
imgDescription = imgPath.linkXmp.description;
}
catch (err){
if (debug) $.writeln(err.message + ", line: " + err.line);
}
//add caption contents from XMP Description
finally{
if (imgDescription != null){
captionInsPts.contents = "\r" + imgDescription;
}
else {
captionInsPts.contents = "\r Description metadata not available";
}
}
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 have attached the full script in case the problem cannot be found in the excerpt below:
if(imgFile != null) {
//remove figure tag from text
foundItem.remove();
story.recompose();
//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;
captionInsPts = imgCaption.insertionPoints[-1]
//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];
//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);
}
}
var imgPath = img.itemLink;
//find and place metadata as caption
try {
//get XMP Description
imgDescription = imgPath.linkXmp.description;
}
catch (err){
if (debug) $.writeln(err.message + ", line: " + err.line);
}
//add caption contents from XMP Description
finally{
if (imgDescription != null){
captionInsPts.contents = "\r" + imgDescription;
}
else {
captionInsPts.contents = "\r Description metadata not available";
}
}
//finalize styles
imgCaption.applyObjectStyle(currentStyle);
imgCaption.clearObjectStyleOverrides ();
container.applyObjectStyle(app.activeDocument.objectStyleGroups.itemByName("Figures"));
container.clearObjectStyleOverrides ();
count++;
}
else {
logErrArr.push(figureTagID + " - the file doesn't exist." + " - page " + ((pageNumber != null) ? pageNumber : "N/A"));
}
