Skip to main content
Lordrhavin
Inspiring
September 26, 2024
Question

Copying page content

  • September 26, 2024
  • 1 reply
  • 674 views

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?

This topic has been closed for replies.

1 reply

Robert at ID-Tasker
Legend
September 26, 2024

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. 

 

Robert at ID-Tasker
Legend
September 26, 2024

Can you share your sample document? 

 

Lordrhavin
Inspiring
September 26, 2024

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