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
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
...
Copy link to clipboard
Copied
Please show a meaningful example. I can't imagine that looks typographically correct.
Copy link to clipboard
Copied
It is not supposed to look typogrphically correct but playful.
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
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);
}
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).
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);
}
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.
Copy link to clipboard
Copied
Can you screen capture and post the javascript error
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)
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@rob day It's in german, it says error number 8. Thanks in advance for your help. Best, Pia
Copy link to clipboard
Copied
Du hast das Script falsch gespeichert. (Word? oder RFT?)
Öffne ein Programm, das *JSX Dateien speichern kann. Hast du keines, dann:
Copy link to clipboard
Copied
Danke, es funktioniert jetzt. 🙂