Copy link to clipboard
Copied
I am looking for help to create a script, that adjusts varing page sizes based on pulled in Data Merge info - the CSV file has fields for W & H, so I want to adjust the pages created from the merge, to be specific sizes, each unique in the created merged document.
e.g. - CSV file, Row 1 has "6" in the W column, "10" in the H column, after the merged doc is created, page 1 would pull in all fields, and also set the page size to 6" x 10" in size. Each page would be a different size.
I am new to scripting so I thought this would be a good first go at doing so. I've started my research but don't find a whole lot in the way of combining data merge & scripting. Would it be best to incorporate the action along w/ the merge, or after the document is created, create a script to resize each page based on a given field populated by the CSV file after the fact? ( <<WIDTH>> and <<HEIGHT>> created fields in the merge.
Hopefully that all makes sense -
Thanks!
Hi @J_Kraft , You might be able to use an idleTask listener along with script labels.
Here my data is:
This example invokes the DataMerge menu item, labels the w & h text frames and adds an idletask:
//need targetengine for listeners
#targetengine "session"
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
var mergeDoc, iTask;
var wLabel = "width";
var hLabel = "height";
var wData = "<<W>>";
var hData = "<<H>>";
//a boolean to get the merge document
var isMerge = f
...
Copy link to clipboard
Copied
In my opinion, you will have to run a post processing script that runs after the merge is done(either via scripting or manually). Now for this script to resize the pages, you would have to identify the value of W and H in the merged document. How you identify these values would depend upon how your document has been setup and the incoming data, some of the scenarios that I can think of are mentioned below
I hope this makes sense and did not confuse you more
-Manan
Copy link to clipboard
Copied
Great! Thank you for the start of how to think about setting this up.
I'd be interested to see if anyone has an alternative approach.
Copy link to clipboard
Copied
Another apporach could be using a pre processing script which does the following
This approach would be less complex than the previous one but both approaches have their pros and cons
-Manan
Copy link to clipboard
Copied
Another alternative approach would be to roll your own DataMerge with a custom CSV parser and build the pages on the fly, with varying height and width per row.
Copy link to clipboard
Copied
As someone who is just getting started writing scripts - by any chance of a good resource, or a script I could Git to monkey with that you might recommend to Frankenstine together?
Copy link to clipboard
Copied
Hi @J_Kraft , You might be able to use an idleTask listener along with script labels.
Here my data is:
This example invokes the DataMerge menu item, labels the w & h text frames and adds an idletask:
//need targetengine for listeners
#targetengine "session"
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
var mergeDoc, iTask;
var wLabel = "width";
var hLabel = "height";
var wData = "<<W>>";
var hData = "<<H>>";
//a boolean to get the merge document
var isMerge = false;
setLabels();
//invoke the datamerge menu, the Data Merge Panel needs to be open
var menuItem = app.menuActions.itemByID(108036)
if(menuItem.enabled){
menuItem.invoke();
//set isMerge to true and listen for the idleEvent
isMerge = true;
//make a new idleTask. The task name and the sleep time 4 sec
iTask = makeIdleTask("onMergeDocument", 4000)
iTask.addEventListener(IdleEvent.ON_IDLE, onMergeDoc);
}else{
alert("Open DataMerge panel and try again");
};
/**
* Idle task function runs when data merge is invoked
* @param event
*
*/
function onMergeDoc(e){
if (isMerge) {
//the merged document
mergeDoc = app.activeDocument;
iTask.remove();
//resize the pages based on the w & h fields
setPages()
}
}
/**
* Resize the merged document‘s pages
* @return void
*/
function setPages(){
var pgs = mergeDoc.pages
var api, nw, nh;
for (var i = 0; i < pgs.length; i++){
api = pgs[i].allPageItems;
//gwet the width and heights
for (var j = 0; j < api.length; j++){
if (api[j].label == wLabel) {
nw = Number(api[j].contents);
}
if (api[j].label == hLabel) {
nh = Number(api[j].contents);
}
}
//Resize the pages, adjust as needed
pgs[i].resize (CoordinateSpaces.INNER_COORDINATES,AnchorPoint.CENTER_ANCHOR,ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,[nw*72,nh*72])
}
cleanup();
}
/**
* sets datamerge field’s script label
* @return void
*
*/
function setLabels(){
var mapi = app.documents[0].allPageItems;
for (var i = 0; i < mapi.length; i++){
if (mapi[i].contents == wData) {
mapi[i].label = wLabel;
}
if (mapi[i].contents == hData) {
mapi[i].label = hLabel;
}
};
}
/**
* Makes a new named idleTask
* @param the task name
* @param the sleep time
* @return the named idleTask
*/
function makeIdleTask(n, s){
var it;
try {
app.idleTasks.add({name:n, sleep:s})
}catch(e) {
it = app.idleTasks.item (n);
}
return app.idleTasks.item (n);
}
/**
* Remove the w & h text frames
* @return void
*
*/
function cleanup(){
var api = mergeDoc.allPageItems
for (var i = 0; i < api.length; i++){
if (api[i].label == wLabel || api[i].label == hLabel) {
api[i].remove()
}
};
}
The resized merged pages
Copy link to clipboard
Copied
Hey there Rob!
Thank you! I got the script to run, and it looked like it was working like magic! - it took a moment to resize all the pages (in this case there are 42 records in the CSV), and then immediately crashed ID. Could it be something to do with it merging from a CSV file, and one w/ headers for that?
So close!
Copy link to clipboard
Copied
Oh, I think I got it working, seemed to crash if the input range wasn't exact to the # of records (I included an extra row w/ no info) Chunked it out into 10, 20 and they worked fine.
Only other issue I caught, it removes the W and H fields from the doc, which I'd like to keep if possible.
Thanks again!
Copy link to clipboard
Copied
Remove the cleanup() call at the end of the setpages() function