Tutorial how to relink Indesign files and export them to pdf with actionscript

In my summer job I was asked to open hundred InDesign files and relink all assets to another location. I didn’t found any examples on the internet, so here’s mine. I hope this will be useful for someone…

Relink all InDesign files from a folder

It’s simple: open each file in the specified folder, check every link and change/update the link if┬ánecessary.

//disable interaction
app.scriptPreferences.userInteractionLevel = \
UserInteractionLevels.NEVER_INTERACT;
var myFolder = new Folder("C:\\InDesignFilees\\");
var myFileArr = myFolder.getFiles();
var fileNumber = 0;

function changeLinks()
{
    newFile = new File(myFileArr[fileNumber]);
    myDocument = app.open(File(myFileArr[fileNumber]));
    myLinks = myDocument.links;

    for ( i = 0; i < myLinks.length; i++)
    {
        var newLinkPath = "";
        if (myLinks[i].filePath == "C:\\SomeWhere\\asset.jpg")
        {
            newLinkPath = "C:\\CorrectPlace\\asset.jpg";
        }
        if (myLinks[i].filePath == "C:\\SomeWhere\\asset2.jpg")
        {
            newLinkPath = "C:\\CorrectPlace\\asset2.jpg";
        }

        if (newLinkPath!="")
        {
            newFile = new File(newLinkPath);
            myLinks[i].relink(newFile);
            myLinks[i].update();
        }
    }
    myDocument.close(SaveOptions.YES);
}
//enable interaction
app.scriptPreferences.userInteractionLevel = \
UserInteractionLevels.INTERACT_WITH_ALL;

Export all InDesign files from a folder

This code opens every file in a specified folder and export all files to another specified folder.

//disable interaction
app.scriptPreferences.userInteractionLevel = \
UserInteractionLevels.NEVER_INTERACT;

var myPDFExportPreset = \
app.pdfExportPresets.item("MagazineAds_1v3_IND4");
// you can use also "[Smallest File Size]"

var fileNumber = 0;
var myFolder = "";
var myFilePath = "";
var myFileArr = [];

function myExport()
{
	var newFile = new File(myFileArr[fileNumber]);
	var myDocument = app.open(File(myFileArr[fileNumber]));
	var name = (myDocument.name.split('.'))[0];
	myFile = new File(myFilePath + name + ".pdf");
	myDocument.exportFile(ExportFormat.pdfType, myFile, false, \
            myPDFExportPreset);
	myDocument.close(SaveOptions.YES);
}

myFolder = new Folder("C:\\InDesignFiles\\");
myFileArr = myFolder.getFiles();
myFilePath = "C:\\ExportFiles\\";
for (fileNumber = 0; fileNumber < myFileArr.length; fileNumber++){
    myExport();
}
//enable interaction
app.scriptPreferences.userInteractionLevel = \
UserInteractionLevels.INTERACT_WITH_ALL;

Leave a Reply

Your email address will not be published. Required fields are marked *