Friday, 30 January 2015

Adobe InDesign Script UI window



// Dialog: A dialog remains visible as long as you interact with it, and while it is visible you can’t do anything else in InDesign.
var myWindow = new Window ("dialog", "Form");
myWindow.show ( );

// Palette: Palette is displayed on the screen you can continue to work in InDesign.
var myWindow = new Window ("palette", "Form");
myWindow.show ( );

// View Orientation
myWindow.orientation = "row"; // Window's default orientation is column

// Align
myWindow.alignChildren = "top";

// Groups and Panels
we can group items together using the ScriptUI items group and panel. These two function the same in that they group items together, but differ in two ways: panels have a border, groups don't; and the default orientation of a group is row, that of a panel, column. Groups (and panels) are good layout tools when you script windows.
Group
var myInputGroup = myWindow.add ("group");
myInputGroup.add ("statictext", undefined, "Name:");

myInputGroup.alignment = "right"; //aligned the group to the right of the window using alignment

 // StaticText / Label 

var myMessage = myWindow.add ("statictext", undefined, "Hello, world!");
                     or
 var myMessage = myWindow.add ("statictext");
 myMessage.text = "Hello, world!";

myText.characters = 30; //set the width of an edittext control using its characters property

myText.active = true; //edit field were activated when the window is displayed so that the user needn't place the cursor there.

// EditText / Inputbox
var myText = myWindow.add ("edittext");
                     or
var myText = myWindow.add ("edittext", undefined, "John");



// Button
myWindow.add ("button", undefined, "OK");


//How to deal with the user's input and how to use that input in the rest of the script. In this example, two things can happen: the user clicks OK (which in this script corresponds to pressing Enter) or they can click
Cancel (which is the equivalent of pressing Escape). The window's behaviour is this: if the user presses OK, the line myWindow.show ( ) returns 1, if they press Esc, that line returns 2. We capture this as follows:
if (myWindow.show () == 1)
         var myName = myText.text;
else
         exit ();
Example: 
var myName = myInput ();
// rest of the script
function myInput ()
{
         var myWindow = new Window ("dialog", "Form");
         var myInputGroup = myWindow.add ("group");
         myInputGroup.add ("statictext", undefined, "Name:");
                  var myText = myInputGroup.add ("edittext", undefined, "John");
                  myText.characters = 20;
                  myText.active = true;
         var myButtonGroup = myWindow.add ("group");
                  myButtonGroup.alignment = "right";
                  myButtonGroup.add ("button", undefined, "OK");
                  myButtonGroup.add ("button", undefined, "Cancel");
         if (myWindow.show () == 1)
                  return myText.text;
         else
                  exit ();
}

Basic Document Management Adobe InDesign Script

Create
New document
var myDocument = app.documents.add();

Creates a new document using the specified document preset
var myDocument = app.documents.add(true,app.documentPresets.item("myDocumentPreset"));

Creates a new document without showing the document window.
//The first parameter (showingWindow) controls the visibility of the
//document. Hidden documents are not minimized, and will not appear until
//you add a new window to the document.
var myDocument = app.documents.add(false);
//To show the window:
var myWindow = myDocument.windows.add();

Open
OpenDocument.
app.open(File("/c/myTestDocument.indd"));

Opens an existing document in the background,
var myDocument = app.open(File("/c/myTestDocument.indd"), false);

When you want to show the hidden document, create a new window.
var myLayoutWindow = myDocument.windows.add();

Save
If the active document has been changed since it was last saved, save it.
if(app.activeDocument.modified == true){
      app.activeDocument.save();
}

If the active document has not been saved (ever), save it.
if(app.activeDocument.saved == false){
      //If you do not provide a file name, InDesign displays the Save dialog box.
      app.activeDocument.save(new File("/c/myTestDocument.indd"));
}

Save the active document as a template.
var myFileName;
if(app.activeDocument.saved == true){
      //Convert the file name to a string.
      myFileName = app.activeDocument.fullName + "";
      //If the file name contains the extension ".indd", change it to ".indt".
      if(myFileName.indexOf(".indd")!=-1){
            var myRegularExpression = /.indd/gi
            myFileName = myFileName.replace(myRegularExpression, ".indt");
      }
}
//If the document has not been saved, then give it a default file name/file path.
else{
myFileName = "/c/myTestDocument.indt";
}
app.activeDocument.save(File(myFileName), true);

Close
CloseDocument
app.activeDocument.close();
//Note that you could also use:
//app.documents.item(0).close();

Close document with saving
//Use SaveOptions.yes to save the document,SaveOptions.no to close the
//document without saving, or SaveOptions.ask to display a prompt. If
//you use SaveOptions.yes, you'll need to provide a reference to a file
//to save to in the second parameter (SavingIn).
//Note that the file path is provided using the JavaScript URI form
//rather than the platform-specific form.
//If the file has not been saved, display a prompt.
if(app.activeDocument.saved != true)
{
      app.activeDocument.close(SaveOptions.ask);
      //Or, to save to a specific file name:
      //var myFile = File("/c/myTestDocument.indd");
      //app.activeDocument.close(SaveOptions.yes, myFile);
}
Else
{
      //If the file has already been saved, save it.
      app.activeDocument.close(SaveOptions.yes);
}

Close all open documents without saving
for(myCounter = app.documents.length; myCounter > 0; myCounter--){
      app.documents.item(myCounter-1).close(SaveOptions.no);
}