Skip to main content
Inspiring
January 21, 2025
Answered

The script works on Windows, but crashes on MacOs

  • January 21, 2025
  • 2 replies
  • 853 views

Hello. I have made my script. It works fine on windows. When I decided to run it on a macbook, InDesign crashed immediately. I spent some time trying to find the cause.
I have two drop-down lists in my script. One shows all the fonts available in the system, and the other shows all its styles (regular, bold, italic, etc.). I don't know why, but on windows everything works fine, but on macbook m1 on macOS sequoia it doesn't want to work.

var GroupFont = panelTextTab.add("group");
GroupFont.orientation = "row";
GroupFont.add("statictext", undefined, "Font:");

// Getting an array of Font objects
var fontObjects = app.fonts.everyItem().getElements();
var uniqueFontNames = []; // Array for unique font names

// Collecting unique font families
for (var i = 0; i < fontObjects.length; i++) {
    var fontFamily = fontObjects[i].fontFamily;
    // Check if the font already exists in the array
    var isFound = false;
    for (var j = 0; j < uniqueFontNames.length; j++) {
        if (uniqueFontNames[j] === fontFamily) {
            isFound = true;
            break;
        }
    }
    if (!isFound) {
        uniqueFontNames.push(fontFamily);
    }
}

// Sort the array of fonts
uniqueFontNames.sort();

var defaultFontIndex = -1;
// Search for the DejaVu Sans index in the list of unique fonts
for (var i = 0; i < uniqueFontNames.length; i++) {
    if (uniqueFontNames[i].indexOf("DejaVu Sans") !== -1) {
        defaultFontIndex = i;
        break;
    }
}

var fontDropdown = GroupFont.add("dropdownlist", undefined, uniqueFontNames);
if (defaultFontIndex !== -1) {
    fontDropdown.selection = defaultFontIndex;
} else {
    fontDropdown.selection = 0;
    alert("Шрифт 'DejaVu Sans' не знайдено в системі. Встановіть його та спробуйте ще раз.");
}

var GroupFontBold = panelTextTab.add("group");
GroupFontBold.orientation = "row";
GroupFontBold.add("statictext", undefined, "Style:");
var FontBoldDropdown = GroupFontBold.add("dropdownlist", undefined, []);

// Function to update the outline depending on the selected font
function updateFontStyles(selectedFont) {
    var styles = [];
    // Collect all outlines for the selected font family
    for (var i = 0; i < fontObjects.length; i++) {
        if (fontObjects[i].fontFamily === selectedFont) {
            styles.push(fontObjects[i].fontStyleName);
        }
    }
    
    // Sorting the styles
    styles.sort();
    
    // Clear the list
    FontBoldDropdown.removeAll();
    
    // Add new elements
    for (var j = 0; j < styles.length; j++) {
        FontBoldDropdown.add("item", styles[j]);
    }
    
    // Set the first element by default
    if (styles.length > 0) {
        FontBoldDropdown.selection = 0;
    }
    
    return styles; // Return an array of styles
}

// Initial filling of the outline for the default font
updateFontStyles(fontDropdown.selection.text);

fontDropdown.onChange = function() {
    updateFontStyles(fontDropdown.selection.text);
    if (previewCheckbox.value === true) {
        updatePreview(previewCheckbox.value);
    }
};

FontBoldDropdown.onChange = function() {
    if (previewCheckbox.value === true) {
        updatePreview(previewCheckbox.value);
    }
};


No matter how hard I tried, I can't get the list of fonts and their styles to appear in two separate drop-down lists. Once I managed to get the list of all fonts, but they were with all styles at once, like this:

var GroupFont = panelTextTab.add("group");
GroupFont.orientation = "row";
GroupFont.add("statictext", undefined, "Шрифт:");

var fontObjects = app.fonts.everyItem().getElements();
var fontNames = [];
var defaultFontIndex = -1;
for (var i = 0; i < fontObjects.length; i++) {
    fontNames.push(fontObjects[i].name);
    if (fontObjects[i].name.indexOf("DejaVu Sans") !== -1) {
        defaultFontIndex = i;
    }
}
var selectedFontValue;
if (defaultFontIndex !== -1) {
    selectedFontValue = fontObjects[defaultFontIndex].name; //Отримуємо точну назву шрифта
}
var fontDropdown = GroupFont.add("dropdownlist", undefined, fontNames);
if (defaultFontIndex !== -1) {
    fontDropdown.selection = defaultFontIndex;
} else {
    fontDropdown.selection = 0;
    alert("Шрифт ʼDejaVu Sans' не знайдено в системі. Встановіть його та спробуйте ще раз.");
}

Maybe someone knows how to implement this on a Mac?

Correct answer Funfy
quote

Okay, I did what you said. However, the problem is with the font. But how do I find it? I have 2000+ fonts on my system :\.


By @Funfy

 

You should change the way you work... 

 

Do you always, in every document - need 2000+ fonts? 

 

It's best to keep fonts used in the document - in the subfolder, next to your INDD document - Document Fonts. 

 


Okay, I think I've found a solution. I added the following check.

if (/[^a-zA-Z0-9\s]/.test(fontFamily)) {
        continue;
    }


But I'm not sure how stable it is?

2 replies

Legend
January 21, 2025

There was a recent discussion with the same kind of script. If this is not about homework, maybe borrow some code from there.

https://community.adobe.com/t5/indesign-discussions/save-only-one-fontfamilyname-in-a-dropdownlist/m-p/15091056#M605888

 

Anyway a script should not crash InDesign. Share the crash log (pastebin?) and provide a link here.

As you're dealing with fonts anyway, a bad font could be a reason. Disable user fonts by temporarily moving them somewhere else, then retry.

FunfyAuthor
Inspiring
January 21, 2025

Okay, I did what you said. However, the problem is with the font. But how do I find it? I have 2000+ fonts on my system :\.

Robert at ID-Tasker
Legend
January 21, 2025
quote

Okay, I did what you said. However, the problem is with the font. But how do I find it? I have 2000+ fonts on my system :\.


By @Funfy

 

You should change the way you work... 

 

Do you always, in every document - need 2000+ fonts? 

 

It's best to keep fonts used in the document - in the subfolder, next to your INDD document - Document Fonts. 

 

Robert at ID-Tasker
Legend
January 21, 2025

What crashes? Whole InDesign or just script?

 

Do you have any error messages - script is crashing?

 

Try adding alerts() - with a line number or something - every few lines, so even if inDesing is crashing - you should get to the place where is the problem.

 

FunfyAuthor
Inspiring
January 21, 2025

AdobeInDesign crashes. When I run the script, the application crashes immediately.