Showcase and discover digital art at yex

Follow Design Stacks


What else can scripts do?

What else can scripts do?

Scripts can do just about anything that you can do using InDesign’s user interface, and a few things that you can’t do. Don’t think about scripting only in terms of automating tedious, repetitive tasks. Scripting’s good for that, but you can also use it to speed up the smaller tasks that drive you crazy during the course of the day. When you take a common task that involves some number of actions and replace it with a simple double-click or keystroke (all it takes to run a script), you reduce the difficulty and complexity of your work.

Like what? Here’s an example: it can be a chore tracking down the original graphics files you’ve placed in your document. Sure, you can use the Links palette to show you the file path to a graphic, but it’s quite another thing to find that location on disk. Below is a simple JavaScript that opens the folder containing the selected graphic in the Finder (on the Mac OS) or in Windows Explorer. You can copy this out and save it as a text file in your Scripts folder (make sure the file’s name ends with .js), or download it from ZIP FILE.

//Opens the folder containing the selected graphic.
//Assumes that the graphic is selected using the Direct Selection tool.

var myFilePath = app.selection[0].itemLink.filePath;myFilePath.parent.execute();

The above script will fail if you’ve selected the graphic with the Selection tool (rather than the Direct Selection tool). What if you want to select a graphic using the Selection tool, or select a series of graphics? To do that, we need to add a little bit more to the script. Something like this:

//An InDesign CS JavaScript
////This script opens the folder containing the selected linked file.
////Create a list–we’ll use it to store the qualifying graphics.
var myObjectList = new Array;if(app.documents.length != 0)
{ if(app.selection.length !=0){ for(var myCounter = 0; myCounter < app.selection.length; myCounter ++)
{ switch(app.selection[myCounter]
{ case “Rectangle”: case “Oval”: case “Polygon”: case “GraphicLine”: case “Image”: case “EPS”: case “PDF”:
//If a frame was selected with the Selection tool, and the frame
 //contains a graphic, then add the graphic to the list.
if(app.selection[myCounter].contentType == ContentType.graphicType){ myObjectList.push(app.selection[myCounter].graphics.item(0)); } break;
//If a graphic was selected with the Direct Selection tool,
//then add it to the list. default: myObjectList.push(app.selection[myCounter]); break; } } }
if(myObjectList.length != 0){ for(myCounter = 0; myCounter < myObjectList.length; myCounter++)
{ myOpenLinkFolder(myObjectList[myCounter]); } } }}function myOpenLinkFolder(myGraphic)
{ var myLink = myGraphic.itemLink; var myFilePath = myLink.filePath; myFilePath.parent.execute();}