Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Mix two weights of a font randomly

Community Beginner ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

Hey Guys, 

I would like to set all the titles, sub-titles and pull quotes for a book project from a mixture of weights. I would like to mix the Thin and Light styles of a font – but on a random basis, i.e. have the same letter sometimes in Light and sometimes in Thin. Is there already a script for this or can someone help explain how to make this work?

 

Thank you

Pia 

TOPICS
Scripting

Views

289

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Mar 14, 2021 Mar 14, 2021
I think the easiest way to script it would be to randomly apply Character Styles. Here I’ve set up 8 character styles Here’s the code, which applies the styles randomly on the selected text: var doc = app.documents.item(0); var sel = doc.selection[0].characters; var cstyles = doc.characterStyles; var cl = cstyles.length-1 var i = sel.length; while (i--) sel[i].appliedCharacterStyle = cstyles[getRandom(1,cl)] function getRandom(min,max){ return Math.floor(Math.random()*(max-min+1)+mi...

Likes

Translate

Translate
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

Please show a meaningful example. I can't imagine that looks typographically correct.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

It is not supposed to look typogrphically correct but playful. Bildschirmfoto 2021-03-14 um 13.51.54.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

I think the easiest way to script it would be to randomly apply Character Styles. Here I’ve set up 8 character styles

 

Screen Shot.png

 

Here’s the code, which applies the styles randomly on the selected text:

 

var doc = app.documents.item(0);
var sel = doc.selection[0].characters;
var cstyles = doc.characterStyles;
var cl = cstyles.length-1
var i = sel.length; while (i--) sel[i].appliedCharacterStyle = cstyles[getRandom(1,cl)]


function getRandom(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

  

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

Thank you @rob day that's very helpful. How can I adjust the code if I only want to apply two of my character styles and exclude the others? And would you apply the script in the end of the process? (sorry i don't have much experience with scripts).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

You could make a group with the styles. This version would get the two styles in a group named "RandomGroup" and apply them to the selected text (if there’s no text selection you’ll get an error) 

 

var cg = "RandomGroup"

var doc = app.documents.item(0);
var sel = doc.selection[0].characters;
var cstyles = doc.characterStyleGroups.itemByName(cg).characterStyles;
var cl = cstyles.length-1
var i = sel.length; while (i--) sel[i].appliedCharacterStyle = cstyles[getRandom(0,cl)]


function getRandom(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

 

 

Screen Shot 1.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Thank you very much @rob day Unfortunately it doesn't work yet.  How exactly did you go about it? I have loaded the script in Indesign, and when I select text and click on the script, a syntax error comes up. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Can you screen capture and post the javascript error

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

The character style group you make for the two styles has to be named RandomGroup—the name has to be exact (see the style panel capture in my post)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Bildschirmfoto 2021-03-22 um 09.55.11.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

@rob day It's in german, it says error number 8. Thanks in advance for your help. Best, Pia

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Du hast das Script falsch gespeichert. (Word? oder RFT?)

 

  • Kopiere die Codezeilen.

 

Öffne ein Programm, das *JSX Dateien speichern kann. Hast du keines, dann:

  • Öffne den Text Editor.
  • Füge den Inhalt der Zwischenablage in eine neue leere Datei ein
  • Speichere als deineDatei.TXT (Plaintext)
  • Benenne die Datei im Explorer in deineDatei.JSX um (Die Fehlermeldung, dass die Datei unbrauchbar wird, kannst du in diesem Fall ignorieren.)

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

LATEST

Danke, es funktioniert jetzt. 🙂 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines