Hi @Chris Panny, I've written a script that basically does what you ask. It can be quite flexible, but I've included two usages to match the description of your needs. See the lines starting "moveItemsToLayers"? They are the two calls to execute the function. You can turn one or the other off by prepending two forward slashes (//) to the line you don't want to execute. Save the script as plain text—it won't work if saved as rich text (which many editors do by default, so you must ask for plain text).
Let me know if it helps. Was written quickly and only tested on my simple test document, so it will no doubt need some tweaking if something doesn't work the way you expect in your real document.
- Mark
/**
* Move items to layer.
* Examples of usage:
* 1. move text frames containing text with caption paragraph style
* 2. move all graphics of document
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/looking-for-script-to-put-content-on-a-specified-layer/m-p/13970732
*/
function main() {
var doc = app.activeDocument,
captionStyle = doc.paragraphStyles.itemByName('Caption');
moveItemsToLayer(doc, captionStyle, 'captions');
moveItemsToLayer(doc, doc.allGraphics, 'Images');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Move Items To Layer');
/**
* Move items to layer.
* @author m1b
* @version 2023-07-29
* @param {Document} doc - an Indesign Document.
* @param {Array|Collection|Function|ParagraphStyle} getItems - items to move, or a method for getting them.
* @param {Layer} layer - the target layer.
* @returns {Number} - the count of moved items.
*/
function moveItemsToLayer(doc, getItems, layer) {
if (layer.constructor.name == 'String')
layer = doc.layers.itemByName(layer);
if (!layer.isValid) {
alert('Layer "' + layer.name + '" is invalid.');
return;
}
var items,
counter = 0,
already = {};
if (typeof getItems === 'function')
items = getItems(doc);
else if (getItems.constructor.name == 'ParagraphStyle')
items = findParagraphs(doc, getItems);
else if (getItems.hasOwnProperty('0'))
items = getItems;
for (var i = items.length - 1; i >= 0; i--) {
var moveMe = getMoveableItem(items[i]);
if (
moveMe == undefined
|| !moveMe.isValid
|| already[moveMe.toSpecifier()] == true
)
continue;
try {
moveMe.move(layer);
already[moveMe.toSpecifier()] = true;
counter++;
} catch (error) { }
}
return counter;
};
/**
* Get array of paragraphs with applied style.
* @author m1b
* @version 2023-07-29
* @param {Document} doc - an Indesign Document
* @param {ParagraphStyle} paragraphStyle - the target style.
* @return {Array<Paragraph>}
*/
function findParagraphs(doc, paragraphStyle) {
if (
paragraphStyle == undefined
|| !paragraphStyle.isValid
)
return [];
app.findTextPreferences = NothingEnum.NOTHING;
app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.appliedParagraphStyle = paragraphStyle;
return doc.findText();
}
/**
* Returns object (hopefully!) suitable
* for moving across layers, eg. given text,
* will return the parent text frame.
* @author m1b
* @version 2023-07-29
* @param {any} item - an Indesign DOM object.
* @return {any} - a 'moveable' Indesign DOM object.
*/
function getMoveableItem(item) {
var target = item;
while (
!target.hasOwnProperty('geometricBounds')
&& target.hasOwnProperty('parent')
) {
if (
target.hasOwnProperty('parentTextFrames')
&& target.parentTextFrames.length == 1
)
target = target.parentTextFrames[0];
else if (target.hasOwnProperty('parent'))
target = target.parent;
}
if (target.hasOwnProperty('itemLink'))
target = target.parent;
if (!target.hasOwnProperty('geometricBounds'))
// don't think we can move this item
return;
return target;
};
Edit 2023-08-02: improved error message and layer targetting.