Highlighted

Randomly assign font to characters

New Here ,
Aug 21, 2015

Copy link to clipboard

Copied

Hi,

I'm trying to figure out a way to randomly assign one of a set of fonts to each character in the document.
I've searched the internet and found the below script that is supposed to work for Microsoft Word (I'm still a novice when it comes to scripting, and the script is sort of old, so I haven't succeeded in making it work).

I wanted to see if anyone knew of a way to use this concept and adapt a script for InDesign that would randomly assign a font to each character in the document from a list of fonts.

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Set objRandom = CreateObject("System.Random")

intLow = 1

intHigh = 5

Set objRange = objDoc.Range()

Set colCharacters = objRange.Characters

For Each strCharacter in colCharacters

    intRandom = objRandom.Next_2(intLow,intHigh)

   

    Select Case intRandom

        Case 1 strCharacter.Font.Name = "Arial"

        Case 2 strCharacter.Font.Name = "Times New Roman"

        Case 3 strCharacter.Font.Name = "Courier New"

        Case 4 strCharacter.Font.Name = "Forte"

    End Select

Next

Any help is greatly appreciated!!

TOPICS
Scripting

Views

862

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

Randomly assign font to characters

New Here ,
Aug 21, 2015

Copy link to clipboard

Copied

Hi,

I'm trying to figure out a way to randomly assign one of a set of fonts to each character in the document.
I've searched the internet and found the below script that is supposed to work for Microsoft Word (I'm still a novice when it comes to scripting, and the script is sort of old, so I haven't succeeded in making it work).

I wanted to see if anyone knew of a way to use this concept and adapt a script for InDesign that would randomly assign a font to each character in the document from a list of fonts.

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Set objRandom = CreateObject("System.Random")

intLow = 1

intHigh = 5

Set objRange = objDoc.Range()

Set colCharacters = objRange.Characters

For Each strCharacter in colCharacters

    intRandom = objRandom.Next_2(intLow,intHigh)

   

    Select Case intRandom

        Case 1 strCharacter.Font.Name = "Arial"

        Case 2 strCharacter.Font.Name = "Times New Roman"

        Case 3 strCharacter.Font.Name = "Courier New"

        Case 4 strCharacter.Font.Name = "Forte"

    End Select

Next

Any help is greatly appreciated!!

TOPICS
Scripting

Views

863

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
Aug 21, 2015 0
Adobe Community Professional ,
Aug 22, 2015

Copy link to clipboard

Copied

You should refine your googling : "indesign apply random font" gives you numerous links, the more interisting of which is Random font script?

But that doesn't apply random fonts to characters. This does (select a text frame or some text):

fontNames = ['Arial', 'Times New Roman', 'Courier New', 'Forte'];

ch = app.selection[0].parentStory.characters.everyItem().getElements();

for (i = ch.length-1; i >= 0; i--) {

    ch.appliedFont = fontNames[Math.floor(Math.random()*4)];

}

Expect very, very long running times on large documents.

Peter

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
Reply
Loading...
Aug 22, 2015 2
New Here ,
Nov 23, 2017

Copy link to clipboard

Copied

been using this for a while, very good sir

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
Reply
Loading...
Nov 23, 2017 0
New Here ,
Sep 30, 2020

Copy link to clipboard

Copied

Hi, I am not sure if I am doing this wrong.

I have a 10 page story and am trying to run this script on it. Nothing seems to be changing though.

Is there more to this code that I should be adding?

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
Reply
Loading...
Sep 30, 2020 0
Adobe Community Professional ,
Sep 30, 2020

Copy link to clipboard

Copied

Hi Anna,

one explanation why this script is not working for you:

You have no fonts installed that match the names in the array with the variable fontNames.

 

Regards,
Uwe Laubender

( ACP )

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
Reply
Loading...
Sep 30, 2020 0
New Here ,
Sep 30, 2020

Copy link to clipboard

Copied

I do have all those fonts 😞

I'm saving this as a .jsx file in my Scripts Panel, this is correct right?

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
Reply
Loading...
Sep 30, 2020 0
Adobe Community Professional ,
Sep 30, 2020

Copy link to clipboard

Copied

Are you selected somewhere into a text frame with your text tool? 

Do you get any warning or error messages? 

 

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
Reply
Loading...
Sep 30, 2020 0
New Here ,
Oct 01, 2020

Copy link to clipboard

Copied

Yes, I have tried putting the cursor in the text frame, selecting text, and just selecting the text frame.

Unfortunately I do not get any error messages. I will attach a screen recording of what I am doing... maybe there is something I am doing very wrong.

 

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
Reply
Loading...
Oct 01, 2020 0