Script for adjusting numerical values

Community Beginner ,
Dec 10, 2020 Dec 10, 2020

Copy link to clipboard

Copied

Hi there

I am trying to make an Illustrator script that will change all numeric values in a drawing, but things are not going well.

I have some technical drawings with mesurements in mm, and I need to convert all to inches. The mesurements is just plain numbers - they are not to scale. The values are on their own layer.

I think it must be possible to make a script that takes all numeric values in a drawing, divide them by 25 (or something), and then put them back where they belong.

I hav seen a script for InDesign that does exactly that. It is called "Price adjuster" and it can adjust prices in for instance a cataloge, and convert prices from different currencys.

I have tried to make this script work in Illustrator, but unsurprisingly it did not work.

Can you suggest something?

TOPICS
Scripting

Views

168

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

Enthusiast , Dec 12, 2020 Dec 12, 2020
var frames = app.activeDocument.textFrames; for (var i = 0; i < frames.length; i++) { if (isNaN(frames[i].contents)) {continue}; frames[i].contents = Number(frames[i].contents) / 25; }

Likes

Translate

Translate
Adobe Community Professional ,
Dec 10, 2020 Dec 10, 2020

Copy link to clipboard

Copied

Please show something.

 

Are these real texts in Text frames?

Point text or area text?

There are text frames with other contents exists?

How the measurement is written/formatted?

[number][number][m][m] - 12mm

[number][number][space][m][m] - 12 mm

[number][number][dot][number][number][space][m][m] - 12.34 mm

[number][number][comma][number][number][space][m][m] - 12,34 mm

or in any other way?

 

Do you have an example document (with no confidential contents) for us for testing?

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
Enthusiast ,
Dec 10, 2020 Dec 10, 2020

Copy link to clipboard

Copied

It's doable, but I suggest you show us your document, either by sharing a screenshot of the relevant content or, preferably, an AI file (for me this has to be CS6 compatible).   

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 ,
Dec 10, 2020 Dec 10, 2020

Copy link to clipboard

Copied

for a given text frame, you can do the following:

 

var myTextFrame = app.activeDocument.textFrames[0];

myTextFrame.contents = parseInt(myTextFrame.contents) * 25;

 

the "contents" property of the text frame is what holds the actual text that you can see. it's a string, so if you want to multiply it by something, you need to convert it to a number. Assuming you're working with integers, the above code should work fine. If you have decimals (floating points), then just swap parseInt() for parseFloat().

 

Then it's just a matter of running a loop of the above logic to change all the text frames.

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 ,
Dec 12, 2020 Dec 12, 2020

Copy link to clipboard

Copied

Hi. Thank you for your answers.

 

I Tried your suggestion, DilliamWowling, and it works. But it would be nice if all the numbers could change in one go. I attach af sample file where the mesurements are in mm. These are the values that needs to be converted to inches.

Sample_mm2inch.ai 

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
Enthusiast ,
Dec 12, 2020 Dec 12, 2020

Copy link to clipboard

Copied

var frames = app.activeDocument.textFrames;
for (var i = 0; i < frames.length; i++) {
  if (isNaN(frames[i].contents)) {continue};
  frames[i].contents = Number(frames[i].contents) / 25;
}

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 ,
Dec 14, 2020 Dec 14, 2020

Copy link to clipboard

Copied

LATEST

This is awesome! Thank you very much. All I have to do now is to ensure that all numbers are on the same layer. So I remove any text from the layer and Bingo!

Thanks

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