Object Layer Options Scripting

Viewing 25 reply threads
  • Author
    Posts
    • #60606

      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!

    • #60612

      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

    • #60666

      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!

    • #60704

      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.

    • #61540
      David Blatner
      Keymaster

      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:

    • #61542

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

    • #61543
      David Blatner
      Keymaster

      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?

    • #61545

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

    • #61546

      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.

    • #61556
      David Blatner
      Keymaster

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

    • #61635
      daPupe
      Participant

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

    • #61637
    • #61639
      daPupe
      Participant

      Thank you Jongware, it seems to work very well.

      I have CS5 7.0.4 version. Man many many thanks.

      Good work!

    • #62721

      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?

    • #63515
      paddirn
      Member

      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?

    • #64147
      Bob Rubey
      Member

      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,
      Bob

      Edit: 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.”

    • #65182
      David Blatner
      Keymaster

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

      You’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.

    • #85540
      Eva Healy
      Member

      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

      • #85748
        Ari Singer
        Member

        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

    • #85802
      Ari Singer
      Member

      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();
              }
          }
      
    • #85867
      Eva Healy
      Member

      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

    • #88218

      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 undefined

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

      • #88223
        Ari Singer
        Member

        How are you running the script, from the Scripts panel or from the ESTK?

    • #88236

      Hi Ari. Running from the Scripts panel in InDesign CC 2015 (latest version for Mac)

    • #88261

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

    • #90625
      Aleks Kr
      Member

      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!

    • #103070
      Maximilian Jänicke
      Participant

      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

    • #113741
      Kevin Ward
      Member

      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!

Viewing 25 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo