• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to Change font size and baseline randomly

Community Beginner ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

So, I want to change the font size and baseline of each character in the selection randomly.

 

The baseline part works the way I want it to but for the line:
textRef.textRange.characters[i].characterAttributes.size=newSize;

I keep getting an "Invalid Text Range" error.  Here's the script:

 

//--------------------------
//Reference the document.
var docRef = activeDocument;

//Reference the first selection in the document.
var textRef = docRef.selection[0];

//Get the number of characters in the selected object.
var charCount=textRef.textRange.characters.length;

//Set a variable to serve as alternate positive and negative value.
var shiftSign=1;

//Let the user determine the baseline shift range and varying point size.
var shiftRange = prompt("Enter the distance, in points, by which you want the Baseline Shift to vary.", 1);
var minSize = prompt("Enter the minimum point size:", "12");
var maxSize = prompt("Enter the maximum point size:", "24");

//For each character...
for(i=0; i<charCount; i++){

//...flop the positive/negative...
shiftSign*=-1;

//...reference the range multiplied by a random number between 0 and 1...
var curShift=Math.abs(Math.random()*shiftRange);
var newSize=Math.random()*(maxSize - minSize)+minSize;

//...shift the current character by that amount.
textRef.textRange.characters[i].characterAttributes.baselineShift=curShift*shiftSign;

//...shift the current character size by that amount
textRef.textRange.characters[i].characterAttributes.size=newSize;


}

//-------------------------

 

Thanks for any insight!!

TOPICS
Scripting , Type

Views

367

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 2 Correct answers

Guide , Jan 27, 2023 Jan 27, 2023

Try changing

var minSize = prompt("Enter the minimum point size:", "12");
var maxSize = prompt("Enter the maximum point size:", "24");

to

var minSize = Number(prompt("Enter the minimum point size:", "12"));
var maxSize = Number(prompt("Enter the maximum point size:", "24"));

 

Votes

Translate

Translate
Community Expert , Jan 28, 2023 Jan 28, 2023

Votes

Translate

Translate
Adobe
Guide ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Try changing

var minSize = prompt("Enter the minimum point size:", "12");
var maxSize = prompt("Enter the maximum point size:", "24");

to

var minSize = Number(prompt("Enter the minimum point size:", "12"));
var maxSize = Number(prompt("Enter the maximum point size:", "24"));

 

Votes

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
Guide ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

To expound on the above: prompt() returns a string. The plus in the Math.random expression will concatenate rather than add, practically not changing the random number. If the difference between min and max is small, there is a chance the random number will be <0.1, which is an invalid font size.

Votes

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 ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

LATEST

I guessed it had to be something like that but I couldn't figure it out. Thank you SO MUCH!

Votes

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 Expert ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Votes

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 ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

Thanks!  I'll check it out.  🙂

 

Votes

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