The script works on Windows, but crashes on MacOs
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?
