11 Replies Latest reply on Feb 28, 2016 6:11 PM by Qwertyfly...

    Change italic character attribute to no italics.

    neiltonl5760991 Level 1

      Hello. Can anyone help me?

      I am not a programmer and I need a script to change a text of specific character in a Illustrator image, which are equations.

      The images contain "(" and ")" (no quotation marks) that are in italics and must change them to "regular / normal" (not italics).

      Excuse my English, I'm using google translate. thank you.

        • 1. Re: Change italic character attribute to no italics.
          Qwertyfly... Level 4

          this should get you started.

           

          var doc = app.activeDocument;
          var txt = doc.textFrames;
          for(var i=0; i<txt.length; i++){
              for(var j=0; j<txt[i].textRange.characters.length; j++){
                  var ch = txt[i].textRange.characters[j];
                  var letter = ch.contents;
                  if(letter === "("||letter === ")"){
                      if(ch.characterAttributes.textFont.style = "Italics"){
                          ch.characterAttributes.textFont=app.textFonts.getByName("MyriadPro-Regular"); 
                      }
                  }
              }
          }
          
          • 2. Re: Change italic character attribute to no italics.
            neiltonl5760991 Level 1

            Dear Qwertyfly...

            Fantastic! My nights sleep thank you immensely. It is the first time I use the forum and I am impressed with the return of the community.

            Thank you very much.

            • 3. Re: Change italic character attribute to no italics.
              neiltonl5760991 Level 1

              Now came another question, how can I run this batch script? This is as possible as is done actions?

              Thanks.

              • 4. Re: Change italic character attribute to no italics.
                Silly-V Adobe Community Professional

                One of the easiest ways to run a script on a batch of files is to record an action and use the Insert Menu Item Command inside the Action's fly-out menu to insert a menu item of a script which you have to have had already 'installed' into your Illustrator File > Scripts menu, by pasting that script into an appropriate folder in your Illustrator application folder.

                On my Windows machine, the folder is here: C:\Program Files\Adobe\Adobe Illustrator CC 2015\Presets\en_US\Scripts

                Once a script .jsx file is pasted into this folder, Illustrator has to be then restarted so that the script will appear inside the File > Scripts menu item.

                Now, you can do the recording of your action and as described above, use the Insert Menu Item command to put the script's menu item into the action.

                Using the batch action process, you can now play this script on an entire batch of files.

                 

                Not only is it a hassle to have to paste in a script and restart illustrator just to see your new script appear in the menu, but once you have recorded an action, you will find out that when you restart Illustrator, your new action will not work and complain the script is menu item is missing. This is one of the most frustrating bugs, but is easily and tediously remedied each time by clicking and hovering over the File > Scripts menu to first reveal the list of scripts, which somehow 'registers' the menu item for the actions palette to be able to use. Then you re-load the action to find your action-script back to working properly.

                 

                If you have enough actions that play scripts that this is a real bother, the most light-weight cross-platform, CS6+ version solution is to have a simple script whose job it is just to re-load actions from a specified folder, and to have it inside the File > Scripts menu. This way, each time you restart, you'll be able to 'fix' your actions with a single click inside of the File > Scripts menu because by the action of showing the File > Scripts menu, which you would have to do to run the re-loader, it will register all the menu items for use in the actions palette. For specific platforms where you can run platform-specific scripts such as AutoHotKey or VB for Windows and Applescript for Mac, this could be solved to remove all involvement from the user because those scripts could activate menu items and send mouse clicks and shortcut keys.

                 

                Furthermore, I have been thinking over an 'advanced actions technique' which could reduce the need for restarting Illustrator all the time when inserting new scripts for this purpose. It is possible to have one single script sitting inside the File > Scripts menu, whose sole job is to look for a name of the topmost layer and see if it corresponds to a script file name in a specified folder. Then it would read the script file as text and play it. This way you could put in new script snippets inside individual files inside of a folder, and inside of your actions you could assign which script to play by creating a temporary layer with a recorded name. To my knowledge, the few ways you can record your own custom text into an action is by adding a new note to an art item and making a new named layer (alt-click new layer button in the layers panel to produce the naming dialog box). The play-script script would also mop up by deleting the temporary topmost layer after reading its name.

                 

                I'll produce an article on the above convoluted explanation pretty soon!

                • 5. Re: Change italic character attribute to no italics.
                  Qwertyfly... Level 4

                  I made a mistake, in the italics test it should be === not =

                   

                  here is an update that will batch process a folder of pdfs.

                  WARNING!

                  This Will Overwrite Your Files.

                  Make Copies before running this script.

                  I take no responsibility for loss of data.

                   

                  
                  
                  var folder = Folder.selectDialog("Select Source Folder..."); 
                  var log = "Working Folder: " + folder + '\n';
                  if (folder !== null) {
                      var files = folder.getFiles("*.pdf");
                      var log = log + "Number of Files: " + files.length + '\n\n';
                      if (files.length > 0) {  
                          for (i = 0; i < files.length; i++) {  
                              var log = log + "File " + (i+1) + ": " + files[i] + '\n';
                              var doc = app.open(files[i]); 
                              var qty = modify(doc);
                              doc.close(SaveOptions.SAVECHANGES);
                              var log = log + qty + " Changes made" + '\n\n';
                          }
                          var log = log + "Finished." + '\n';
                          txtlog = new File(folder + '/textlog.txt');  
                          var isopen = txtlog.open("w"); //open file for editing  
                          if (isopen){ //test file is open  
                              txtlog.seek(0, 0);  
                              txtlog.write(log);  
                              txtlog.close();  
                          }  
                      }
                  }
                  
                  
                  function modify(doc){
                      var count = 0;
                      var txt = doc.textFrames;
                      for(var i=0; i<txt.length; i++){
                          for(var j=0; j<txt[i].textRange.characters.length; j++){
                              var ch = txt[i].textRange.characters[j];
                              var letter = ch.contents;
                              if(letter === "("||letter === ")"){
                                  if(ch.characterAttributes.textFont.style === "Italics"){
                                      count++
                                      ch.characterAttributes.textFont=app.textFonts.getByName("MyriadPro-Regular"); 
                                  }
                              }
                          }
                      }
                      return count;
                  }
                  
                  • 6. Re: Change italic character attribute to no italics.
                    neiltonl5760991 Level 1

                    Hi,

                    As I said the first script worked, but the second although running the whole procedure, the brackets not alternated. I made the switch back to = and it worked. Sorry about the delay in giving back, I was a little busy.

                    Thank you very much.

                    • 7. Re: Change italic character attribute to no italics.
                      neiltonl5760991 Level 1

                      Hello.

                      Still I did not realize the tests with your suggestion as soon as possible and I will give you a return. Thank you.

                      • 8. Re: Change italic character attribute to no italics.
                        Qwertyfly... Level 4

                        your italic font is not setting the textFont.style tag correctly.

                        with only one = you are trying to set the textFont.style to "italics"

                        the === is asking if textFont style is equal to "italics"

                         

                        so by having just one = you essentially skip that if statement.

                        this means all Brackets will be changed to MyriadPro-Regular, not just the ones in italics.

                        you could try this...

                         

                        but it depends on the font that the brackets are set to.

                         

                        1. if(ch.characterAttributes.textFont === app.textFonts.getByName("MyriadPro-It")){ 
                        2.                     count++ 
                        3.                     ch.characterAttributes.textFont=app.textFonts.getByName("MyriadPro-Regular");  
                        4.                 }
                        • 9. Re: Change italic character attribute to no italics.
                          neiltonl5760991 Level 1

                          Hi Qwertyfly...

                          I tested again with this suggestion and informed fonts used for file but had error in the name of the font ("LBRITEI") - Lucida Bright Italic. The file has another font "Lucida Sans Unicode". Thanks.

                           

                          if(ch.characterAttributes.textFont === app.textFonts.getByName("LBRITEI")){

                                              count++

                                              ch.characterAttributes.textFont=app.textFonts.getByName("LBRITE");

                                          }

                           

                          error.jpg

                          Sorry but my level programming is almost zero.

                          • 10. Re: Change italic character attribute to no italics.
                            neiltonl5760991 Level 1

                            Hi,

                            Problem solved. I test using the font name correctly.

                             

                              if(ch.characterAttributes.textFont === app.textFonts.getByName("LucidaBright-Italic")){

                                                count++

                                                ch.characterAttributes.textFont=app.textFonts.getByName("LucidaBright"); 

                             

                            Thank you.

                            • 11. Re: Change italic character attribute to no italics.
                              Qwertyfly... Level 4

                              glad you found the correct font name.

                              for future, you can use this line of code to see what font the selection is set to.

                              that way you can get the font name correct.

                               

                              alert(app.activeDocument.selection[0].textRange.characterAttributes.textFont.name);