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

To list all the glyphs and their counts used in a document from a specific font

Explorer ,
Sep 15, 2024 Sep 15, 2024

Copy link to clipboard

Copied

I am trying to make a script to list all the glyphs by their names or IDs and their counts in a document. The glyphs should be listed by their names or IDs (not by Unicode) as each Unicode character has several alternate shapes and I want to get result for each and every alternate shape. I have tried but in vain. Here is my script that I am working on.

 

// Get the active document
var doc = app.activeDocument;

// Define an object to store the glyph name counts
var glyphCounts = {};

// Define the output file path
var outputFile = File("~/Desktop/GlyphCount.txt");
outputFile.open("w");

// Write the initial line to the file
outputFile.writeln("Glyph Counts for the font: Arabic Font 1");

// Function to process characters and count glyphs
function countGlyphs(character) {
    try {
        // Check if the character has glyphs
        if (character.hasOwnProperty('glyphs') && character.glyphs.length > 0) {
            var glyph = character.glyphs[0]; // Get the first glyph object
            var glyphName = glyph.glyphName; // Get the glyph name
            
            // Use a placeholder if glyphName is undefined
            if (!glyphName) {
                glyphName = "UnknownGlyph";
            }

            // Update the glyph count based on the glyph name
            if (glyphCounts[glyphName] === undefined) {
                glyphCounts[glyphName] = 1;
            } else {
                glyphCounts[glyphName]++;
            }

            // Debug output using alerts
            alert("Glyph: " + glyphName + ", Count: " + glyphCounts[glyphName]);
        } else {
            alert("No glyphs found for character.");
        }
    } catch (e) {
        // Log errors if glyph retrieval fails
        alert("Error processing character. Error: " + e.toString());
    }
}

// Loop through all stories (text frames) in the document
for (var i = 0; i < doc.stories.length; i++) {
    var story = doc.stories[i];

    // Loop through all characters in the story
    for (var j = 0; j < story.characters.length; j++) {
        var character = story.characters[j];
        var font = character.appliedFont.name;

        // Check if the character is from the specific font you're interested in
        if (font === "Arabic Font 1") {
            countGlyphs(character);
        }
    }
}

// Write the glyph counts to the file
for (var glyphName in glyphCounts) {
    outputFile.writeln("Glyph: " + glyphName + ", Count: " + glyphCounts[glyphName]);
}

// Close the file
outputFile.close();

// Notify the user
alert("Glyph count saved to " + outputFile.fsName);


Any help is greatly appreciated.

TOPICS
Scripting

Views

516

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 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

This is a fascinating discussion. I wish I understood more of it.

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
Guide ,
Sep 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

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 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

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 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

LATEST

@zuhair777

 

Why your link is no linger valid?

 

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