4 Replies Latest reply on Nov 11, 2014 4:26 AM by Kai Rübsamen

    Find font, if nothing find then no need to put char style

    Murali_Vetha Level 1

      Hi All,

       

      In the below coding is to find Italic and apply char style, if there is no char style then add new char style, But in my code doing if there is no Italic created new char style, but I need if there is no italic font nothing to do. Please advice.

       

      app.findTextPreferences = app.changeTextPreferences =null;

      app.findChangeTextOptions.includeMasterPages = false;

      app.findTextPreferences.fontStyle ="Italic";

      app.findTextPreferences.position = Position.NORMAL;

      app.findTextPreferences.capitalization = Capitalization.NORMAL;

       

       

        try{

        app.changeTextPreferences.appliedCharacterStyle ="15 Italic";

        app.documents.item(0).changeText();

        }

        catch(e){

        app.documents.item(0).characterStyles.add({name:"15 Italic", fontStyle:"Italic"});

        app.changeTextPreferences.appliedCharacterStyle ="15 Italic";

        app.documents.item(0).changeText();

        }

       

       

      Regards,

      Vetha

        • 1. Re: Find font, if nothing find then no need to put char style
          Peter Spier Most Valuable Participant (Moderator)

          Moved to the scripting forum....

          • 2. Re: Find font, if nothing find then no need to put char style
            Chinnadk Level 4

            Hi Vetha,

             

            Try this,

             

            var doc = app.activeDocument;
            var italic = doc.characterStyles.item("15 Italic");
            if(!italic.isValid)
            {
                      italic = doc.characterStyles.add({name:"15 Italic", fontStyle:"Italic"})
                }
            app.findTextPreferences = app.changeTextPreferences =null;
            app.findChangeTextOptions.includeMasterPages = false;
            app.findTextPreferences.fontStyle ="Italic";
            app.findTextPreferences.position = Position.NORMAL;
            app.findTextPreferences.capitalization = Capitalization.NORMAL;
            app.changeTextPreferences.appliedCharacterStyle =italic;
            app.documents.item(0).changeText();
            
            

             

            Regards,

            Chinna

            • 3. Re: Re: Find font, if nothing find then no need to put char style
              Sajeev Sridharan Level 4

              hope the below code suits your requirement,

               

              app.findGrepPreferences = app.changeGrepPreferences =null;
              app.findChangeTextOptions.includeMasterPages = false;
              app.findGrepPreferences.findWhat = ".+";    
              app.findGrepPreferences.fontStyle ="Italic";
              app.findGrepPreferences.position = Position.NORMAL;
              app.findGrepPreferences.capitalization = Capitalization.NORMAL;
              var found = app.activeDocument.findGrep();   
                try{
                app.changeGrepPreferences.appliedCharacterStyle ="15 Italic";
                app.documents.item(0).changeGrep();
                }
                catch(e){
                if(found.length>0){
                  app.documents.item(0).characterStyles.add({name:"15 Italic", fontStyle:"Italic"});
                  app.changeGrepPreferences.appliedCharacterStyle ="15 Italic";
                  app.documents.item(0).changeGrep();
                  }
                }
              

               

              Vandy

              • 4. Re: Re: Re: Find font, if nothing find then no need to put char style
                Kai Rübsamen Adobe Community Professional

                or this one:

                 

                var curDoc = app.activeDocument;
                
                app.findTextPreferences = app.changeTextPreferences = null;  
                app.findChangeTextOptions.includeMasterPages = false;  
                app.findTextPreferences.fontStyle = "Italic";  
                app.findTextPreferences.position = Position.NORMAL;  
                app.findTextPreferences.capitalization = Capitalization.NORMAL;  
                
                var findResult = curDoc.findText();
                
                if ( findResult.length > 0 ) { 
                  var italic = curDoc.characterStyles.item( "15 Italic" );  
                  if ( !italic.isValid ) {  
                    italic = curDoc.characterStyles.add({ name: "15 Italic", fontStyle: "Italic" });
                  } 
                  app.changeTextPreferences.appliedCharacterStyle = italic;  
                  curDoc.changeText();
                }
                app.findTextPreferences = app.changeTextPreferences =null;