Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post Reply
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

I'm having a hell of a time with scripting for InDesign and need some assistance. I'm using the InDesign configurator and passing a command script with no arguments.

With the script I'm writing, I want it to open an InDesign document (.indd), remove the items from the pasteboard and re-link any missing images, then after that executes, the InDesign configurator should be able to pass the document and export a PDF. I've got the script working so that it removes the pasteboard items and I could even technically export a PDF from within the script but the person who designed this flow (I'm just doing the scripting part for this) wants the Configurator to pass or fail the InDesign file. The problem is I get the following error every time:

Code: Select all

Script returned error: ERROR: ImagesFail: Unavailable images were found in the document
I have a feeling it has to do with how Switch moves jobs along and places them in folders and I just haven't worked out how to perhaps drop the job in a specific folder and pull the document from there, work on it, and then push it through the configurator to pass or fail it.

Here is the script I have (below) and I would greatly appreciate any insight on how to improve this and/or what I could do differently to get the result I want.

Code: Select all

// Open the document in InDesign
var switchFile = File($infile);
var curDoc = app.open( switchFile );

// Functions to remove the pasteboard items that aren't
// needed any longer.
function removePasteboardItems(/*?Document*/doc)
//--------------------------------------
{
    if( !doc ) return;
 
    var items = doc.pageItems.everyItem().getElements(),
        t = null;
 
    while( t=items.pop() ) t.parentPage || removeItem(t);
 
    t = items = null;
}

function removeItem(/*PageItem*/item)
//--------------------------------------
{
    try {
        item.locked = false;
        item.remove();
        }
    catch(_){}
}

// Running the function and pushing through the document via app
removePasteboardItems(app.documents.length && app.activeDocument);

// Save the InDesign file to see which location the document is looking for links in
//app.activeDocument.save(File("/c/tmp/New.indd"));

// Function to replace/restore links in the document that are missing or not available
// Does not seem to be working...
function checkDoc(doc) {
	var fts = doc.fonts;
	var lks = doc.links;
	var missingFonts = [];
	var missingLinks = [];
	var n = fts.length;
	var msg = "";
	while (n--) {
		if ( /SUBSTITUTED|NOT_AVAILABLE/.test ( fts[n].properties.status.toString() ) ){
			missingFonts [ missingFonts.length ] = fts[n].name.replace ( /\t+/g, " ");
		}
	}

	n = lks.length;
	
	while (n--) {
		if ( /LINK_MISSING|LINK_INACCESSIBLE/.test ( lks[n].properties.status.toString() ) ) {
			missingLinks[ missingLinks.length ] = lks[n].name;
		}
	}
	
	if ( missingFonts.length ) msg+= missingFonts.length+" missing fonts ["+missingFonts.join()+"]. ";
	if ( missingLinks.length ) msg+= missingLinks.length+" missing links ["+missingLinks.join()+"]. ";
	
	return ( missingFonts.length || missingLinks.length )? msg : "";
}

// Checking the active $doc
var c = checkDoc($doc);
c!="" && $error = c;

//Close the document and save changes
curDoc.close(SaveOptions.NO)
I have the SaveOptions set to NO because when I do YES I get the error:

Code: Select all

Script returned error: ERROR: User canceled this action.

Is there any way to process the document as mentioned above and have it pass the Configurator tests for "Fail jobs with unavailable fonts" and "Fail jobs with unavailable images"? Or, if I use the script above and export a PDF, would that result in a PDF that contains all of the information in the trim/bleed area?
Last edited by kwoodVisions on Thu Apr 18, 2019 3:58 pm, edited 1 time in total.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by jan_suhr »

Is there any special reason this has to be done in InDesign, if the document is a PDF you can remove all objects outside of the bleed or media box with an Action list in PitStop
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by freddyp »

You should read the "Javascript for applications" part of the Switch documentation. There it says what variables you MUST use. And because you are not using them, ...

Also note that you can specify a script for three steps of the process: a script for opening the file (which you do not need to do when it is an Indesign file), a script for processing the opened file (that is where your script comes in), and a script for saving the file (again something you should not worry about).
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

jan_suhr wrote: Wed Apr 17, 2019 6:57 am Is there any special reason this has to be done in InDesign, if the document is a PDF you can remove all objects outside of the bleed or media box with an Action list in PitStop
The document coming in is an InDesign file (.indd) - we get files from our customers and I'm scripting the process a user is currently doing which is to remove all of the items from the pasteboard and then export the document as a print-ready PDF.
freddyp wrote: Wed Apr 17, 2019 8:38 am You should read the "Javascript for applications" part of the Switch documentation. There it says what variables you MUST use. And because you are not using them, ...

Also note that you can specify a script for three steps of the process: a script for opening the file (which you do not need to do when it is an Indesign file), a script for processing the opened file (that is where your script comes in), and a script for saving the file (again something you should not worry about).
I believe you're referencing this link: Javascript for applications -- even if I change doc to $doc, it doesn't work. In addition, the open portion that's supposed to happen automatically doesn't seem to happen because if I comment out lines 2 and 3 and run the flow, I get an error of:

Code: Select all

Script returned error: ERROR: Object is invalid

Here is the edited code ... Can you offer any suggestions on what else I should be changing?

Code: Select all

// Open the document in InDesign
var switchFile = File($infile);
var curDoc = app.open( switchFile );

// Functions to remove the pasteboard items that aren't
// needed any longer.
function removePasteboardItems(/*?Document*/$doc)
//--------------------------------------
{
    if( !$doc ) return;
 
    var items = $doc.pageItems.everyItem().getElements(),
        t = null;
 
    while( t=items.pop() ) t.parentPage || removeItem(t);
 
    t = items = null;
}

function removeItem(/*PageItem*/item)
//--------------------------------------
{
    try {
        item.locked = false;
        item.remove();
        }
    catch(_){}
}

// Running the function and pushing through the document via app
removePasteboardItems(app.documents.length && app.activeDocument);

// Save the InDesign file to see which location the document is looking for links in
//app.activeDocument.save(File("/c/tmp/New.indd"));

// Function to replace/restore links in the document that are missing or not available
// Does not seem to be working...
function checkDoc($doc) {
	var fts = $doc.fonts;
	var lks = $doc.links;
	var missingFonts = [];
	var missingLinks = [];
	var n = fts.length;
	var msg = "";
	while (n--) {
		if ( /SUBSTITUTED|NOT_AVAILABLE/.test ( fts[n].properties.status.toString() ) ){
			missingFonts [ missingFonts.length ] = fts[n].name.replace ( /\t+/g, " ");
		}
	}

	n = lks.length;
	
	while (n--) {
		if ( /LINK_MISSING|LINK_INACCESSIBLE/.test ( lks[n].properties.status.toString() ) ) {
			missingLinks[ missingLinks.length ] = lks[n].name;
		}
	}
	
	if ( missingFonts.length ) msg+= missingFonts.length+" missing fonts ["+missingFonts.join()+"]. ";
	if ( missingLinks.length ) msg+= missingLinks.length+" missing links ["+missingLinks.join()+"]. ";
	
	return ( missingFonts.length || missingLinks.length )? msg : "";
}

// Checking the active $doc
var c = checkDoc($doc);
c!="" && $error = c;

app.activeDocument.exportFile(ExportFormat.pdfType, File("/c/tmp/HoldFolder/testDocument.pdf"), false);

//Close the document and save changes
//curDoc.close(SaveOptions.YES);
app.activeDocument.close(SaveOptions.NO);
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

I've updated my script further and get the following error which makes sense but I don't know how to point it in the direction of the Links folder since Switch moves the job around:

Code: Select all

Script returned error: ERROR: 1 missing links [shutterstock_20904106.jpg].
This is the image used in the main section of the InDesign file however if I save the InDesign file and reopen it in InDesign, the pasteboard items are gone and the image I would expect is still on screen, along with text, but it says the link to the image is gone.

Here is my updated code:

Code: Select all

// Open the document in InDesign
var switchFile = File($infile);
var curDoc = app.open( switchFile );

// Functions to remove the pasteboard items that aren't
// needed any longer.
function removePasteboardItems(/*?Document*/$doc)
//--------------------------------------
{
    if( !$doc ) return;
 
    var items = $doc.pageItems.everyItem().getElements(),
        t = null;
 
    while( t=items.pop() ) t.parentPage || removeItem(t);
 
    t = items = null;
}

function removeItem(/*PageItem*/item)
//--------------------------------------
{
    try {
        item.locked = false;
        item.remove();
        }
    catch(_){}
}

// Running the function and pushing through the document via app
//removePasteboardItems(app.documents.length && app.activeDocument);
removePasteboardItems($doc);

// Save the InDesign file to see which location the document is looking for links in
app.activeDocument.save(File("/c/tmp/New.indd"));

// Function to replace/restore links in the document that are missing or not available
// Does not seem to be working...
function checkDoc($doc) {
	var fts = $doc.fonts;
	var lks = $doc.links;
	var missingFonts = [];
	var missingLinks = [];
	var n = fts.length;
	var msg = "";
	while (n--) {
		if ( /SUBSTITUTED|NOT_AVAILABLE/.test ( fts[n].properties.status.toString() ) ){
			missingFonts [ missingFonts.length ] = fts[n].name.replace ( /\t+/g, " ");
		}
	}

	n = lks.length;
	
	while (n--) {
		if ( /LINK_MISSING|LINK_INACCESSIBLE/.test ( lks[n].properties.status.toString() ) ) {
			missingLinks[ missingLinks.length ] = lks[n].name;
		}
	}
	
	if ( missingFonts.length ) msg+= missingFonts.length+" missing fonts ["+missingFonts.join()+"]. ";
	if ( missingLinks.length ) msg+= missingLinks.length+" missing links ["+missingLinks.join()+"]. ";
	
	return ( missingFonts.length || missingLinks.length )? msg : "";
}

// Checking the active $doc
var c = checkDoc($doc);
c!="" && $error = c;

app.activeDocument.exportFile(ExportFormat.pdfType, File("/c/tmp/HoldFolder/testDocument.pdf"), false);

//Close the document and save changes
//curDoc.close(SaveOptions.YES);
app.activeDocument.close(SaveOptions.NO);
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by jan_suhr »

kwoodVisions wrote: Wed Apr 17, 2019 4:54 pm
jan_suhr wrote: Wed Apr 17, 2019 6:57 am Is there any special reason this has to be done in InDesign, if the document is a PDF you can remove all objects outside of the bleed or media box with an Action list in PitStop
The document coming in is an InDesign file (.indd) - we get files from our customers and I'm scripting the process a user is currently doing which is to remove all of the items from the pasteboard and then export the document as a print-ready PDF.
That would be a lot easier to just use Switch and the InDesign-configurator to simply save as PDF without any scripting and then use the Action list in PitStop Server to remove everything outside the Mediabox. No scripting required and a lot easier to maintain.

With Switch you have to think a step further and not use it to do the same thing as you do manually. Switch is a lot smarter and have a lot of smart tools that can give you the same result but done in a different way that you do the manual way.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

jan_suhr wrote: Wed Apr 17, 2019 5:35 pm
kwoodVisions wrote: Wed Apr 17, 2019 4:54 pm
jan_suhr wrote: Wed Apr 17, 2019 6:57 am Is there any special reason this has to be done in InDesign, if the document is a PDF you can remove all objects outside of the bleed or media box with an Action list in PitStop
The document coming in is an InDesign file (.indd) - we get files from our customers and I'm scripting the process a user is currently doing which is to remove all of the items from the pasteboard and then export the document as a print-ready PDF.
That would be a lot easier to just use Switch and the InDesign-configurator to simply save as PDF without any scripting and then use the Action list in PitStop Server to remove everything outside the Mediabox. No scripting required and a lot easier to maintain.

With Switch you have to think a step further and not use it to do the same thing as you do manually. Switch is a lot smarter and have a lot of smart tools that can give you the same result but done in a different way that you do the manual way.


Right now, the configurator's open action is set to Automatic so it automatically opens the InDesign file and the "Save as" option is set to "Adobe PDF".

The problem I'm having is with the Command script -- what you told me to do is what I've been trying to do all along and I am running into problems. For the command script, I have it removing items from the pasteboard but when it does that, it loses all of it's links and then fails the configurator when I have "Fail jobs with unavailable fonts" and "Fail jobs with unavailable images" set to Yes.

That is, I'm already trying to do what you're saying and it's failing, hence the reason I'm asking for help.

My script used to only contain this:

Code: Select all

// Open the document in InDesign
var switchFile = File($infile);
var curDoc = app.open( switchFile );

// Functions to remove the pasteboard items that aren't
// needed any longer.
function removePasteboardItems(/*?Document*/$doc)
//--------------------------------------
{
    if( !$doc ) return;
 
    var items = $doc.pageItems.everyItem().getElements(),
        t = null;
 
    while( t=items.pop() ) t.parentPage || removeItem(t);
 
    t = items = null;
}

function removeItem(/*PageItem*/item)
//--------------------------------------
{
    try {
        item.locked = false;
        item.remove();
        }
    catch(_){}
}

// Running the function and pushing through the document via app
//removePasteboardItems(app.documents.length && app.activeDocument);
removePasteboardItems($doc);
Which successfully removes the pasteboard items however it FAILS the configurator test because it thinks it's missing links, and that is why I added the code below that section to try and relink missing links but I'm running into trouble with it.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by jan_suhr »

I don't see that you running PitStop Server after InDesign and then in PitStop Server running an Action list that removes everything outside the media box.

You don't need some tricky scripting to solve this task!

Try this one:
Remove everything outside mediabox.eal.zip
(1.75 KiB) Downloaded 688 times
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

jan_suhr wrote: Wed Apr 17, 2019 5:58 pm I don't see that you running PitStop Server after InDesign and then in PitStop Server running an Action list that removes everything outside the media box.

You don't need some tricky scripting to solve this task!

Try this one:

Remove everything outside mediabox.eal.zip
How would I set that up then? As it is right now, if I have a folder come into the Adobe InDesign Configurator and open the InDesign file, when it goes to save the PDF it FAILS because it is missing images...... It can't find the links because the folder moves along the path in Switch and changes folders.




I really just need to know how to relink the graphics correctly and save the file. What you told me does not work -- I've tried putting PitStop Server after the InDesign configurator and it gives me an error of:

Code: Select all

This configurator accepts only files. Please re-design the workflow
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by jan_suhr »

From the error message you are sending a folder to the flow. Indesign needs a .indd-file and that will become a PDF-file, not a folder.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kwoodVisions »

jan_suhr wrote: Wed Apr 17, 2019 7:19 pm From the error message you are sending a folder to the flow. Indesign needs a .indd-file and that will become a PDF-file, not a folder.
That error was when I tried to send an InDesign file to the PitStop Server element.

I tried again, manually copying an InDesign file into the folder before the PitStop Server element and got this error:

Code: Select all

The PitStop Server CLI gave an unknown error code: -2
User avatar
Terkelsen
Advanced member
Posts: 297
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by Terkelsen »

I fully agree with Jan Suhr that this would be so much easier to do in Pitstop. However, you will need at PDF to send to PitStop Server. It will never accept an indd-file as input. All you need is to use the InDesign configurator to create a PDF that will then be sent to PitStop.

The InDesign configurator has the property option "Re-link all graphics". InDesign will of course need access to the images to do that, but a script will need that as well.

If you have the possibility it would be easier to generate an InDesign package and send that to Switch. In that case the images will be in the package and updating the graphics will be no problem.
Sean IO
Newbie
Posts: 18
Joined: Mon Dec 03, 2018 7:58 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by Sean IO »

If I understand this correctly, your InDesign configurator is failing because the pasteboard items links are missing. This will be the case if the your client supplied an InDesign package, as when the package is collected it ignores items on the pasteboard.

Try changing your InDesign Configurator to save as InDesign instead of PDF. It will save your InDesign file without pasteboard items. Then add another InDesign configurator that saves a PDF. If that passes then you have your print ready PDF.
kiran sahu
Newbie
Posts: 2
Joined: Mon May 20, 2019 1:57 pm

Re: Scripting with InDesign - Remove Pasteboard & Re-Link Files

Post by kiran sahu »

That would be significantly simpler to simply utilize Switch and the InDesign-configurator to just spare as PDF with no scripting and afterward utilize the Activity list in PitStop Server to expel everything outside the Mediabox. No scripting required and significantly simpler to keep up.

With Change you need to think above and beyond and not utilize it to do a similar thing as you do physically. Switch is much more astute and have a great deal of savvy apparatuses that can give you a similar outcome however done in an alternate manner that you do the manual way.
Programing Languages for 2020
Post Reply