Skip to main content
Participant
January 20, 2023
Answered

Script for object styles being applied to specific layer

  • January 20, 2023
  • 1 reply
  • 472 views

Hi, I'm very new to scripting and I've been unable to locate this answer elsewhere on the community. I'm trying to make a specific object style appear on a specific layer so that I can set that later to non-print and it doesn't show up on my exports.

 

Example: I have an object style called "Post It - Comments" which is a bright pink text box we use to write comments that stand out, but we don't want them to end up getting accidentally exported. I want to make sure all of these comment boxes can be easily toggled off, so I want them to move to a layer called "Comments" that I can default to not print in exports. Right now, it takes a long time (full of user error) to manually add these comments to the right layer because InDesign defaults to the last used layer, which is typically the Text layer.

 

I have attached a screenshot of how I use the Object Style as an example. I'm sure this is a simple script but can't find it. Also, it would be great if you can help me know how to apply it. Can I just add the script to a Word file and save as a .jsx and that will work in my Scripts Panel in my file explorer? Thanks!

This topic has been closed for replies.
Correct answer Mike Bro

Hello @Krystal280365920rnq ,

I found what you are looking for here...

https://creativepro.com/a-script-to-collect-all-objects-with-a-particular-object-style/

 

How to Install a Script in InDesign

https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post/

 

This version of the script has been modified for your needs.

 

EDIT: this version has been updated 012123

 

function main() {
    
    app.findObjectPreferences = null;
    app.findObjectPreferences.appliedObjectStyles = "Post It - Comments";
    found_list = app.activeDocument.findObject(true);
    myCommentsLayer = app.activeDocument.layers.itemByName("Comments");
    if(!myCommentsLayer.isValid){
    myCommentsLayer = app.activeDocument.layers.add();
    myCommentsLayer.name = "Comments";
    }
    app.activeDocument.layers.itemByName("Comments").printable = false;
    for(i=0; i<found_list.length; i++)
    found_list[i].move(myCommentsLayer);
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

 

Regards,

Mike

1 reply

Mike BroCorrect answer
Legend
January 20, 2023

Hello @Krystal280365920rnq ,

I found what you are looking for here...

https://creativepro.com/a-script-to-collect-all-objects-with-a-particular-object-style/

 

How to Install a Script in InDesign

https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post/

 

This version of the script has been modified for your needs.

 

EDIT: this version has been updated 012123

 

function main() {
    
    app.findObjectPreferences = null;
    app.findObjectPreferences.appliedObjectStyles = "Post It - Comments";
    found_list = app.activeDocument.findObject(true);
    myCommentsLayer = app.activeDocument.layers.itemByName("Comments");
    if(!myCommentsLayer.isValid){
    myCommentsLayer = app.activeDocument.layers.add();
    myCommentsLayer.name = "Comments";
    }
    app.activeDocument.layers.itemByName("Comments").printable = false;
    for(i=0; i<found_list.length; i++)
    found_list[i].move(myCommentsLayer);
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

 

Regards,

Mike

Participant
January 24, 2023

Thanks, Mike! I was able to successfully apply the script to my INDD and it will move all of those object styles to the new Comments layer and make it non-print.

 

However, I got the script to work once just fine. I closed the file and went back in to edit the same file later and now when I run the script, it just makes all of those object styles invisible. I can't even see them when I make sure the layer is visible. In the layer options, Show Layer is already selected, so I'm not sure why they all go invisible now. 

 

I've attached an image to show that my selected object is pink in the top right corner, showing it's on the Comments layer, but you can't visibly see anything. The object style is still applied so it thinks the fill color and paragraph style is still applied so it didn't clear out the styles that I can tell. 

 

Thanks for any help thus far!

Best,

Krystal