• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

Looking For Script To Put Content On A Specified Layer

Engaged ,
Jul 28, 2023 Jul 28, 2023

Copy link to clipboard

Copied

I'm working on cleaning up a training manual. The InDesign file has four layers but all copy and photographs were lumped into one layer.

 

Is there a script that can target images and put them into a layer named Images?

 

I need a similar task done for the copy. The script would target a paragraph style name and then move it to a particular layer. 

Example: all copy linked to a paragraph style named "Captions" gets put those into a layer named Captions.


It's only an island if you look at it from the water.
TOPICS
How to , Scripting

Views

471

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 28, 2023 Jul 28, 2023

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 t

...

Votes

Translate

Translate
Explorer ,
Jul 28, 2023 Jul 28, 2023

Copy link to clipboard

Copied

It's both very easy and yet raise questions. What if you have a group made of a text and an image? 

Images inside a table? You probably want to define your constraints and what you accept and what you don't.

Or in other words, if done manually, which challenges would you face and which logic should a script follow on those cases?

Once you cleared this out, helping with a script will be easier.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2023 Jul 28, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 01, 2023 Aug 01, 2023

Copy link to clipboard

Copied

Thank you both for your input! I'm very grateful for the script.

I took the script and copied it to a text editor and saved it with a .js extension. Then I moved it to the InDesign scripts folder. When I ran it, I got some error messages:

 

ChrisPanny_0-1690914342940.png

 


It's only an island if you look at it from the water.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2023 Aug 01, 2023

Copy link to clipboard

Copied

Hi @Chris Panny, thanks for trying it. That error most likely means that the target layer in your document doesn't exist. I've updated the script to give a more user-friendly alert when that happens. Can you try it again? And you must edit the layer names if they don't match the layers in your document.

- Mark

Screenshot 2023-08-02 at 08.52.29.png

 

Edit 2023-08-02: updated when I noticed that you have posted the actual layer names in your screenshot.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 03, 2023 Aug 03, 2023

Copy link to clipboard

Copied

I made the edits to your script and it worked! Thank you!!


It's only an island if you look at it from the water.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2023 Aug 01, 2023

Copy link to clipboard

Copied

Why is it an issue? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 03, 2023 Aug 03, 2023

Copy link to clipboard

Copied

The manuals I work on are 100-250 pages. I end up having to move the assets into their correct layers manually. It's a lot of redundant work so I'm looking to automate this.


It's only an island if you look at it from the water.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2023 Aug 03, 2023

Copy link to clipboard

Copied

But why do it at all? 
Just for aesthetics? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 03, 2023 Aug 03, 2023

Copy link to clipboard

Copied

Sometimes I have to isolate the assets such as images. If they're all in one layer with text frames and vector objects, it means I'd have to sort through this to find multiple images in order to turn them off / on. There could be 40+ assets in one layer. It makes the layer cluttered.

 

It's also easier to apply object styles to the images if they're in their own layer. It makes my work flow more effecient. 


It's only an island if you look at it from the water.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

Hi, Eugene, there are several reasons to keep text and art on separate layers. Most of them are technical, and avoid additional complications and a lot more work down the line.

 

For example, I found this thread looking for this script because I have some art frames with softmasks that are in the same layer as the text, layered over the text, which are rasterizing it. When the transparencies are flattened, the type gets sliced up into pieces. (I had whole sections of text move around when someone tried to make an edit to the flattened file, so now I'm fixing it for a reprint.)

 

I also live in the real world, and I know every designer puts their content onto the page without concern for what layer it's on. (Much like most people leave every email they receive in their Inbox.) Part of my job is helping the next designer that gets to pick up this layout clean it up so they can more easily make changes.

 

Text hidden behind art can be a problem. It's honestly not really an issue so much anymore with modern RIPs, as they raster only what can be seen, but historically there have been cases where the hidden text made its way onto the black plate, printing on top of the art.

 

Plus there's the fact that the copy is still in the file, even when it's behind an art frame, and it shows up in text extractions, spell checks, style sheets, font usage, etc.

 

And we also do a fair amount of international editions. We do multiple layers of text for US and UK English spellings, as well as other languages. It's a simple matter to turn one off and the other on. 

 

To that end, you can export a PDF with those layers intact, and that selection of text can more easily happen within the PDF without going back to the design files.

 

And then as an organizational tool: with a complex layout, I can turn off the text to get at the art underneath, or the other way around, isolating the text, when you have to select groups of objects to move them around.

 

If I could make one analogy it would be this: do your kitchen cabinets have shelves? or are all of your dishes in one stack?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

I do work with layers on a daily basis. I don't really have a need for them -but people are obsessed with them. I get that people can be untidy with artwork - and I've seen in illustrator when they hide something on layers - and one time I hit the keyboard shortcut to unhide all items and next of all there were thousands of objects all over the page that were earlier marked for deletion - but instead of deleting just hid them in the layers... insane. 

 

Anyway - I understand the philosophy of layers, I do use them, but previously never used them and never had any issues.

But I understand that not everyone is 'neat'. 

 

I also open artworks created by other artworkers from all over the world - and the things going on in files is another chapter of it's own - bizarre solutions to simple problems. 

 

Anyway - I was just curious to the OP why it was so necessary - seemed like a lot of work for little to no gain - in that the result of output would (should) be the same. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 20, 2024 Feb 20, 2024

Copy link to clipboard

Copied

LATEST

I can only speak to my needs. I have a layout with a whole lot of text frames some of which are layered underneath a whole lot of art frames with softmasks, and that's rasterizing the text. That's not how it should be laid out, and the output would not be the same as if it were done correctly.

 

My choices are going through each frame one by one, selecting all the text frames manually and moving them to the top of the stack order, or double-clicking one script in a menu and having it done for me. When you talk of "a lot of work for little gain," this would be the opposite of that — tremendous gain for almost no effort. Because again, the idea of a script is to not have to do the work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines