Answered
save only one fontFamilyName in a dropdownlist.
Hi
I'm writing a script with the help of copilot.
I want to save only one fontFamilyName in a dropdownlist.
How can I do that?
#target indesign
// Create a new dialog
var dialog = new Window("dialog", "Choose a Font");
// Add a dropdown list for font family
var fontFamilyDropdown = dialog.add("dropdownlist", undefined, []);
fontFamilyDropdown.size = [200, 30];
// Add a dropdown list for font style
var fontStyleDropdown = dialog.add("dropdownlist", undefined, []);
fontStyleDropdown.size = [200, 30];
// Populate the font family dropdown list with all font families
var allfonts = app.fonts.everyItem().getElements();
for (var i = 0; i < allfonts.length; i++) {
fontFamilyDropdown.add("item", allfonts[i].fontFamily);
}
// Set the first item as the default selection
fontFamilyDropdown.selection = 0;
// Function to update the font style dropdown based on the selected font family
function updateFontStyleDropdown() {
fontStyleDropdown.removeAll();
var selectedFontFamily = fontFamilyDropdown.selection.text;
var fontStyles = [];
for (var i = 0; i < allfonts.length; i++) {
if (allfonts[i].fontFamily === selectedFontFamily) {
fontStyles.push(allfonts[i].fontStyleName);
}
}
for (var j = 0; j < fontStyles.length; j++) {
fontStyleDropdown.add("item", fontStyles[j]);
}
fontStyleDropdown.selection = 0;
}
// Update the font style dropdown when a new font family is selected
fontFamilyDropdown.onChange = updateFontStyleDropdown;
// Add a button to close the dialog
dialog.add("button", undefined, "OK");
// Initial population of font style dropdown
updateFontStyleDropdown();
// Show the dialog
dialog.show();
// Get the selected font family and style
var selectedFontFamily = fontFamilyDropdown.selection.text;
var selectedFontStyle = fontStyleDropdown.selection.text;
alert("You selected: " + selectedFontFamily + " - " + selectedFontStyle);
