-
Notifications
You must be signed in to change notification settings - Fork 30
Documents
###documents
Everything happens in documents it is something like the base container. You can access the document in different ways. Like so:
/**
* Look for an active document
* without try catch it could produce an error
*
*/
var doc;
try{
doc = app.activeDocument; // works with no doc
var name = doc.name; // this is what throws an error
alert("You have an active document.");
}catch(error){
// error is a variable that contains
// a message that can be read
// alert(error);
alert("You dont have an active document.");
}A document can be created with the values already set. This can be pretty handy but is hard to debug. You should always first test a property solo and than combine them in an object. So this code:
var doc = app.documents.add();
var val = 150;
var prefs = doc.documentPreferences;
prefs.pageWidth = val;
prefs.pageHeight = val;Could be minified to the following code. This is pretty handy, isn't it?
app.documents.add({
documentPreferences:{
pageWidth:150,pageHeight:150
}
});This means you can create a JSON object with the right syntax and than inject it into an app.documents.add()
!Achtung! This works only in InDesign CS5+
/**
* You can create objects with the same structure as
* a documents to define values
* and than in inject them within a loop
* **!Achtung!** This works only in InDesign CS5+
*/
main();
// everyting is in here
function main(){
var doc_data = {
"documentPreferences":{
"pageWidth": 10 ,"pageHeight":10
}
};
/*
now you could do a lot of fancy stuff
and still have your important data
like the settings for the doc on the top of the script
also you could place them into another
file or something like this.
*/
// now loop and create some documents
var i = 1;
var num = 13;
var arr = new Array();
while(i < num){
// do something fancy here
doc_data.documentPreferences.pageWidth+=10;
doc_data.documentPreferences.pageHeight+=10;
// now push it into an array
arr.push(app.documents.add(doc_data));
i++;
}
// add some text. just for fun
add_text (arr);
}
/**
* This function adds text to an doc
* it takes an array of documents as argument
*/
function add_text(arr){
for(var j in arr){
var d = arr[j];
var pw = d.documentPreferences.pageWidth;
var ph = d.documentPreferences.pageHeight;
var gutter = pw/10;
var gb = [gutter,gutter,ph - gutter, pw - gutter];
var pg = d.pages.item(0);
pg.textFrames.add({
geometricBounds:gb,
contents:"I'm doc number " + j
});
}
}Some useful code when working with lots of documents or developing scripts that creates documents.
Revert to last version:
app.documents.everyItem().revert()Close all open documents:
// Like Uncle Ben saz: "With great power comes great responsibility!"
app.documents.everyItem().close(SaveOptions.NO);This wiki is maintained by:
fabiantheblind
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
Thanks to the students from the seminar for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- app
- Colorbrewer
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import