Fonts.length Inaccuracy?

Explorer ,
Jul 05, 2022 Jul 05, 2022

Copy link to clipboard

Copied

Hi, I've made a simple script (attached as txt) that scrapes each font's values into a csv and getting a weird discrepancy between fonts.length and the number of fonts in the output. Any idea why this is happening and if I should be concerned? Thanks.

 

I added a confirmation message at finish to report what happened.

 

nickcombs_1-1657057728758.png

 

You can see it reports 1366 fonts and none were skipped. I added the second part as a diagnostic because when I open the output file, I only see 1305 fonts listed .I was originally displaying fonts.length and also getting 1366, but now I've used a loop counter and a conditional checking each property exists to try to confirm there's no unreported error for those 61 fonts. Same result.

 

nickcombs_2-1657057989631.png

TOPICS
Actions and scripting

Views

70

Likes

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

Adobe Community Professional , Jul 05, 2022 Jul 05, 2022

Tried on four computers and couldn't reproduce the error ¯\_(ツ)_/¯

You can add additional checks:

a) check how many times the condition is true (this shouldn't be a problem, but it's best to check)

 

if( font.name && font.family && font.style && font.postScriptName )

 

b) check if the operation returns true each time (due to the large number of file openings/closings, it is possible that some lines cannot be written)

outFile.write( row.join(',') + '\n' )

 

Also you can try this way (it's about 1

...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 05, 2022 Jul 05, 2022

Copy link to clipboard

Copied

Tried on four computers and couldn't reproduce the error ¯\_(ツ)_/¯

You can add additional checks:

a) check how many times the condition is true (this shouldn't be a problem, but it's best to check)

 

if( font.name && font.family && font.style && font.postScriptName )

 

b) check if the operation returns true each time (due to the large number of file openings/closings, it is possible that some lines cannot be written)

outFile.write( row.join(',') + '\n' )

 

Also you can try this way (it's about 10x faster by accessing the font list through the ActionManager and reducing the number of file open and close operations😞

 

const scriptName = 'Create Font List'
const scriptVersion = '1.0'
const summary = 'This tool exports a CSV containing information about the fonts in the font library'
CreateFontList()
function CreateFontList() {
    try {
        var row = [],
            s2t = stringIDToTypeID;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('fontList'));
        r.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
        var fontsList = executeActionGet(r).getObjectValue(p);
        row.push('font.name,font.family,font.style,font.postScriptName')
        var fontNames = fontsList.getList(s2t('fontName')),
            fontFamilyNames = fontsList.getList(s2t('fontFamilyName')),
            fontStyleNames = fontsList.getList(s2t('fontStyleName')),
            fontPostScriptNames = fontsList.getList(s2t('fontPostScriptName')),
            fontLen = fontsList.getList(s2t('fontPostScriptName')).count,
            recorded = 0;
        for (var i = 0; i < fontLen; i++) {
            row.push(
                [
                    fontNames.getString(i),
                    fontFamilyNames.getString(i),
                    fontStyleNames.getString(i),
                    fontPostScriptNames.getString(i)
                ].join(','))
            recorded++
        }
        var outFile = new File(Folder.desktop + '/FontList.csv')
        outFile.open('w')
        outFile.encoding = "UTF8"
        outFile.write(row.join('\n'))
        outFile.close()
        // font loop
        if (outFile.exists && !outFile.error) {
            const font_size = 14
            const btn_w = 135
            const btn_h = 35
            var win = new Window('dialog')
            win.text = scriptName + ' ' + scriptVersion
            var txt = win.add('statictext', undefined, undefined, { multiline: true })
            txt.text = [outFile.fullName + ' was created successfully.',
            'It contains the ' + recorded + ' fonts in your local library.',
            (fontLen - recorded) + ' fonts could not be recorded.'].join('\n\n')
            txt.graphics.font = 'dialog:' + font_size
            txt.preferredSize.width = 350
            txt.justify = 'center'
            var grp = win.add('group')
            var btn_Open = grp.add('button')
            btn_Open.text = 'Open List'
            btn_Open.graphics.font = 'dialog:' + font_size
            btn_Open.preferredSize = [btn_w, btn_h]
            var btn_OK = grp.add('button')
            btn_OK.text = 'OK'
            btn_OK.graphics.font = 'dialog:' + font_size
            btn_OK.preferredSize = [btn_w, btn_h]
            btn_Open.onClick = function () {
                outFile.execute()
                win.close()
            } // btn_Open.onClick
            win.show()
        } // if success
    } catch (e) { alert('CreateFontList ' + e) }
} // CreateFontList

 

 

Likes

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 ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

Thanks, @jazz-y! That has solved the issue. I'm now seeing all of the fonts recorded in the sheet. I did a bit more testing on my end and it turns out this command is making the difference:

outFile.encoding = "UTF8"

Penabico Ornaments Two appears to be the font where it stopped being able to write with the default encoding.

 

I also confirmed the actman call is much more performant. The dom call script takes over 600 ms for me while the yours takes about 30 ms.

 

Good to know that rapid file ops could be a future concern. I haven't encountered that myself, and I do a lot of debug logging to files. But it might become a system-specific issue, so I'll try to write in chunks going forward.

Likes

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 ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

LATEST

I've updated my code to write in chunks (attached as txt), and it turns out that made up most of the performance loss. It takes about 40 ms this way and I find the dom calls more readable.

 

This version includes the utf8 encoding too. I also removed the conditional that was checking all the properties, since it doesn't appear to be needed.

Likes

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