6 Replies Latest reply on May 11, 2016 6:14 AM by JustOneBren

    Swatch Value Switch Script (layer palette labeled groups)

    JustOneBren Level 1

      Please help ...

       

      I work on a catalogue that has section tabs down the side of the page, there are 14 tabs, each with 2 different colours.

      The tab objects have been labeled in the layer palette as "Tab 1", "Tab 2", etc. When the script is run, depending on which tab has been (manually) selected, the "Template Colour A" and "Template Colour B" swatches are coloured accordingly.

      I thought I could use the "Switch" function but I'm having a problem getting it to work, I have an object selected but the colour changes don't happen, it just continues to the "move to another layer" part of the script. Not sure what I am missing to make this work?

       

      switch (app.selection[0].pageItems.item){

           case "Tab 1":

               app.activeDocument.swatches.itemByName("Template Colour A").colorValue=[50,0,100,0]; // Swatch Value Change

               app.activeDocument.swatches.itemByName("Template Colour B").colorValue=[0,50,100,0]; // Swatch Value Change

                     break;

           case "Tab 2":

               app.activeDocument.swatches.itemByName("Template Colour A").colorValue=[100,100,10,15]; // Swatch Value Change

               app.activeDocument.swatches.itemByName("Template Colour B").colorValue=[70,00,00,00]; // Swatch Value Change

                     break;

                  default:

                     break;

      }

      app.selection[0].itemLayer = "Layer 1";

       

       

      Any thoughts would be gratefully appreciated, Brendan

        • 1. Re: Swatch Value Switch Script (layer palette labeled groups)
          Loic.Aigon Adobe Community Professional

          You are evaluating the "item" function itself regarding to strings. Basically you say when my object function equals a string. As it never happens it just hits the default break command;

           

          Try this:

          var main = function() {
            var doc = app.properties.activeDocument;
            var sws, sw;
            var props = {
            "Tab 1":{
            A:[50,0,100,0],
            B:[0,50,100,0],
            },
            //"Tab 2"…
            }
            if ( !doc ) return;
            sws = doc.swatches;
          
            if ( app.selection.length!=1
            || (app.selection[0].properties.parentStory
            && !(app.selection[0] instanceof TextFrame)
            )
            ) return;
          
            var name = app.selection[0].name;
          
            if ( !props[name] 
            || !props[name].A
            || !props[name].B) {
            alert("No data for "+name );
            return;
            }
          
          
            sws.item ("Template Colour A") = props[name].A;
            sws.item ("Template Colour B") = props[name].B;
          }
          
          
          main();
          
          • 2. Re: Swatch Value Switch Script (layer palette labeled groups)
            JustOneBren Level 1

            Many Thanks for your help, iv'e run the script but it errors out with:

            Error Number: 3

            Error String: Cannot assign value

            Line 33:

            Source sws.item ("Template Colour A") = props[name].A;

            Offending Text: props

            Do you have any idea why this would be, I'm running CS6 on a Mac if that makes any difference.

            Thanks again , Bren

            • 3. Re: Swatch Value Switch Script (layer palette labeled groups)
              Loic.Aigon Adobe Community Professional

              colorValue was missing, sorry about that…

               

              var main = function() {  
                var doc = app.properties.activeDocument;  
                var sws, sw;  
                var props = {  
                "Tab 1":{  
                A:[50,0,100,0],  
                B:[0,50,100,0],  
                },  
                //"Tab 2"…  
                }  
                if ( !doc ) return;  
                sws = doc.swatches;  
                
                if ( app.selection.length!=1  
                || (app.selection[0].properties.parentStory  
                && !(app.selection[0] instanceof TextFrame)  
                )  
                ) return;  
                
                var name = app.selection[0].name;  
                
                if ( !props[name]   
                || !props[name].A  
                || !props[name].B) {  
                alert("No data for "+name );  
                return;  
                }  
                
                
                sws.item ("Template Colour A").colorValue = props[name].A;  
                sws.item ("Template Colour B").colorValue = props[name].B;  
              }  
                
                
              main();  
              

               

              Loic

              • 4. Re: Swatch Value Switch Script (layer palette labeled groups)
                JustOneBren Level 1

                Thanks Loic,

                 

                That works perfectly with Tabs 1&2 but as soon as I try it with "Tab 3" onward (at present there are 14 Tabs but this number could rise in future it comes back with the "No Data" message. I presume it needs some sort of counter so it can handle these extra tabs, sorry, I didn't make it clear in my original post there were more than 2.

                 

                Sorry

                Thanks again, Bren

                • 5. Re: Swatch Value Switch Script (layer palette labeled groups)
                  Loic.Aigon Adobe Community Professional

                  What your props object looks like at the moment ?

                  • 6. Re: Swatch Value Switch Script (layer palette labeled groups)
                    JustOneBren Level 1

                    I'm such a plank!

                    Found the answer,

                    "Tab 2": { A:[00,70,100,00], B:[5,90,00,00], },

                    "Tab 3": { A:[00,70,100,00], A:[5,90,00,00], },

                    Tab 3 onward I had a double 'A'

                    Your script worked like a dream

                    I will check and double check the simple stuff in future.

                     

                    Thank you for your help, Bren