9 Replies Latest reply on Jun 14, 2016 8:19 AM by Puthoskey

    Finding objects

    Puthoskey

      Good morning guys!

      How could I know if an object with the attribute “AL” exists in my current document?

      If file exists, it displays an AlertBox.

       

      Many thanks in advance guys :-)

        • 1. Re: Finding objects
          Silly-V Adobe Community Professional

          Do you mean a note with "AL"?

          • 2. Re: Finding objects
            Puthoskey Level 1

            Yeah... with a note with "AL" Silly!

            • 3. Re: Finding objects
              Silly-V Adobe Community Professional

              Then prepare to gape in amazement as this script here fulfills your most extravagant wishes, regarding only this particular case of yours!

               

              #target illustrator-19
              function test(){
              
                String.prototype.hexEncode = function(){ 
                //http://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex 
                  var hex = ''; 
                  for(var i=0;i<this.length;i++) { 
                      hex += ''+this.charCodeAt(i).toString(16); 
                  } 
                  return hex; 
                };
              
              
                function writeFile(fileDestStr, contents){ 
                  var newFile = File(fileDestStr); 
                  newFile.open('w'); 
                  newFile.write(contents); 
                  newFile.close(); 
                }; 
                 
                var NOTE_VALUE = "AL"; 
              
                var actionString = [
                  "/version 3",
                  "/name [ 4",
                  " 54657374", /* Test */
                  "]",
                  "/isOpen 1",
                  "/actionCount 2",
                  "/action-1 {",
                  " /name [ 13",
                  " 53656c6563744f626a65637473", /* SelectObjects */
                  " ]",
                  " /keyIndex 0",
                  " /colorIndex 0",
                  " /isOpen 1",
                  " /eventCount 1",
                  " /event-1 {",
                  " /useRulersIn1stQuadrant 0",
                  " /internalName (adobe_setSelection)",
                  " /localizedName [ 13",
                  " 5365742053656c656374696f6e",
                  " ]",
                  " /isOpen 0",
                  " /isOn 1",
                  " /hasDialog 0",
                  " /parameterCount 3",
                  " /parameter-1 {",
                  " /key 1952807028",
                  " /showInPalette -1",
                  " /type (ustring)",
                  " /value [ PUT_NOTE_VALUE_CHAR_LENGTH_HERE",
                  " PUT_HEX_NOTE_VALUE_HERE",
                  " ]",
                  " }",
                  " /parameter-2 {",
                  " /key 2003792484",
                  " /showInPalette -1",
                  " /type (boolean)",
                  " /value 0",
                  " }",
                  " /parameter-3 {",
                  " /key 1667330917",
                  " /showInPalette -1",
                  " /type (boolean)",
                  " /value 1",
                  " }",
                  " }",
                  "}",
                  "/action-2 {",
                  " /name [ 13",
                  " 4c6f636174654f626a65637473", /* LocateObjects */
                  " ]",
                  " /keyIndex 0",
                  " /colorIndex 0",
                  " /isOpen 1",
                  " /eventCount 1",
                  " /event-1 {",
                  " /useRulersIn1stQuadrant 0",
                  " /internalName (ai_plugin_Layer)",
                  " /localizedName [ 5",
                  " 4c61796572",
                  " ]",
                  " /isOpen 0",
                  " /isOn 1",
                  " /hasDialog 0",
                  " /parameterCount 2",
                  " /parameter-1 {",
                  " /key 1836411236",
                  " /showInPalette -1",
                  " /type (integer)",
                  " /value 19",
                  " }",
                  " /parameter-2 {",
                  " /key 1851878757",
                  " /showInPalette -1",
                  " /type (ustring)",
                  " /value [ 13",
                  " 4c6f63617465204f626a656374",
                  " ]",
                  " }",
                  " }",
                  "}"
                ].join("\n"); 
              
                if(app.documents.length == 0){ 
                  return; 
                } 
                 
                var actionFileDestStr = Folder.desktop + "/MyAction.aia";
              
              
                writeFile(actionFileDestStr, actionString.replace("PUT_NOTE_VALUE_CHAR_LENGTH_HERE", NOTE_VALUE.length).replace("PUT_HEX_NOTE_VALUE_HERE", NOTE_VALUE.hexEncode())); 
                var actionFile = File(actionFileDestStr); 
                app.loadAction(actionFile); 
                app.doScript("SelectObjects", "Test");
                if(app.activeDocument.selection != null && app.activeDocument.selection.length > 0){
                  app.doScript("LocateObjects", "Test");
                }
              
                //clean up 
                actionFile.remove();
                app.unloadAction("Test", ''); // thanks qwertyfly!
              };
              test();
              
              • 4. Re: Finding objects
                CarlosCanto Adobe Community Professional & MVP

                hahaha I'm completely amazed!!

                • 5. Re: Finding objects
                  Stephen_A_Marsh Adobe Community Professional

                  Vasily, Carlos – not being a scripter, what am I missing out on here?!

                   

                  I am usually quite critical (scathing?) of Illustrator’s Actions when compared to Photoshop’s implementation, however in this case a single action step (select object) can do the same thing as 124 lines of code!

                   

                  So apart from being able to include this code into a larger script, what is the advantage? Please put me out of my misery! ;]

                  • 6. Re: Finding objects
                    Silly-V Adobe Community Professional

                    That what you see here in my snippet is actually contents of an actions file (.aia) which is stored as text in memory and then written to a user's desktop as a .aia file, loaded into the Illustrator Actions panel, played, and then both removed from the actions panel and also erased off the desktop. If these actions were reliably on a person's computer, then a script can simply play the actions with a single line of code - however if we are to change the parameter of the "Select Object" action command which instantly selects all objects with a specified note attribute, this dynamic variable has to be passed into an action, which means that dynamic building and loading of the action is necessary to have it select a note which is supplied by the rest of the script.

                    This code can be used by anyone to create custom tools such as a floating panel which selects and finds items based on the user input inside of a textbox in the panel, or any other creative use they can think up!

                    • 7. Re: Finding objects
                      Stephen_A_Marsh Adobe Community Professional

                      Thanks Vasily, if Carlos was impressed then I knew enough that something special was going on! That sounds really cool, thanks for the explanation.

                      • 8. Re: Finding objects
                        CarlosCanto Adobe Community Professional & MVP

                        you're right Stephen, I think the selecting the objects was all it was needed, but sv went fancy and located them in the layers panel as well

                        • 9. Re: Finding objects
                          Puthoskey Level 1

                          Many thanks

                          Worked like a charm >D