Tuesday, 10 March 2015

Adobe InDesign Scripting XML

 Setting XML preferences:

 Control the appearance of the InDesign structure panel using the XML view-preferences object:

var myDocument = app.documents.add();
var myXMLViewPreferences = myDocument.xmlViewPreferences;
myXMLViewPreferences.showAttributes = true;
myXMLViewPreferences.showStructure = true;
myXMLViewPreferences.showTaggedFrames = true;
myXMLViewPreferences.showTagMarkers = true;
myXMLViewPreferences.showTextSnippets = true;


Specify XML tagging preset preferences (the default tag names and user-interface colors for
tables and stories) using the XML preferences object:


var myDocument = app.documents.add();
var myXMLPreferences = myDocument.xmlPreferences;
myXMLPreferences.defaultCellTagColor = UIColors.blue;
myXMLPreferences.defaultCellTagName = "cell";
myXMLPreferences.defaultImageTagColor = UIColors.brickRed;
myXMLPreferences.defaultImageTagName = "image";
myXMLPreferences.defaultStoryTagColor = UIColors.charcoal;
myXMLPreferences.defaultStoryTagName = "text";
myXMLPreferences.defaultTableTagColor = UIColors.cuteTeal;
myXMLPreferences.defaultTableTagName = "table";



Setting XML import preferences:
Before importing an XML file, you can set XML import preferences that can apply an XSLT transform, govern the way white space in the XML file is handled, or create repeating text elements.

var myDocument = app.documents.add();
var myXMLImportPreferences = myDocument.xmlImportPreferences;
myXMLImportPreferences.allowTransform = false;
myXMLImportPreferences.createLinkToXML = false;
myXMLImportPreferences.ignoreUnmatchedIncoming = true;
myXMLImportPreferences.ignoreWhitespace = true;
myXMLImportPreferences.importCALSTables = true;
myXMLImportPreferences.importStyle = XMLImportStyles.mergeImport;
myXMLImportPreferences.importTextIntoTables = false;
myXMLImportPreferences.importToSelected = false;
myXMLImportPreferences.removeUnmatchedExisting = false;
myXMLImportPreferences.repeatTextElements = true;
//The following properties are only used when the
//AllowTransform property is set to True.
//myXMLImportPreferences.transformFilename = "c:\myTransform.xsl"
//If you have defined parameters in your XSL file, then you can pass
//parameters to the file during the XML import process. For each parameter,
//enter an array containing two strings. The first string is the name of the
//parameter, the second is the value of the parameter.
//myXMLImportPreferences.transformParameters = [["format", "1"]];


Importing XML:
Set the XML import preferences the way you want them, after that you can import an XML file.

myDocument.importXML(File("/c/xml_test.xml"));

XML file into a specific XML element

myXMLElement.importXML(File("/c/xml_test.xml"));

You also can set the importToSelected property of the xmlImportPreferences object to true, then
select the XML element, and then import the XML file

var myXMLTag = myDocument.xmlTags.add("xml_element");
var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag);
myDocument.select(myXMLElement);
myDocument.xmlImportPreferences.importToSelected = true;
//Import into the selected XML element.
myDocument.importXML(File("/c/xml_test.xml"));


Creating an XML tag:
XML tags are the names of the XML elements you want to create in a document. When you import XML, the element names in the XML file are added to the list of XML tags in the document. You also can create XML tags directly.

//You can create an XML tag without specifying a color for the tag.
var myXMLTagA = myDocument.xmlTags.add("XML_tag_A");
//You can define the highlight color of the XML tag using the UIColors enumeration...
var myXMLTagB = myDocument.xmlTags.add("XML_tag_B", UIColors.gray);
//...or you can provide an RGB array to set the color of the tag.
var myXMLTagC = myDocument.xmlTags.add("XML_tag_C", [0, 92, 128]);


Loading XML tags:
You can import XML tags from an XML file without importing the XML contents of the file. You might want to do this to work out a tag-to-style or style-to-tag mapping before you import the XML data.

myDocument.loadXMLTags(File("/c/test.xml"));

Saving XML tags:
You can save XML tags to a file. Only the tags themselves are saved in the XML file; document data is not included.

