You must be logged in to post Login

Search Forums:


 






Object Layer Options Scripting

UserPost

12:43 am
September 26, 2011


fruitlesseffort

UK

Community Member

posts 4

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!

2:54 am
September 27, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

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

7:05 am
October 3, 2011


fruitlesseffort

UK

Community Member

posts 4

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!

11:32 am
October 6, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

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.

10:48 am
January 25, 2012


David Blatner

Admin

posts 823

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:


co-host, InDesignSecrets.com

2:01 pm
January 25, 2012


Jongware

Member

posts 764

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 -.)

2:16 pm
January 25, 2012


David Blatner

Admin

posts 823

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?

co-host, InDesignSecrets.com

4:13 pm
January 25, 2012


Jongware

Member

posts 764

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

4:19 pm
January 25, 2012


Jongware

Member

posts 764

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.

11:32 pm
January 25, 2012


David Blatner

Admin

posts 823

AWESOME! Thank you, Theun. I hope to demo this at Macworld Expo tomorrow morning.

co-host, InDesignSecrets.com

4:23 am
February 16, 2012


daPupe

New Member

posts 2

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 :)

7:48 am
February 16, 2012


Jongware

Member

posts 764

daPupe, that sounds like this would be useful:

http://indesignsecrets.com/for…..off-layers

9:41 am
February 16, 2012


daPupe

New Member

posts 2

Thank you Jongware, it seems to work very well.

I have CS5 7.0.4 version. Man many many thanks.

Good work!