"Invalid Parameter" Error on Modified Script that Replaces Text Tags with Graphics
Trying this again because I think my last post was way too overwhelming and asking for too many things. I will keep this post brief and provide details in the comments.
I modified a script created by Kasyan (found here) to serve the purposes of our team. The major changes were made to how the script selects the image files, and additions that further formatted the graphic once it was placed.
This morning I tried to run the script on a new doc to show someone that it was working (because it was!) and now it is giving the error "critical error: Invalid parameter., line:110" and not running.
The meat of the script is below, and I can pull from the full script if you need to see another part of it. Line:110 is at the end- it's the section that groups the image and caption frames.
The full script is significantly longer, but I am not worried that errors are found there since it was written by Kasyan who is a subject matter expert and I didn't make any edits to the majority of the script.
function Main() {
try {
var foundItem, file, text, img, container, gb, width, height,
imgFile, imgFiles, figureTagID, story, insPtIndex, pageNumber,
progressWin, progressBar, progressTxt,
imgsFolder = (debug) ? new Folder("~/Desktop/Pictures") : Folder.selectDialog("Choose a folder with images"),
startTime = new Date(),
count = 0;
var progressWin = new Window("window", scriptName);
var progressBar = progressWin.add("progressbar" , undefined, 0, foundItems.length);
progressBar.preferredSize.width = 400;
var progressTxt = progressWin.add("statictext", undefined, "Starting processing classes");
progressTxt.alignment = "fill";
progressWin.show();
if (log) logArr.push("Date & time: " + GetDate() + "\n");
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) + ": " + figureTagID + "\tpage: " + ((pageNumber != null) ? pageNumber : "N/A"));
//tried to add on: + "\tfile placed: " + imgFile.displayName .........to the end of this log but it kept throwing the error "undefined is not an object"
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);
progressBar.value = (i + 1);
progressTxt.text = figureTagID;
if (imgFile != null) {
foundItem.remove();
story.recompose();
container = story.insertionPoints[insPtIndex].rectangles.add({strokeWeight: 0});
container.clearObjectStyleOverrides ()
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 ];
img = container.place(imgFile)[0];
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);
container.anchoredObjectSettings.releaseAnchoredObject() //this needs to happen so the captions can be grouped with the image
currentStyle=container.appliedObjectStyle
}
}
imgCaption = container.parentPage.textFrames.add();
//set position and size of the caption
imgCaption.geometricBounds = [container.geometricBounds[2] + .75,
container.geometricBounds[1],
container.geometricBounds[2] + .125,
container.geometricBounds[3]];
//add placeholder contents
imgCaption.contents = imgFile.displayName
//group image and caption
var finalFigure = doc.groups.add([container, imgCaption]);
//re-anchor the grouped figure and apply current object style
finalFigure.anchoredObjectSettings.insertAnchoredObject(foundItem.insertionPoints[0]);
finalFigure.applyObjectStyle(currentStyle)
finalFigure.clearObjectStyleOverrides ()
count++;
}
else {
logErrArr.push(figureTagID + " - the file doesn't exist." + " - page " + ((pageNumber != null) ? pageNumber : "N/A"));
}
} // end for