myDocument.saveXMLTags(File("/c/xml_tags.xml"), "Tag set created October 5, 2015");


Creating an XML element:

var myDocument = myInDesign.documents.add();
var myXMLTagA = myDocument.xmlTags.add("XML_tag_A");
var myXMLElementA = myDocument.xmlElements.item(0).xmlElements.add(myXMLTagA);
myXMLElementA.contents = "This is an XML element containing text.";

Moving an XML element:
You can move XML elements within the XML structure using the move method,

var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementA = myRootXMLElement.xmlElements.item(0);
myXMLElementA.move(LocationOptions.after, myRootXMLElement.xmlElements.item(2));
myRootXMLElement.xmlElements.item(-1).move(LocationOptions.atBeginning);


Deleting an XML element:
Deleting an XML element removes it from both the layout and the XML structure,

myRootXMLElement.xmlElements.item(0).remove();

Duplicating an XML element:
When you duplicate an XML element, the new XML element appears immediately after the original XML element in the XML structure

var myDocument = app.documents.item(0);
var myRootXMLElement = myDocument.xmlElements.item(0);
//Duplicate the XML element containing "A"
var myNewXMLElement = myRootXMLElement.xmlElements.item(0).duplicate();
//Change the content of the duplicated XML element.
myNewXMLElement.contents = myNewXMLElement.contents + " duplicate";


Removing items from the XML structure:
To break the association between a page item or text and an XML element, use the untag method, as
shown in the following script. The objects are not deleted, but they are no longer tied to an XML element (which is deleted). Any content of the deleted XML element becomes associated with the parent XML element. If the XML element is the root XML element, any layout objects (text or page items) associated with the XML element remain in the document.

var myXMLElement = myDocument.xmlElements.item(0).xmlElements.item(0);
myXMLElement.untag();


Creating an XML comment:
XML comments are used to make notes in XML data structures. You can add an XML comment using

var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementB = myRootXMLElement.xmlElements.item(1);
myXMLElementB.xmlComments.add("This is an XML comment.");


Creating an XML processing instruction:
A processing instruction (PI) is an XML element that contains directions for the application reading the XML document. XML processing instructions are ignored by InDesign but can be inserted in an InDesign XML structure for export to other applications. An XML document can contain multiple processing instructions. An XML processing instruction has two parts, target and value. The following is an example: <?xml-stylesheet type="text/css" href="generic.css"?>

var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLProcessingInstruction = myRootXMLElement.xmlInstructions.add("xml-stylesheet
type=\"text/css\" ", "href=\"generic.css\"");


Working with XML attributes:
XML attributes are “metadata” that can be associated with an XML element. An XML element can have any number of XML attributes, but each attribute name must be unique within the element (that is, you cannot have two attributes named “id”).

var myDocument = app.documents.item(0);
var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementB = myRootXMLElement.xmlElements.item(1);
myXMLElementB.xmlAttributes.add("example_attribute", "This is an XML attribute. It
will not appear in the layout!");

You can convert XML elements to attributes. When you do this, the text contents of the XML element become the value of an XML attribute added to the parent of the XML element. Because the name of the XML element becomes the name of the attribute, this method can fail when an attribute with that name already exists in the parent of the XML element. If the XML element contains page items, those page items are deleted from the layout. When you convert an XML attribute to an XML element, you can specify the location where the new XML element is added. The new XML element can be added to the beginning or end of the parent of the XML attribute. By default, the new element is added at the beginning of the parent element. You also can specify am XML mark-up tag for the new XML element. If you omit this parameter, the new XML element is created with the same XML tag as XML element containing the XML attribute.

var myRootXMLElement = myDocument.xmlElements.item(0);
myRootXMLElement.xmlElements.item(-1).convertToAttribute();


You also can convert an XML attribute to an XML element,

var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementB = myRootXMLElement.xmlElements.item(1);

//The "at" parameter can be either LocationOptions.atEnd or LocationOptions.atBeginning, but cannot be LocationOptions.after or LocationOptions.before.
myXMLElementB.xmlAttributes.item(0).convertToElement(LocationOptions.atEnd,
myDocument.xmlTags.item("xml_element"));





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);
}