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

InDesign Script to Auto Apply certain CharStyle when Punctuation is used with specific Font

New Here ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

I'm using a custom font for my headers in InDesign called Xillian Family, which doesn't have any punctuation marks. If I need to use punctuation within a header, I apply a font called Yeseva One. I'm trying to automate this process by writing an indesign script that will automatically apply a Yeseva One character style to any punctuation within a text range or text frame that uses Xillian Family. I tried using the script below, but it's not working.

 

// Define the font name and the character style name
var fontName = "Xillian Family";
var cStyleName = "Yeseva One";

// Get a reference to the font and the character style, and create the character style if it doesn't exist
var font = app.fonts.itemByName(fontName);
var cStyle;
if (app.activeDocument.characterStyles.itemByName(cStyleName) == null) {
  cStyle = app.activeDocument.characterStyles.add({name: cStyleName, appliedFont: "Yeseva One"});
} else {
  cStyle = app.activeDocument.characterStyles.itemByName(cStyleName);
}

// Define a function to apply the character style to punctuation marks in a text range
function applyCharacterStyleToPunctuation(textRange) {
  for (var j = 0; j < textRange.characters.length; j++) {
    var character = textRange.characters[j];

    // Check if the character is a punctuation mark
    if (/[\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E]/.test(character.contents)) {

      // Check if the text range still exists
      if (textRange.isValid) {
        // Apply the character style to the punctuation mark
        character.appliedCharacterStyle = cStyle;
      }
    }
  }
}

// Define a function to handle the "afterSelectionChanged" event
function onSelectionChanged(event) {
  var selection = app.selection[0];

  // Check if the selection is a text frame or text range
  if (selection instanceof TextFrame || selection instanceof InsertionPoint || selection instanceof Character) {

    // Loop through all the text ranges in the selection
    for (var i = 0; i < selection.textStyleRanges.length; i++) {
      var textRange = selection.textStyleRanges[i];

      // Check if the text range uses the Xillian Family font
      if (textRange.appliedFont == font) {

        // Apply the character style to punctuation marks in the text range
        applyCharacterStyleToPunctuation(textRange);
      }
    }
  }
}

// Add an event listener for the "afterSelectionChanged" event
app.eventListeners.add("afterSelectionChanged", onSelectionChanged);

 

TOPICS
Scripting

Views

240

Translate

Translate

Report

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

You can do this with GREP or Nested styles in paragraph styles.

Similar to the instructions here

https://creativepro.com/5-cool-things-you-can-do-with-grep-styles/

 

 

Votes

Translate

Translate

Report

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

Hi @Kallista262462118vle , In your case the GREP Style code Eugene is suggesting would probably be [[:punc:]]. Here the Paragraph Style named Xillian Style would apply the Character Style named Yeseva One to all the paragraph’s puncuation

 

Screen Shot.png

Votes

Translate

Translate

Report

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

LATEST

yes - that's good for most punctuation - but I do notice it doesn't find a minus sign and a few other out there characters that are punctuation. I'm not sure why.

 

So if there are any further characters you need you can include them with the same steps - new style and include extra punctuation this way

You can insert or remove any of the punctuation required. 

Where the grep for punct as pointed out is good - I found it doesn't always catch all.

 

The below is just an example of how to insert various combinations of different punctuation that may be missed for some reason.

 

[\,\.\?\!;\:\'\"\(\)\[\]\{\}\-\—\–\/\\]

This character class includes:

Comma: ,
Period: .
Question mark: ?
Exclamation point: !
Semicolon: ;
Colon: :
Apostrophe: '
Quotation marks: "
Parentheses: ()
Brackets: []
Curly braces: {}
Hyphens: -, —, and –
Slashes: / and \

Votes

Translate

Translate

Report

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