A little time ago we've made an open source class library, Adam.JSGenerator, available for use. It's not only easy for
snippets interacting with client side components, but the Adobe ExtendScript (an
integrated development environment for the creation and debugging of JavaScript
code) in DocMaker for communicating with InDesign Server is also generated with
the Adam.JSGenerator.
This article will show some extra examples of the JSGenerator used for DocMaker
and InDesign Scripting. For each example we will show three snippets: the ExtendScript
itself, the C# code without the generator and the generator enabled C# code.
Simple operation
We'll start by something really simple. Setting the fontSize of a story. (An
InDesign Story is the contents of a textframe on a page).
Script
| JavaScript |
1
|
app.stories.item(0).pointSize = 12;
|
Without
| C# |
1
2
3
4
5
|
int fontSize = 12;
StringBuilder script = new StringBuilder();
script.AppendFormat(
"app.stories.item(0).pointSize = {0};", fontSize);
return script.ToString();
|
With
| C# |
1
2
|
return JS.Id("app").Dot("stories").Dot("item")
.Call(0).Dot("pointSize").AssignWith(fontSize).ToString();
|
Documents
Following example consists of two operations. Opening and saving a document.
Script
| JavaScript |
1
2
3
|
var myDocument = app.open("//serverName/share/BrandbookCS5.indd");
var pathToSave = "//serverName/share/BrandbookCS5Edited.indd";
myDocument.close(SaveOptions.YES, pathToSave);
|
Without
| C# |
1
2
3
4
5
6
7
8
|
StringBuilder script = new StringBuilder();
script.AppendFormat(
"var myDocument = app.open(\"{0}\");\n", document.Path);
script.AppendLine(
"var pathToSave = \"//serverName/share/Brandbook_CS5.indd\";");
script.AppendLine(
"myDocument.close(SaveOptions.YES, pathToSave);");
return script.ToString();
|
With
| C# |
1
2
3
4
5
6
7
8
9
|
var myDocument = JS.Id("myDocument");
var pathToSave = JS.Id("pathToSave");
var item1 = JS.Var(myDocument.AssignWith(
JS.Id("app").Dot("open").Call(document.Path)));
var item2 = JS.Var(pathToSave.AssignWith(
"//serverName/share/Brandbook_CS5.indd"));
var item3 = myDocument.Dot("close").Call(
JS.Id("SaveOptions").Dot("YES"), pathToSave);
return JS.Script(item1, item2, item3).ToString();
|
Updating a textColor
The last example is a bit more complex. The script tries to load a document color.
When the color does not exist it is created. When the color is available it is applied
to the text of the story.
Script
| JavaScript |
1
2
3
4
5
6
7
8
9
10
11
|
try {
var myColor = document.colors.item("R=100 G=123 B=24");
var myName = myColor.name;
}
catch(e){
myColor = document.colors.add({ name:"R=100 G=123 B=24",
space:ColorSpace.RGB,
model:ColorModel.process,
colorValue:[100,123,24]});
}
myStory.fillColor = myColor;
|
Without
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// red green and blue are arguments passed to a function
// containing following code
string colorName = string.Format("R={0} G={1} B={2}", red, green, blue);
StringBuilder script = new StringBuilder();
script.AppendLine("try");
script.AppendLine("{");
script.AppendFormat(" var myColor =
document.colors.item(\"{0}\");", colorName);
script.AppendLine();
script.AppendLine(" var myName = myColor.name;");
script.AppendLine("}");
script.AppendLine("catch(e){");
script.AppendFormat(" myColor = document.colors.add(
{{name:\"{0}\",
space:ColorSpace.RGB,
model:ColorModel.process,
colorValue:[{1}, {2}, {3}]}});",
colorName, red, green, blue);
script.AppendLine();
script.AppendLine("}");
script.AppendLine("myStory.fillColor = myColor;");
return script.ToString();
|
With
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// red green and blue are arguments passed to a function
// containing following code
string colorName = string.Format("R={0} G={1} B={2}", red, green, blue);
var myColor = JS.Id("myColor");
var myColorValue = JS.Var(myColor.AssignWith(
JS.ParseId("document.colors.item").Call(colorName)));
var myName = JS.Var(JS.Id("myName").AssignWith(myColor.Dot("name")));
var colorObject = JS.Object(new
{
name = colorName,
space = JS.Id("ColorSpace").Dot("RGB"),
model = JS.Id("ColorModel").Dot("process"),
colorValue = new[] { red, green, blue }
});
var myColorCatch = myColor.AssignWith(
JS.Id("document").Dot("colors").Dot("add").Call(colorObject));
return JS.Script(JS.Try(myColorValue, myName).Catch("e", myColorCatch),
JS.Id("myStory").Dot("fillColor").AssignWith(myColor)).ToString();
|
Conclusion
Building Javascripts using the Adam.JSGenerator instead of concatinated strings
with converted variables is better, easier, faster and more maintainable. It may
seems a bit difficult at the start, but once you start using it, you'll notice the
difference.