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

"Invalid Parameter" Error on Modified Script that Replaces Text Tags with Graphics

Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

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

 

TOPICS
Scripting

Views

1.0K

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

Explorer , Mar 02, 2023 Mar 02, 2023

This was the line that was throwing the error:

var finalFigure = doc.groups.add([container, imgCaption]);

I found the cause of the error- for some reason if a character style or certain object styles are selected in inDesign before I run the script, this line that groups the two main elements together to create the final figure doesn't want to work anymore. Right now the script works great, you just need to make sure the correct styles are selected before running it.

 

Thank you so much for your ins

...

Votes

Translate

Translate
Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

Original script created by Kasyan found here.

 

Our workflow involves copywriters placing a tag where scientific figures need to appear within a large document while those graphic files are still being edited and created by our designers. Since we have a strict naming convention, I switched up the script to find a file that starts with the found text, instead of having to exactly match the file name (since version control will most likely change the file names several times afterwards).

 

I then made several other modifactions related to layout and design so the placed file would more closely match the end design of our scientific figures. I un-anchored the graphic, added a text frame with a caption based on the file name (for QC and as a placeholder), grouped the caption and image, applied an object style to the group, and re-anchored the group in the same place.

 

Main Concern:

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.

 

Other Concerns:

Before this, the script ran great MOST of the time, but is still glitchy and has lots of different errors that pop up sometimes, the following are the most common issues- but all of these worked at one point in the process:

 

  1. I added a part to the log that lets you know what file replaced what found text (since they're not identical anymore) But it had to be commented out because it kept telling me that "undefined is not an object" which is probably related to:
  2. when the log addition I made does work- the last graphic it placed always said "undefined" in the log instead of the file name. Even when the rest of the script was successful, this never changed
  3. when the sizing parameters in the opening dialog are.... bad combos? (I can't figure out exactly what combos make that part break) it throws an error. This is my least concern right now because there are plenty of combos that DO work.

 

Overall my mods seemed to have made the code super glitchy and it is a very unhappy and grumpy script.

I would love some help troubleshooting these errors, and hopefully get some advice or insight on how to make the script more robust and generally happier if someone has the time and desire for that as well. 

 

Thank you all so much for your help! If I can get this script to work, then HOURS of finding and placing figure graphics will be saved by members of our team, and we can focus on editing the layout and design with everything we need right there in the document. 

 

The full script is attached if you would like to take a look at it. Again it's super long, because Kasyan made it do some pretty cool stuff!

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 ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

@Kasyan Servetsky you might be best to look at this one if you have time. @Bridgette.Is.Enthusiastic it would make things much quicker if you posted a simple document that included examples of glitchy behaviour that we could test against. Is that possible?

- Mark

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 ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

LATEST

Thank you so much! 

 

I might not be using the term "glitchy" correctly- when the script works, it works great and everything in the document looks fabulous. The problem is that certain settings seem to prevent the script from working at all. 

 

The "invalid parameter" error was because I had a character style selected before running the script. That seems like I've got something set up in a way that is absolutely not ideal. I'm glad it's working now, but I would love to have something that can work more dependably across multiple documents and styles.

 

And then this issue mentioned in my comment above is still a concern:

  1. when the sizing parameters in the opening dialog are.... bad combos? (I can't figure out exactly what combos make that part break) it throws an error. This is my least concern right now because there are plenty of combos that DO work.

 

And then lastly, and arguably most importantly, I would really really love to add the file name to the log since the "foundItem" isn't identical to it anymore, but it keeps saying that imgFile.displayName is "undefined" even though I use the same exact expression later in the script to create the caption placeholder text.

 

So I feel like I've made novice errors throughout the script that make it error prone and grumpy. I was curious if anyone had any tips for better practices I could be using for more robust error handling.

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
People's Champ ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

Without knowing the exact line #110 content on your side, it will be hard to help and yes it's likely@Kasyan Servetsky  will be the best person to help here.

For what's worth, Invalid parameter, is likely to be thrown by a native function call expecting data from a certain type that you either didn't supply or pass a wrong data type. So the starting point here would be to introspect the exact kind of arguments the method expects and see what you are passing as arguments.

FWIW

Loic

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 ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

This was the line that was throwing the error:

var finalFigure = doc.groups.add([container, imgCaption]);

I found the cause of the error- for some reason if a character style or certain object styles are selected in inDesign before I run the script, this line that groups the two main elements together to create the final figure doesn't want to work anymore. Right now the script works great, you just need to make sure the correct styles are selected before running it.

 

Thank you so much for your insight! I learned a ton modifying this script, and I still have so much more to understand. 

 

Now I'm focusing on why the other concerns don't work the way I'd expect them to. Now that the script is actually running 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