Object Layer Options Scripting
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Object Layer Options Scripting
- This topic has 27 replies, 14 voices, and was last updated 5 years, 2 months ago by Kevin Ward.
-
AuthorPosts
-
-
September 26, 2011 at 12:43 am #60606fruitlesseffortMember
Hello
I have a load of placed InDesign files on a page, each one has a layer called Frames. Is there a way to script Object Layer Options to go through and turn the visibility off for that specific layer to save me going through each link individually.
The reason I don't turn it off when placing the files is that I need it on for a pdf.
Many thanks!
-
September 27, 2011 at 2:54 am #60612Kasyan ServetskyMember
Main();
function Main() {
var page, placedFile, graphics, i, j,
doc = app.activeDocument,
pages = doc.pages;for (i = 0; i < pages.length; i++) {
page = pages[i];
var graphics = page.allGraphics;
for (j = graphics.length-1; j >= 0; j–) {
placedFile = page.allGraphics[j];
if (placedFile.constructor.name == “ImportedPage” && placedFile.itemLink.linkType == “InDesign Format Name”) {
try {
placedFile.graphicLayerOptions.graphicLayers.item(“Frames”).currentVisibility = false;
}
catch (err) {}
}
}}
alert(“Done”);
}P.S. Written in InDesign CS5.5, Windows.
Hope this helps.
Kasyan
-
October 3, 2011 at 7:05 am #60666fruitlesseffortMember
Hi Kasyan
Thanks for this, sorry for the late reply! I am getting an error at
for (j = graphics.length-1; j >= 0; j–) {
Any idea why?
Thanks again!
-
October 6, 2011 at 11:32 am #60704Kasyan ServetskyMember
I need more information to solve the problem: a screenshot or detailed description of the error. Ideally, I'd like to get a sample file so I could test the script against it.
-
January 25, 2012 at 10:48 am #61540David BlatnerKeymaster
I had the same problem in CS5.5 Mac. I wonder if the forum messed up the code a little somewhere? Here's the error:
-
January 25, 2012 at 2:01 pm #61542Theunis De JongMember
It's the minus at the end, right before the closing parens. If you check, you will find it's probably an en-dash — one of the features I actually like about the forum! Every two hyphens get converted into an en-dash. So in Javascript code, you need to change them back to 2 hyphens, like this:
j++
(Hah, I wasn't going to fall in the same trap :) Where I typed a + you need a -.)
-
January 25, 2012 at 2:16 pm #61543David BlatnerKeymaster
Ah ha! Of course. I thought that en dash looked strange.
This still doesn't work for me, but I think it is because I am using placed PSD or PDF files (not INDD files).
What I'm hoping for is a way to toggle the setting of a particular layer on/off in all PDF/INDD/PSD/AI files that I have placed. For example, I might place 20 graphics that have a layer called “French” and when I run the script, I would want that layer to be turned on or off in all the images. Easy change to this script?
-
January 25, 2012 at 4:13 pm #61545Theunis De JongMember
It was easy – – surprisingly so! Kasyan's script did all the work, but he checks explicitly for a placed InDesign page. It took only a little check to find out under what name a placed Photoshop image is stored (see the commented-out “alert” line in my version below). It turned out to be a type of “Image” with a link type of “Photoshop” …
… (After about half an hour of head scratching..)
In fact it was so easy I decided to take it to the next level :) I added a little dialog that shows a selection of checkboxes, and you can turn each one on or off as you wish. The “layer names” list does not come from your placed images — although it is possible to read all of them, you'd get a load of extra names as well. You can adjust the list of names near the top of the function “Main”.
An oddity I ran into: it seems changing layer visibility invalidates the image and re-creates the link! That lead to the aforementioned bout of head-scratching, as suddenly “in the middle of the script”, it would report that the referenced image had disappeared! So I took a shortcut here: “refresh” the link on line 43, rather than re-using the loaded variable “placedFile”. Oh well, that's the sort of thing I do for fun.
So here is the script; it seems wrapping it up inside {pre} and {code} tags defeat WordPress post-beautifications (curly quotes, en-dashed) but if not, at least now you know what to look for.
Main();function Main()
{
var page, placedFile, links, layer, i, j, k,
doc = app.activeDocument,
pages = doc.pages;
var checklist = [];var layernames = [ “English”, “French”, “Dutch”, Russian ];
var ilDialog = app.dialogs.add({name:”Image Layers on/off”, canCancel:true});
with (ilDialog)
{
with(dialogColumns.add())
{
with(dialogRows.add())
{
staticTexts.add ({staticLabel:”Check to show, uncheck to hide”});
for (i=0; i<layernames.length; i++)
{
with(dialogRows.add())
checklist.push (checkboxControls.add({staticLabel:layernames[i], checkedState:false}));
}
}
}
}
var numChanges = 0;
var imageChange;
if (ilDialog.show() == true)
{
links = doc.links;
for (i = links.length-1; i>=0; i–)
{
placedFile = links[i].parent;
// show the internal type of this image:
// alert (placedFile.constructor.name+” / “+placedFile.itemLink.linkType);
if (placedFile.constructor.name == “Image” && placedFile.itemLink != null && placedFile.itemLink.linkType == “Photoshop”)
{
imageChange = false;
for (k=0; k<layernames.length; k++)
{
layer = links[i].parent.graphicLayerOptions.graphicLayers.item(layernames[k]);
if (layer.isValid && layer.currentVisibility != checklist[k].checkedState)
{
layer.currentVisibility = checklist[k].checkedState;
imageChange = true;
}
}
if (imageChange)
numChanges++;
}
}
alert(“Done, made changes in “+numChanges+” images”);
} else
{
ilDialog.destroy();
}
} -
January 25, 2012 at 4:19 pm #61546Theunis De JongMember
Ah, I was too optimistic :(
The script is still beautified, but only on line 30. Right after the last i in the line that reads
for (i = links.length-1; i>=0; i–)
there should be two hyphens, not one single en-dash.
-
January 25, 2012 at 11:32 pm #61556David BlatnerKeymaster
AWESOME! Thank you, Theun. I hope to demo this at Macworld Expo tomorrow morning.
-
February 16, 2012 at 4:23 am #61635daPupeParticipant
Jongware said:
Ah, I was too optimistic :(
The script is still beautified, but only on line 30. Right after the last i in the line that reads
for (i = links.length-1; i>=0; i–)
there should be two hyphens, not one single en-dash.
Hi Jongware, i'm a new user.
Your script is very intersting, maybe you can help me.
My problem is similar. I have a inDesign book and many levels for the language. Every time i have to make a pdf of the book i must open the book's files one by one and change the language layer. I have 9 languages ( “IT”, “EN”, “FR”, “DE”, “ES”, “PT”, “RU”, “HU) and maybe in the future there will be others.
The question is, can you fit the posted script to solve my problem? I would like to change the language layer of the entire book without opening all the file one by one.
I tried also to modify your script… but, really, i'm not capable.
It would be very useful for me if anybody could help me.
Thanks, daPupe
PS. sorry for my english, it is not very well. i hopeyou understand my problem :)
-
February 16, 2012 at 7:48 am #61637Theunis De JongMember
daPupe, that sounds like this would be useful:
/forum/general-indesign-topics/any-way-to-synchronize-a-book-to-turn-onoff-layer
-
February 16, 2012 at 9:41 am #61639daPupeParticipant
Thank you Jongware, it seems to work very well.
I have CS5 7.0.4 version. Man many many thanks.
Good work!
-
July 30, 2012 at 5:57 am #62721Claudio MarconatoMember
This is a very intersting script, I tried on InDesign CS5.5 and CS6, but it returned to me an error, that I attach here:
https://creative.adobe.com/file/6395175b-d8ec-405b-9347-e5f98b45cdf6
This happens either selecting or not selecting any objects
Any suggestion?
-
November 6, 2012 at 11:35 am #63515paddirnMember
I was able to get Jongware's code to work with placed InDesign files, however, I was wondering if there was a way to make this selectively happen on certain Masters? I was trying to cobble together some code to at least have it execute on a single declared Master (“A-Master”, or “B-Master”, etc), but I don't have nearly enough experience with Javascript to know how to get that to work.
Ideally, it would be great if a checklist dialogue could pop up (before the layer checklist) and ask which Masters the layer visibility should be applied to and then it selectively affects those pages with the right Master-page(s) with Jonware's original code.
Is this making sense at all or does anyone have an idea how to get this done?
-
February 13, 2013 at 12:42 pm #64147Bob RubeyMember
Can one of these scripts be edited to turn off/on a consistently named layer of placed PSD files? (See FPO Conundrum in The Pub forum.)
I've tried editing both Kasyan's and Jongware's scripts without much luck. I know where to make to change to the layer name, but Kasyan's script checks for placed InDesign files and I only managed to change the dialog of Jongware's script.
Thanks,
BobEdit: I suspect that because I'm dealing with the visibility of a single layer, on Jongware's script I would need to reverse “Check to show, uncheck to hide” so that it runs as “Check to hide, uncheck to show.”
-
September 5, 2013 at 11:35 am #65182David BlatnerKeymaster
For anyone looking to use this script (it’s a good one!) I have made this fix and posted it here:
https://creativepro.com/downloads/forcedl/ShowHideLayers.jsxYou’ll just need to edit Line 8 to put the names of your layers (in quotes, just like they are in the script).
Here’s some info on how to install scripts, if you need that.
-
June 8, 2016 at 9:43 am #85540Eva HealyMember
Hello All,
I am new to InDesign Scripting. I am looking to hire someone to write a script to turn off and on select object layers in an InDesign file. I am creating the document from a data merge, which is why it does not make sense to individually set the object layers. Each of my AI files will contain the same layers. I will be creating several different InDesign files, each with a different layer visibility option (I.e. The code must allow me the flexibility to switch which layers I select to turn on). If you are interested in working with me, or know someone who is, please respond to this post with your professional email address. Thanks!
Best,
Eva-
June 14, 2016 at 11:37 am #85748Ari SingerMember
Please send me an email to designerjoe[at]outlook.com with a detailed explanation of the problem and how the script should fix it, and I’ll see what I can do.
Ari
-
-
June 15, 2016 at 4:24 pm #85802Ari SingerMember
At the request of Eva Healy I modified Jongware’s script to handle Illustrator files.
I also added some detailed instructions inside so even the ungeeky among us can manipulate what layers to show.app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Show/Hide Layers - Illustrator"); function Main() { var page, placedFile, links, layer, i, j, k, doc = app.activeDocument; pages = doc.pages; var checklist = []; var layernames = [ "red", "blue", "green", "silver"]; /* ------------------------------------------------------------------------------------------------------- ABOVE IS WHERE YOU ALTER THE LAYER NAMES: After the words "var layernames" there's an opening square bracket , then some sets of text enclosed in quotes sperated by commas, and at the end, a closing square bracket. Delete all of the text and the commas separating it, and in its place put in any layers that you want. For example: If you have three layers, 'red', 'green' and 'silver', then in the brackets put in each layer enclosed in quotes and seperated by a comma, as such: ["red", "green", "silver"]. Make sure they are all double-quotes, not single quotes, and make sure they are 'straight' quotes not 'smart' quotes. To ensure that it's properly formatted, only edit the script in a generic text editor (such as Notepad on Windows, or Text Wrangler on Mac). DO NOT use a word-proccesing program such as MS Word or InDesign, as that will add formatting to the script and it will not work properly. ------------------------------------------------------------------------------------------------------- */ var ilDialog = app.dialogs.add({name:"Image Layers on/off", canCancel:true}); with (ilDialog) { with(dialogColumns.add()) { with(dialogRows.add()) { staticTexts.add ({staticLabel:"Check to show, uncheck to hide"}); for (i=0; i<layernames.length; i++) { with(dialogRows.add()) checklist.push (checkboxControls.add({staticLabel:layernames[i], checkedState:false})); } } } } var numChanges = 0; var imageChange; if (ilDialog.show() == true) { links = doc.links; for (i = links.length-1; i>=0; i--) { placedFile = links[i].parent; // show the internal type of this image: //alert (placedFile.constructor.name+" / "+placedFile.itemLink.linkType); if (placedFile.constructor.name == "PDF" && placedFile.itemLink != null && placedFile.itemLink.linkType == "Adobe Portable Document Format (PDF)") { imageChange = false; for (k=0; k<layernames.length; k++) { layer = links[i].parent.graphicLayerOptions.graphicLayers.item(layernames[k]); if (layer.isValid && layer.currentVisibility != checklist[k].checkedState) { layer.currentVisibility = checklist[k].checkedState; imageChange = true; } } if (imageChange) numChanges++; } } alert("Done, made changes in "+numChanges+" images"); } else { ilDialog.destroy(); } }
-
June 17, 2016 at 11:54 am #85867Eva HealyMember
Thank you so much Ari! This script is incredibly useful, and the directions make it super easy for a scripting novice (like myself) to adapt for their needs. Thanks again!
Best,
Eva -
September 7, 2016 at 10:54 am #88218Edward GelasiMember
Ari, I am also a scripting novice (probably even ranked lower than that!) and I would love to be able to use this script! BUT I get an error message in InDesign:
Javascript Error!
Error Number: 2
Error String: main is undefinedEngine: main
FIle: /Users/ed/Library/Preferences/Adobe InDesign/Version 11.0/en_US/Scripts/Scripts Panel/showhidelayers.jsx
Line: 1
Source: app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined,UndoModes.ENTIRE_SCRIPT, “Show/Hide Layers – Illustrator”);Thanks for coming up with this script (and to all contributors). My first post and looking forward to finding more info on this website!
Ed
-
September 7, 2016 at 5:31 pm #88223Ari SingerMember
How are you running the script, from the Scripts panel or from the ESTK?
-
-
September 8, 2016 at 9:58 am #88236Edward GelasiMember
Hi Ari. Running from the Scripts panel in InDesign CC 2015 (latest version for Mac)
-
September 9, 2016 at 1:55 pm #88261Theunis De JongMember
Ari, Edward is right. That line calls “main” but the function is called “Main”. (So to fix, change one of the two to the other.)
-
December 20, 2016 at 1:10 am #90625Aleks KrMember
Hello all. Very good scripts. Would like to ask you more harder question. Is it possible to make script, that would turn off all layers expect one from “Object layers” in PDF and copied only that visible layer to normal Indesign layer with the same name? And after in the same way all other layers. Or for example just copying all “Object layers” from PDF to Indesign layers, for which visibility after will be changed manually?
Thanks! -
April 13, 2018 at 2:03 am #103070Maximilian JänickeParticipant
Hello all,
I collected all the image types that appeared along the discussion and added them as comments (// in the beginning of the line) to the script, so I can change the active one appropriately. I wonder if it’s possible to A: Choose the file type in the dialogue box and B: toggle between all and selected links.
Cheers,
Max
-
January 18, 2019 at 4:02 am #113741Kevin WardMember
Hi all,
I have been using Kansan’s rather handy script to switch layers on / off in placed InDesign documents.
The thing I would like adding is the ability for locked an/or hidden items to not be effected by the script. This is because I don’t want to change the layers in ALL the placed documents in the document.
If anyone can suggest a line to add or edit, then I’d be most grateful. Thanks!
-
-
AuthorPosts
- You must be logged in to reply to this topic.