Join Keith Gilbert for an in-depth discussion in this video Write the body of the script, part of InDesign Scripting Made Easy.
- Now we can begin writing the part of the script that does the actual work. To begin, open 06_02 Setup_begin in the toolkit, and we're going to begin by saving that with a new name, so choose File, Save As, and just call it Setup. And the first thing we want the script to do is to display the rulers on the screen. The equivalent of choosing Show Rulers from InDesign's view menu. Now even when a command that you're trying to script appears in a straightforward fashion in a menu, you typically don't access the command that way in a script.
There's no macro record command that will transform menu commands or keyboard shortcuts into the correct script syntax, I wish there was. So what do we do? Well we try to find the command within the InDesign object model. We're going to use the object model that's browser-based, that we discussed in an earlier movie, so go to the URL that's displayed on your screen, and click on the Search tab and type in rulers for the search term, and click on the Search button.
And this ViewPreference hit that comes up looks promising, so click on that, and sure enough as you scroll through the right-hand side, you'll see that there's something called showRulers that's highlighted. And indeed showRulers is a property that it says if true, this displays the horizontal and vertical rulers. Well that's exactly what we want. So at this point I would jot down this showRulers command so that we can type it in later, and if you scroll to the bottom of the screen on the right-hand side, you'll see that showRulers is an Object of both the Application viewPreferences and the Document viewPreferences.
That's because, as you're probably aware, preferences in InDesign sometimes apply to the entire document, and sometimes they can also apply just to a specific document. We're going to use the Document viewPreferences in this script. While we're here looking at ViewPreferences, we might want to see if there's something else that might be useful for us, and sure enough there's something called showFrameEdges. And it tells us here that if this is true it displays borders of unselected frames and the diagonal lines in empty unselected frames, that's exactly what we want.
So go ahead and jot down showFrameEdges also. I also want to set the measurement units with this script. So as you scroll through here, sure enough, there's something called horizontalMeasurementUnits. Now this property doesn't have just a simple true or false value, this property can be assigned any of these values here, and we want to set our measurement systems to PICAS, so you're going to want to copy or jot down this MeasurementUnits.PICAS terminology here.
Once you've got all that collected and written somewhere or copied and pasted somewhere, return back to the toolkit and we're ready to begin typing some of that in here. So select line 18, and we're going to replace that comment with myDoc, now myDoc refers to the variable in line 17 that is a stand-in or has been assigned the active documents. So myDoc.viewPreferences.showRulers = true and then a return, and you probably want to copy and paste myDoc.viewPreferences so you don't have to keep on typing that unless you really enjoy typing.
And put in myDoc.viewPreferences.showFrameEdges = true and then paste in myDoc.viewPreferences again and you want horizontalMeasurementUnits = MeasurementUnits.PICAS And then you probably want to select that entire line, copy that, paste it on the next line, and change horizontal to vertical.
And then just check to make sure your capitalization is exactly like mine, that you get the equal sign in the right place and the semicolon, things like that. Notice that this capitalization is really picky and it's not entirely consistent, for example notice that after the periods here we've always got lowercase characters, but in this case there's an uppercase M on MeasurementUnits, and the word PICAS is all uppercase. Doesn't seem very consistent but that is the way it has to be, it's very very picky.
The last thing we want to do here is we want to include some commands for displaying guides. For example, we want to automate the equivalent of choosing grids and guides from the View menu in InDesign. So back in the object model viewer, I'm going to search for the word guides, and scroll through the results here, and sure enough there's something called GuidePreferences. And I'm going to click on GuidePreferences, and we can see that there's something called guidesShown.
Now how did I know to go to GuidePreferences instead of some of these other hits that the word guide is found within? Well that's the tricky part, that sometimes just takes a lot of sleuthing around until you find what you're looking for. The more that you do this the more you'll become familiar with the structure of the object model and will be able to find things a little quicker. So we want guidesShown so jot that down, and then we also want guidesSnapto, so jot that down.
Then return back to the toolkit, and you're going to type in myDoc.guidePreferences.guidesShown = true Notice that true is not blue here because I spelled it wrong, so that caught my eye, so I'm going to spell it correctly and it immediately turns blue, that's so helpful. Then I'm going to copy myDoc.guidePreferences, paste it in and type in myDoc.guidePreferences.guidesSnapto = true and there we have it.
Double- triple-check your script to make sure it looks exactly like mine, save it, and return to InDesign. In InDesign, open the Globe Bank_Small Biz Magazine.indd file, and take a few moments to hide the rulers, hide the frame edges, and hide the guides, and then run your Setup script that you just saved. When you run the script-- well I just got an error, look at that.
Teachable moment, happy to see that. So I got an error, it didn't work, what are we going to do? Well hopefully in this error it tells me that the problem is on line 23, and it was fumble fingers here, I typed in guideShapto instead of Snapto. So I'm going to OK out of that, I'm going to return back to the toolkit, and I'm going to fix the Shapto and put in Snapto. Save it again, return to InDesign, and try running the script, and sure enough this time it finished, it displayed the rulers, it displayed the guides, it displayed the frame edges, all is good.
If your script doesn't run successfully, compare it carefully to my script, and if you're still unsuccessful, no worries, in the next movie we'll learn some troubleshooting techniques. Scripting is picky, you have to locate just the right commands in the object model, and you have to type them precisely with the capitalization just so, but it's fun to see a script you've written begin to automate InDesign.
- What are InDesign scripts?
- Installing scripts
- Editing scripts
- Referencing InDesign objects in scripts
- Creating a script to set preferences
- Creating a script to watermark designs
- Creating a script to transpose characters