// Illustrator Script: Create Glyph Artboards with Descriptive Names
// This script creates individual artboards for each glyph in the given character set with descriptive names

// Character set
var charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
              "abcdefghijklmnopqrstuvwxyz" +
              "1234567890" +
              ". , ; : \"„'’‚…‹›»«*[](){}/-–_<>?!$@&+^#";

function getGlyphName(glyph) {
    var glyphNames = {
        'A': 'Uppercase_A', 'B': 'Uppercase_B', 'C': 'Uppercase_C', 'D': 'Uppercase_D', 'E': 'Uppercase_E',
        'F': 'Uppercase_F', 'G': 'Uppercase_G', 'H': 'Uppercase_H', 'I': 'Uppercase_I', 'J': 'Uppercase_J',
        'K': 'Uppercase_K', 'L': 'Uppercase_L', 'M': 'Uppercase_M', 'N': 'Uppercase_N', 'O': 'Uppercase_O',
        'P': 'Uppercase_P', 'Q': 'Uppercase_Q', 'R': 'Uppercase_R', 'S': 'Uppercase_S', 'T': 'Uppercase_T',
        'U': 'Uppercase_U', 'V': 'Uppercase_V', 'W': 'Uppercase_W', 'X': 'Uppercase_X', 'Y': 'Uppercase_Y',
        'Z': 'Uppercase_Z',
        'a': 'Lowercase_a', 'b': 'Lowercase_b', 'c': 'Lowercase_c', 'd': 'Lowercase_d', 'e': 'Lowercase_e',
        'f': 'Lowercase_f', 'g': 'Lowercase_g', 'h': 'Lowercase_h', 'i': 'Lowercase_i', 'j': 'Lowercase_j',
        'k': 'Lowercase_k', 'l': 'Lowercase_l', 'm': 'Lowercase_m', 'n': 'Lowercase_n', 'o': 'Lowercase_o',
        'p': 'Lowercase_p', 'q': 'Lowercase_q', 'r': 'Lowercase_r', 's': 'Lowercase_s', 't': 'Lowercase_t',
        'u': 'Lowercase_u', 'v': 'Lowercase_v', 'w': 'Lowercase_w', 'x': 'Lowercase_x', 'y': 'Lowercase_y',
        'z': 'Lowercase_z',
        '0': 'Number_0', '1': 'Number_1', '2': 'Number_2', '3': 'Number_3', '4': 'Number_4',
        '5': 'Number_5', '6': 'Number_6', '7': 'Number_7', '8': 'Number_8', '9': 'Number_9',
        '.': 'Period', ',': 'Comma', ';': 'Semicolon', ':': 'Colon', '"': 'Double_Quote',
        '„': 'Double_Low_9_Quotation_Mark', ''': 'Left_Single_Quotation_Mark',
        ''': 'Right_Single_Quotation_Mark', '‚': 'Single_Low_9_Quotation_Mark',
        '...': 'Ellipsis', '‹': 'Single_Left_Pointing_Angle_Quotation_Mark',
        '›': 'Single_Right_Pointing_Angle_Quotation_Mark', '»': 'Right_Pointing_Double_Angle_Quotation_Mark',
        '«': 'Left_Pointing_Double_Angle_Quotation_Mark', '*': 'Asterisk', '[': 'Left_Square_Bracket',
        ']': 'Right_Square_Bracket', '(': 'Left_Parenthesis', ')': 'Right_Parenthesis',
        '{': 'Left_Curly_Bracket', '}': 'Right_Curly_Bracket', '/': 'Forward_Slash',
        '–': 'En_Dash', '-': 'Hyphen', '_': 'Underscore', '<': 'Less_Than', '>': 'Greater_Than',
        '?': 'Question_Mark', '!': 'Exclamation_Mark', '$': 'Dollar_Sign', '@': 'At_Sign',
        '&': 'Ampersand', '+': 'Plus_Sign', '^': 'Caret', '#': 'Hash'
    };
    return glyphNames[glyph] || 'Glyph_' + glyph.charCodeAt(0);
}

// Document setup
var doc = app.documents.add();
var artboardSize = 1024;

// Font settings
var fontSize = 800; // Starting font size, will be adjusted to fit

function findAvailableFont(preferredFonts) {
    for (var i = 0; i < preferredFonts.length; i++) {
        if (app.textFontInfo.get(preferredFonts[i]) != null) {
            return app.textFontInfo.get(preferredFonts[i]);
        }
    }
    // If no preferred fonts are found, return the first available font
    return app.textFontInfo.get(app.fonts[0].name);
}

// List of preferred fonts (in order of preference)
var preferredFonts = ["Arial", "Helvetica", "Myriad Pro", "Tahoma", "Verdana"];
var selectedFont = findAvailableFont(preferredFonts);

// Create artboards and text for each character
for (var i = 0; i < charSet.length; i++) {
    var glyph = charSet[i];
    var glyphName = getGlyphName(glyph);
    
    // Create new artboard
    var artboardRect = [0, 0, artboardSize, artboardSize];
    app.activeDocument.artboards.add();
    var currentArtboard = app.activeDocument.artboards[app.activeDocument.artboards.length - 1];
    currentArtboard.name = glyphName;
    currentArtboard.artboardRect = artboardRect;
    
    // Create text frame
    var textFrame = app.activeDocument.textFrames.add();
    textFrame.contents = glyph;
    textFrame.textRange.characterAttributes.textFont = selectedFont;
    textFrame.textRange.characterAttributes.size = fontSize;
    
    // Center text
    textFrame.left = (artboardSize - textFrame.width) / 2;
    textFrame.top = (artboardSize - textFrame.height) / 2;
    
    // Adjust font size to fit artboard if necessary
    while (textFrame.width > artboardSize * 0.9 || textFrame.height > artboardSize * 0.9) {
        fontSize -= 10;
        textFrame.textRange.characterAttributes.size = fontSize;
        textFrame.left = (artboardSize - textFrame.width) / 2;
        textFrame.top = (artboardSize - textFrame.height) / 2;
    }
}

// Rearrange artboards in a grid
var columns = Math.ceil(Math.sqrt(charSet.length));
var rows = Math.ceil(charSet.length / columns);

for (var i = 0; i < charSet.length; i++) {
    var row = Math.floor(i / columns);
    var col = i % columns;
    var left = col * artboardSize;
    var top = -row * artboardSize;

    // Get the current artboard and its size
    var currentArtboard = app.activeDocument.artboards[i];
    var currentRect = currentArtboard.artboardRect;
    var width = currentRect[2] - currentRect[0];
    var height = currentRect[1] - currentRect[3];

    // Move the current artboard to the desired position
    currentArtboard.artboardRect = [left, top, left + width, top - height];
}

alert("Artboards rearranged successfully!");