Copy link to clipboard
Copied
Ok, this is lots of code for a task that should be simple but I didnt write the API :@
This script copies every item on a src-page to a dst-page and doesnt care if one of them is a master-page. I had to write my own map for that and I'm just missing a tiny bit now: placement. If the original item is a link, I wanted to duplicate the original linked item and link that to the dupe instead. That works now, but the return of contentPlace() seems to be no item and in the API (https://www.indesignjs.de/) I dont find a return Type mentioned.
Also, for some reasons that elude my, orig.remove() does not work and yields the error:
Error Number 1
Error string remove.
WTF?
/* Ancient Adobe-version has no Map() -.- */
function Map() {
this.keys = Array();
this.values = Array();
this.set = function(key, value) {
idx = this.getKeyIdx(key, true);
this.values[idx] = value;
};
this.getKeyIdx = function(key, create) {
for (var i = 0; i< this.keys.length; i++) {
if (this.keys[i] == key)
return i;
}
if (!create)
return undefined;
this.keys.push(key);
return this.keys.length;
};
this.get = function(key) {
idx = this.getKeyIdx(key, false);
if (idx === undefined)
return undefined;
return this.values[idx];
};
}
function copyPageContent(src, dst) {
var itemsOnPage = src.pageItems;
copyMap = new Map();
for (var e = 0; e < itemsOnPage.length; e++) {
orig = itemsOnPage[e];
duped = orig.duplicate(dst);
copyMap.set(orig.id, duped.id);
}
for (var e = 0; e < itemsOnPage.length; e++) {
orig = itemsOnPage[e];
options = orig.linkedPageItemOptions;
if (!options.isValid)
continue;
dupedID = copyMap.get(options.parent.id);
if (dupedID === undefined)
continue;
dupedItem = dst.pageItems.itemByID(dupedID);
parentItem = dupedItem.parent;
gb = orig.geometricBounds;
pos = [gb[1], gb[0]];
newLink = parentItem.contentPlace(dupedItem, true);
newLink.move(pos); // <- this doent work, newLink is no Item.
orig.remove(); // <- this doesent work, even if API mentions remove as an PageItem-function -.-
}
}
How do I move the newly placed content to the position, transformation and scaling of orig before removing it?
Copy link to clipboard
Copied
Scale shouldn't change.
The location - calculate vector by the (0,0) point - or top-left corner of the page - and then move every item by the same vector on the destination page.
Copy link to clipboard
Copied
Can you share your sample document?
Copy link to clipboard
Copied
Call it like this:
function main() {
var doc = app.activeDocument;
var master = doc.masterSpreads.itemByName("B-Parent");
var page = doc.pages.add();
copyPageMetrics(master.pages[0], page);
page.appliedMaster = master.appliedMaster;
copyPageContent(master,page);
}
function copyPageMetrics(src, dst) {
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var bounds = src.bounds;
dst.resize(BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, AnchorPoint.TOP_LEFT_ANCHOR,
ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [bounds[3], bounds[2]]);
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}
Copy link to clipboard
Copied
Hi @Lordrhavin , not sure I completely understand what you are trying to do, but you can specify the Master to use when you add a page to the document, and then override all of the new page’s master page items, which converts them to page items. It’s important to note page items are not the same as master page items—a master page item on a page does not become a page item until it is overriden.
There is also the detatch() method, which detatches the page items from the master. Something like this:
var doc = app.activeDocument;
var master = doc.masterSpreads.itemByName("B-Parent");
//add a new page with B-Parent as the applied master
var page = doc.pages.add({appliedMaster:master});
//get all of the master page items on the new page
var mpi = page.masterPageItems
var pi;
for (var i = 0; i < mpi.length; i++){
//override the masterpage item making it a page item
pi = mpi[i].override(page);
//use detach if you want to break the link to the B-Parent master
pi.detach();
};
//And remove any reference to the origina B-Parent Master?
page.appliedMaster = NothingEnum.NOTHING
Before and after:
Copy link to clipboard
Copied
Don't play with duplicating / copying stuff - apply Master to the destination page - and then do Override Parent Page Item - this way - your links remain intact:
Move duplicated items if needed.
I've edited top item on the regular page to dark brown to check if it's a master/source for the bottom item on a regular page - but it's not - bottom item is still linked to the top item on the Master Page - which I edited to red.
Now, I have warnings for both bottom items - on Master and regular page:
But then I can update both links:
Unfortunately - your bottom items / copies are rotated back...
All this has been done manually - but as I've mentioned in the other post - you can do all this through scripting.
Copy link to clipboard
Copied
2nd to 5th rows are not exactly "aligned" to the rest of the tree structure - but info at the end is I think interesting.
As two separate halves: