3 Replies Latest reply on Sep 26, 2016 1:28 PM by samanthab1411754

    Script for text in rounded rectangle with image?

    samanthab1411754

      Hey guys,

       

      I need  help. I have a hundreds of text elements which all need to have the same specifics.

       

      Different text elements have to be in a rounded rectangular box, and always have the same font size and same font - this is easily done with a graphic style - easy. Here's the tricky part: after the copy I need to have an image (preferably vector so svg or eps) after the text. The distance from the last letter to the image must always be the same. The text might change. So think responsive text box fill with added with an image which changes spacing with the length of the text.

       

       

      I have attached an image to show exactly what I mean. I would love to have an automation/script for it as I have hundreds of text elements. I have tried to do it by myself but scripting is hard and graphic style isn't flexible with spacing and images. Any help or tips would be appreciated. Thanks again!

       

      example.png

        • 1. Re: Script for text in rounded rectangle with image?
          Silly-V Adobe Community Professional

          I think this could be solved potentially with the following technique:

          You can have your text in a point text frame which has some spaces after it as well as some character which would be colored in a different spot-color. The character could be anything, like an asterisk, and the spot color could also be black, but with a different name. Then, a script can copy each of these text objects, outline that copy and then find the shape that is colored with your special spot-color. The location of the found shape is then recorded into memory and the copy is deleted, so that as a final step, the money-bag symbol is pasted and centered into the recorded location.

          However, it would be even simpler if your money-bag art was already a font you use - then you wouldn't even need to do any scripting. If you can find a symbol font that is acceptable for your work, maybe some already have a symbol similar to this one.

          • 2. Re: Script for text in rounded rectangle with image?
            Silly-V Adobe Community Professional

            I got a following test to work, although a little differently.

             

            #target illustrator
            function test(){
              function centerTo(refObj, targetObj){
               var centerA, centerB;
               centerA = [(refObj.width / 2) + refObj.left, (-refObj.height / 2) + refObj.top];
               centerB = [(targetObj.width / 2) + targetObj.left, (-targetObj.height / 2) + targetObj.top];
               dX = centerA[0] - centerB[0];
               dY = centerA[1] - centerB[1];
               targetObj.translate(dX, dY);
              };
              function getSpecialCharacter(outlineObj){
              var thisCp;
              for (var i = 0; i < outlineObj.compoundPathItems.length; i++) {
              thisCp = outlineObj.compoundPathItems[i];
              if(thisCp.width >= 10.72 && thisCp.width <= 10.73 && thisCp.height >= 10.97 && thisCp.height <= 10.98){
              return thisCp;
              }
              }
              return null;
              };
              var doc = app.activeDocument;
              var t = doc.textFrames.getByName("Test");
              var dupe = t.duplicate(t, ElementPlacement.PLACEBEFORE);
              var dupeOutline = dupe.createOutline();
              var newSymbol = doc.symbolItems.add(doc.symbols.getByName("money-bag"));
              var specialChar = getSpecialCharacter(dupeOutline);
              if(specialChar == null){
              alert("Special Character not found");
              return;
              }
              centerTo(specialChar, newSymbol);
              dupeOutline.remove();
            };
            test();
            

             

            Screen Shot 2016-09-19 at 8.15.55 PM.pngScreen Shot 2016-09-19 at 8.23.10 PM.png

             

            The way I made this one work is, I configured my text frame text (named "Test" in the layers panel) to have an asterisk as the last character and I made it huge- big font size. I made sure to use subscript settings to some negative value to make the character come down on its baseline. I also made the rounded corner covnert-to-shape effect on a fill which was adjusted for relative width/height settings and also transformed vertically to be centered on the text. Then I outlined the text to manually record the width and height of the outline of the asterisk. Then in the code, I made the 'special character' be found on the condition that it is pretty much the same width/height as the shape. if(thisCp.width >= 10.72 && thisCp.width <= 10.73 && thisCp.height >= 10.97 && thisCp.height <= 10.98){

            Screen Shot 2016-09-19 at 8.04.33 PM.png

            I also created a money-bag symbol in the document, and the script will put it on the artboard and center it on the asterisk outline.

            Then the script removes the outline copy.

             

            One thing I also did was color the asterisk character as the same background as the rounded rectangle- because since its subscript setting affects the size/position of the background, it was important to keep it in there and not remove it. So the asterisk is still there in the final result, but it is just not seen.

             

            Screen Shot 2016-09-19 at 8.22.39 PM.pngScreen Shot 2016-09-19 at 8.15.48 PM.png

            • 3. Re: Script for text in rounded rectangle with image?
              samanthab1411754 Level 1

              Incredible! It worked!!! I made some small changes (added graphic style in the end). Thank you so much!