Created
December 29, 2011 00:05
-
-
Save eccegordo/1530581 to your computer and use it in GitHub Desktop.
Indesign Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functional edits added by Green4ever from Adobe forums. | |
// http://forums.adobe.com/thread/940934 | |
// Logic behind this script | |
// theScript() | |
// is a wrapper function to provide functional scope to the entire script | |
// This function is invoked at the very bottom of the file | |
// main(); | |
// the main function that gathers the initial parameters and launches the dialog box | |
// function mainDialog(docRef, selectedItems, layers, inputs) | |
// A function that creates a dialog box this function is passed as a parameter to a "factory method" | |
// called function sfDialogFactory(dialog) | |
// which is a general purpose utility for creating dialog boxes. | |
// The mainDialog function has a callback function that responds to the "comtinue" button and after some validation | |
// makes a call to a function called | |
// function doAction(docRef, inputs, options) | |
// This is where the main behvior of the script is suppose to be defined. | |
// it is designed to be called AFTER the dialog box return and is the result of the callback inside mainDialog | |
// The doAction function receives objects that are parameter holders for various inputs and options that come from the | |
// dialog box presented to the user | |
function theScript() { | |
/////////////////////////////////////////////////// | |
// BEGIN THE SCRIPT | |
//================================================= | |
//////////////////////////////////////////////////////////////////////////////// | |
// SCRIPT MAIN PROCESS | |
//////////////////////////////////////////////////////////////////////////////// | |
// Main Process | |
// The beginning of the script starts here | |
// Add main logic routine to this function | |
// Call Main Function to get the ball rolling | |
var elementsData; | |
var theDocument = app.activeDocument; | |
var inputGroup; | |
main(); | |
function main(){ | |
var theSelectedItems = theDocument.selection; | |
var theLayers = theDocument.layers; | |
var myDialog = sfDialogFactory(mainDialog(theDocument, theSelectedItems), theLayers); | |
var result = myDialog.show(); | |
if (result == 1){ | |
newAction(); | |
alert("Done"); | |
} | |
else{ | |
alert("Not Done"); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// SCRIPT SUPPORTING FUNCTIONS | |
//////////////////////////////////////////////////////////////////////////////// | |
// Supporting Functions | |
// Include functions that offer partial functionality | |
// These functions are called and acted upon within the mainProcess function | |
function newAction(){ | |
var formData = getControlValues(inputGroup); | |
var inputs = {}; | |
var options = {}; | |
// alert("callback get form data"); | |
// Do something with formData values here | |
var cLen = formData.controls.length; | |
var formValues = ''; | |
for (var c = 0; c < cLen; c++ ) | |
{ | |
if (formData.controls[c].name === "inputECONumber") { | |
inputs.inputECONumber = formData.controls[c].value; | |
// alert(formData.controls[c].name + " | " + formData.controls[c].value); | |
} | |
if (formData.controls[c].name === "inputDocumentName") { | |
inputs.inputDocumentName = formData.controls[c].value; | |
// alert(formData.controls[c].name + " | " + formData.controls[c].value); | |
} | |
if (formData.controls[c].name === "inputDocumentNumber") { | |
inputs.inputDocumentNumber = formData.controls[c].value; | |
// alert(formData.controls[c].name + " | " + formData.controls[c].value); | |
} | |
if (formData.controls[c].name === "inputDocumentRevision") { | |
inputs.inputDocumentRevision = formData.controls[c].value; | |
// alert(formData.controls[c].name + " | " + formData.controls[c].value); | |
} | |
if (formData.controls[c].name === "inputDocumentType") { | |
inputs.inputDocumentType = formData.controls[c].value; | |
// alert(formData.controls[c].name + " | " + formData.controls[c].value); | |
} | |
}//End FOR | |
doAction(theDocument, inputs, options); | |
} | |
function mainDialog(docRef, selectedItems, layers, inputs) { | |
// Main Dialog | |
// alert("Main Dialog: " + docRef.name); | |
if ( docRef === undefined ) { | |
alert("Cannot Execute, please select a document."); | |
} | |
var currentData = getMetaData(); | |
var dialogObj = {}; | |
dialogObj.groups = []; // An array of dialog groups | |
dialogObj.title = "Update Meta Data"; | |
var groupLabelInfo = {}; | |
groupLabelInfo.title = "Edit: " + docRef.name; | |
// Add Elements using JSON shorthand syntax | |
groupLabelInfo.elements = [ | |
{ | |
"name":"labelECONumber", | |
"type":"statictext", | |
"value":"Engineering Change Order (ECO) Number", | |
"visible":true | |
}, | |
{ | |
"name":"inputECONumber", | |
"type":"edittext", | |
"value": currentData.inputECONumber, | |
"visible":true | |
}, | |
{ | |
"name":"documentName", | |
"type":"statictext", | |
"value":"Document Name", | |
"visible":true | |
}, | |
{ | |
"name":"inputDocumentName", | |
"type":"edittext", | |
"value": currentData.inputDocumentName, | |
"visible":true | |
}, | |
{ | |
"name":"documentNumber", | |
"type":"statictext", | |
"value":"Document Number", | |
"visible":true | |
}, | |
{ | |
"name":"inputDocumentNumber", | |
"type":"edittext", | |
"value": currentData.inputDocumentNumber, | |
"visible":true | |
}, | |
{ | |
"name":"documentRevision", | |
"type":"statictext", | |
"value":"Revision", | |
"visible":true | |
}, | |
{ | |
"name":"inputDocumentRevision", | |
"type":"dropdownlist", | |
"value": currentData.listRevisions, | |
"visible":true, | |
"selection":currentData.selectionRevision, | |
}, | |
{ | |
"name":"documentType", | |
"type":"statictext", | |
"value":"Label Type", | |
"visible":true | |
}, | |
{ | |
"name":"inputDocumentType", | |
"type":"dropdownlist", | |
"value": currentData.listDocumentTypes, | |
"visible":true, | |
"selection":currentData.selectionDocumentType, | |
}, | |
]; | |
// Add to groups to list | |
dialogObj.groups.push(groupLabelInfo); | |
return dialogObj; | |
} | |
function doAction(docRef, inputs, options) { | |
alert("doAction " + docRef.name + " \n INPUTS: \n" + inputs.reflect.properties + "\n\n OPTIONS: \n" + options.reflect.properties); | |
// Get the current contents | |
var fooContents = app.activeDocument.textVariables.item ('Foo').variableOptions.contents; | |
var barContents = app.activeDocument.textVariables.item ('Bar').variableOptions.contents; | |
alert("GET textVariable contents \n Foo: \n" + fooContents + "\n\n Bar: \n" + barContents); | |
// Set/Update the contents | |
// Why does this not seem to work? | |
alert("SET textVariable contents \n Foo: \n" + inputs.inputDocumentName + "\n\n Bar: \n" + inputs.inputDocumentType); | |
app.activeDocument.textVariables.item ('Foo').variableOptions.contents = inputs.inputDocumentName; | |
app.activeDocument.textVariables.item ('Bar').variableOptions.contents = inputs.inputDocumentType; | |
// Script does not seem to reach this point. Why? | |
alert("END of doAction"); | |
} | |
// A Factory function for creating dialog boxes | |
function sfDialogFactory(dialog) { | |
// A factory method for creating dialog screens | |
// Dialog Window | |
var d = new Window("dialog", dialog.title); | |
// alert("Number of Inputs" + dialog.inputs.length); | |
// alert("Number of Options" + dialog.options.length); | |
var i; // counter | |
var len; // length of array elements | |
// Generate Groups | |
if (dialog.groups.length > 0) { | |
len = dialog.groups.length; | |
for (i = 0; i < len; i++ ) | |
{ | |
var currentGroup = dialog.groups[i]; | |
inputGroup = d.add ("panel", undefined, currentGroup.title); | |
inputGroup.alignChildren = ["fill","fill"]; | |
if (currentGroup.elements.length > 0) { | |
// Add Elements | |
var ii; | |
var elemLen = currentGroup.elements.length; | |
for (ii = 0; ii < elemLen; ii++ ) | |
{ | |
var currentElement = currentGroup.elements[ii]; | |
var el = inputGroup.add(currentElement.type, undefined, currentElement.value); | |
// Additional properties added for future reflection | |
el.elName = currentElement.name; | |
el.elIndex = ii; | |
switch(currentElement.type) | |
{ | |
case "statictext": | |
el.visible = currentElement.visible; | |
break; | |
case "edittext": | |
el.visible = currentElement.visible; | |
break; | |
case "dropdownlist": | |
el.visible = currentElement.visible; | |
el.selection = currentElement.selection; | |
el.onChange = currentElement.onChange; | |
break; | |
case "checkbox": | |
el.visible = currentElement.visible; | |
el.value = currentElement.value; | |
break; | |
default: | |
throw new Error('Unknown Dialog Element Type [' + currentElement.type + ']'); | |
} | |
} | |
} | |
} | |
} | |
// Buttons Group | |
var buttonGroup = d.add("group"); | |
var bOK = buttonGroup.add("button", undefined, "Continue", {name: "ok"}); | |
var bCANCEL = buttonGroup.add("button", undefined, "Cancel", {name: "cancel"}); | |
return d; | |
} | |
function getControlValues(set) { | |
elementsData = {}; | |
elementsData.controls = []; | |
// TO DO Add more types | |
var giLen = set.children.length; | |
for (var gi = 0; gi < giLen; gi++ ) | |
{ | |
var child = set.children[gi]; | |
// alert(objReflection(child, "none", false)); | |
// alert(child.type); | |
var control = {}; | |
control.name = child.elName; | |
control.index = child.elIndex; | |
control.type = child.type; | |
control.visible = child.visible; | |
switch(child.type) | |
{ | |
case "statictext": | |
control.value = child.text; | |
break; | |
case "edittext": | |
control.value = child.text; | |
break; | |
case "dropdownlist": | |
control.value = child.selection.text; | |
break; | |
case "checkbox": | |
control.value = child.value; | |
break; | |
default: | |
throw new Error('Unknown Dialog Element Type'); | |
} | |
elementsData.controls.push(control); | |
// alert(objReflection(control, "none", false)); | |
} | |
return elementsData; | |
} | |
function getMetaData() { | |
// Return a data structure that contains meta data from the document. | |
var dataObject = {}; | |
// Default arrays | |
dataObject.listRevisions = generateRangeOfNumbers ("r", 1, 100); | |
dataObject.listDesignComps = generateRangeOfNumbers ("Comp_", 1, 100); | |
dataObject.listDocumentTypes = [ | |
"Datasheet", | |
"Manual", | |
"Tech Guide", | |
"Other" | |
]; | |
// Set Sensible Default Values for the UI Form | |
if (!dataObject.inputECONumber) { | |
dataObject.inputECONumber = "###ECO###"; | |
} | |
if (!dataObject.inputDocumentName) { | |
dataObject.inputDocumentName = "Document Name"; | |
} | |
if (!dataObject.inputDocumentNumber) { | |
dataObject.inputDocumentNumber = "048-xxx-30"; | |
} | |
if (!dataObject.inputDocumentRevision) { | |
dataObject.inputDocumentRevision = "r01"; | |
} | |
if (!dataObject.inputDocumentType) { | |
dataObject.inputDocumentType = "Datasheet"; | |
} | |
return dataObject; | |
} | |
function generateRangeOfNumbers (prefix, start, end) { | |
// This function generates an array of sequential numbers within a range | |
// prefix = string to append to beginning of each element | |
// start = the beginning of the range | |
// end = the end of the range | |
// length = overall number of cycles to loop through, start and end must fall within this value | |
var output = []; | |
for ( var i = start; i <= end; i++ ) | |
{ | |
if (i < 10) { | |
// Add a leading zero | |
output.push(prefix + "0" + i); | |
} | |
else { | |
output.push(prefix + i); | |
} | |
} | |
return output; | |
} | |
//================================================= | |
// END THE SCRIPT | |
/////////////////////////////////////////////////// | |
} | |
theScript(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment