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

Getting different fontStyles in the document for the same font

Explorer ,
Sep 21, 2023 Sep 21, 2023

Copy link to clipboard

Copied

Hi Everyone,

 

I am trying to get fontStyle for the characters through scripting by the below code,

 

app.findGrepPreferences = changeGrepPreferences = NothingEnum.nothing;
app.findChangeGrepOptions.includeFootnotes = true;
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeMasterPages = false;
app.findGrepPreferences.findWhat = ".";
var findss = app.activeDocument.findGrep();
for(j=0;j<findss.length;j++){
$.writeln(findss[j].characters[0].fontStyle)
}

 

but i am getting both 'book' fontStyle and 'Regular' for the different contents. I am using 'adobe caslon pro-regular' font.

 

Screenshot 2023-09-22 at 7.58.52 am.png

I don't know what is wrong with the code or the font, file. Attached files for your reference.

Please help me to sort this issue.

 

Thanks,

SathishS

TOPICS
Scripting

Views

913

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

correct answers 1 Correct answer

Community Expert , Sep 25, 2023 Sep 25, 2023

How bizarre. When you select the paragraph 'continental railroad . . . land' it shows as Regular.

 

The Find/Replace Font window doesn't see Caslon Book (it should be listed there in brackets to show it's there but doesn't exist -- existentially challenging but there you are.

 

And it should show up in the Find/Change dialog (again, in brackets), but it doesn't. But it's there in the document:

 

 

ranges = ...

for (i = 0; i < ranges.length; i++) {
  $.writeln (ranges[i].appliedFont.name);
}

 

 

...

Votes

Translate

Translate
Community Expert ,
Sep 21, 2023 Sep 21, 2023

Copy link to clipboard

Copied

FindWhat = '.' will return whole texts and GREP is rather overkill?

 

If you want to find texts with specific formatting - you can use Text find but you still need to define what formatting you want to find.

 

I'm not JS guy so can't give you exact code. 

 

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
Explorer ,
Sep 21, 2023 Sep 21, 2023

Copy link to clipboard

Copied

FindWhat = '.' will return any single character.

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 ,
Sep 22, 2023 Sep 22, 2023

Copy link to clipboard

Copied

Not exactly, without anything else - it will return all texts - not single characters - so it would be quicker to just iterate all Stories.

 

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 ,
Sep 22, 2023 Sep 22, 2023

Copy link to clipboard

Copied

It's true that "." finds any single character -- but it's also true that it finds all characters, so your script writes every single character (apart from the paragraph break).

 

Better to use styleRanges, then write those font style of those ranges.

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
Explorer ,
Sep 24, 2023 Sep 24, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel,

Thanks for responding... I am new to the scripting. Could you help me where to implement 'styleRanges' in above code.

 

-Sathish

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

You'd do something like this:

ranges = app.activeDocument
  .stories.everyItem()
  .textStyleRanges.everyItem().getElements();

for (i = 0; i < ranges.length; i++) {
  if (ranges[i].appliedFont.name === 'Adobe Caslon Pro\tRegular') {
    // Do something;
  }
}

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

Great piece of code - as always. 

 

But OP needs to remember about one thing - if consecutive pieces of text have the same font, but differ in something else - point size, color, etc. - each piece will be returned as a separate result - which may be a problem, when whole block of text should be processed. 

 

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

True. But you'd have the same problem when looking for .+ and the font name+style.

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

No, you wouldn't:

 

RobertTkaczyk_1-1695637779717.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
Explorer ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel,

 

Thanks for the Code!!! 

 

As attached indesign file, everything is typeset in 'Adobe Caslon Pro', but when i am trying to evaluate the fontStyle of the characters, it returns 'Regular' fontStyle for second textframe and 'book' for first frame. Even there is no 'Book' fontstyle available in this font 'Adobe Caslon Pro'.

 

Screenshot 2023-09-25 at 3.55.56 pm.png

 

 

 

 

 

 

 

 

 

-Sathish

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

But the TextStyleRange can be as small as InsertionPoint - you need to check "type" as well.

 

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

How bizarre. When you select the paragraph 'continental railroad . . . land' it shows as Regular.

 

The Find/Replace Font window doesn't see Caslon Book (it should be listed there in brackets to show it's there but doesn't exist -- existentially challenging but there you are.

 

And it should show up in the Find/Change dialog (again, in brackets), but it doesn't. But it's there in the document:

 

 

ranges = ...

for (i = 0; i < ranges.length; i++) {
  $.writeln (ranges[i].appliedFont.name);
}

 

 

And you can select it (or one of the instances) as follows, if only to show that your eyes don't deceive you:

 

ranges = ...

for (i = 0; i < ranges.length; i++) {
  if (ranges[i].appliedFont.name === 'Adobe Caslon Pro\tBook') {
    ranges[i].select();
    exit();
  }
}

 

And you can change it as follows:

 

ranges = ...
for (i = 0; i < ranges.length; i++) {
  if (ranges[i].appliedFont.name === 'Adobe Caslon Pro\tBook') {
    ranges[i].appliedFont = 'Adobe Caslon Pro\tRegular'
  }
}

 

Maybe some mild corruption. I'd be careful with that document. Perhars it's a good idea to do an IDML roundtrip.

 

(Edit: Some duplication here, hadn't seen your and Robert's replies when I answered.)

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
Explorer ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

Thanks @Peter Kahrel,

 

But XMP cleanup and idml didn't helped. Still getting the same issue.

-Sathish

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
Explorer ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

But after change everything that finds 'Book' to 'Regular', now it all returns as 'Regular' font style. But don't have any clue why it behaves like that.

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

LATEST

But don't have any clue why it behaves like that.

 

It's very strange indeed.

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

That's what I get:

RobertTkaczyk_0-1695639957346.png

 

And Find&Change from UI - even with ".*":

RobertTkaczyk_0-1695640066963.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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

But as you can see on my 1st screenshot - not highlighted - ",?!." in the 2nd TextFrame have different formatting - Kerning set to "Optical"

 

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 ,
Sep 25, 2023 Sep 25, 2023

Copy link to clipboard

Copied

And here is when combined with GREP Find results - ".*":

RobertTkaczyk_0-1695641685183.png

First Found - is EXACTLY the same as TextStyleRange and Paragraph - but 2nd Found - highlighted - is a whole 2nd TextFrame - ignores Kerning option.

 

And from here - there is only one small step to very advanced Text Preflight...

 

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