Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Nov 10, 2014 Nov 10, 2014

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

TOPICS
Scripting
436
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 11, 2014 Nov 11, 2014

Moved to the scripting forum....

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 11, 2014 Nov 11, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 11, 2014 Nov 11, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 11, 2014 Nov 11, 2014
LATEST

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines