Skip to main content
Inspiring
July 11, 2023
Answered

Accessing stylistic alternates by scripting?

  • July 11, 2023
  • 2 replies
  • 692 views

is it possible at all?

 

This topic has been closed for replies.
Correct answer m1b

Hi @dolce5EC2, if you mean you want to set particular set(s) via a script, here is something I wrote to do that:

/**
 * Shows how to set the OpenType Stylistic Sets.
 * Notes: pass zero to set no stylistic sets.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/accessing-stylistic-alternates-by-scripting/m-p/13927994
 */
(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        item == undefined
        || !item.hasOwnProperty('textRange')
    ) {
        alert('Please select a text frame and try again.');
        return;
    }

    // example: activate stylistic sets 1 and 3
    item.textRange.characterAttributes.stylisticSets = getStylisticSetNumberForSets(1, 3);

})();

/**
 * Returns a number suitable for setting
 * the stylisticSets property of characterAttributes.
 * @author m1b
 * @version 2023-07-11
 * @param {Number} s1 ... sN - stylistic set numbers to activate.
 * @returns {Number}
*/
function getStylisticSetNumberForSets(/* pass set numbers as arguments */) {

    var n = 0;

    for (var i = 0; i < arguments.length; i++)
        if (arguments[i] > 0)
            n += 1 << (arguments[i] - 1);

    return n;

};

See if this works for you. It's a quick script, so might need some testing!

- Mark

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 11, 2023

Hi @dolce5EC2, if you mean you want to set particular set(s) via a script, here is something I wrote to do that:

/**
 * Shows how to set the OpenType Stylistic Sets.
 * Notes: pass zero to set no stylistic sets.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/accessing-stylistic-alternates-by-scripting/m-p/13927994
 */
(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        item == undefined
        || !item.hasOwnProperty('textRange')
    ) {
        alert('Please select a text frame and try again.');
        return;
    }

    // example: activate stylistic sets 1 and 3
    item.textRange.characterAttributes.stylisticSets = getStylisticSetNumberForSets(1, 3);

})();

/**
 * Returns a number suitable for setting
 * the stylisticSets property of characterAttributes.
 * @author m1b
 * @version 2023-07-11
 * @param {Number} s1 ... sN - stylistic set numbers to activate.
 * @returns {Number}
*/
function getStylisticSetNumberForSets(/* pass set numbers as arguments */) {

    var n = 0;

    for (var i = 0; i < arguments.length; i++)
        if (arguments[i] > 0)
            n += 1 << (arguments[i] - 1);

    return n;

};

See if this works for you. It's a quick script, so might need some testing!

- Mark

dolce5EC2Author
Inspiring
July 11, 2023

a m a z i n g  - I was about to lose hope!

thanks a bunch!
Whilst I am using just one style, getStylisticSetNumberForSets seems really cryptical, it compacts several into one by offsetting bytes by their size, right?

dolce5EC2Author
Inspiring
July 11, 2023

^several ints

Mylenium
Legend
July 11, 2023

If you write a script, you'd simply use stringFromCharcode() or another method to generate the required glyphs most of the time. Of course you'd have to know the underlying Unicode glyph IDs/ character codes.

 

Mylenium