Copy link to clipboard
Copied
Hi everyone,
I need to add rounding to this script. I need two versions, one for 1 decimal place and one for 0 decimal places. Can anyone help out?
#target illustrator
addMeasurements();
function addMeasurements() {
var i, doc, imp, met, txtItems, txt, nub;
doc = app.activeDocument;
imp = doc.layers.getByName( 'Text' );
met = doc.layers.add(); met.name = 'New';
txtItems = imp.textFrames;
for ( i = 0; i < txtItems.length; i++ ) {
txtItems.duplicate( met, ElementPlacement.PLACEATEND )
};
txtItems = met.textFrames;
for ( i = 0; i < txtItems.length; i++ ) {
var nub = parseInt( txt = txtItems.contents ); // For Numbers only!!!!
txtItems.contents = String( txt * 1.1 )
};
};
Thanks in advance.
Copy link to clipboard
Copied
What's the content of your textFrames?
Rounding: up or down or mathematical or other?
e.g. like this:
var x = 123.4567;
alert (Math.round (x*10)/10);
alert (Math.round (x));
Try it.
Copy link to clipboard
Copied
Hi. Thanks for the help. The text frames are numbers only, or numbers and 1 letter eg 10R Id like to have it round down for up to .49 and up from .5.
I'll gave it a go but couldnt get it to work.
Copy link to clipboard
Copied
What I use…
$.writeln( roundToDP( 1234.56789, 2 ) );
function roundToDP( n, dP ) {
return Math.round( n * Math.pow( 10, dP ) ) / Math.pow( 10, dP );
};
Copy link to clipboard
Copied
Please show some examples of your numbers and a screenshot with visible layers palette..
Copy link to clipboard
Copied
Thanks for the replies. Excuse my lack of scripting knowledge. Mark thanks for the example, where do I insert that to the code you already gave me?
Heres a test file of what im using it on
https://www.dropbox.com/s/ko2v9dx89uh3wjk/script.ai
Thanks
Copy link to clipboard
Copied
Thanx for your example-file.
But what I really need is a file with numbers before and after using script. (Please CS3-conform)
Copy link to clipboard
Copied
The example is just a basic function… Just place it after the main function body in your script… Call it where you need it… here is an example… I changed your parseInt() to parseFloat() to deal with your decmial values… While this should wolk it still may NOT be what you want… i.e. it does nothing about keeping the alpha characters in your text frames the new numbers replace all content…
#target illustrator
addMeasurements();
function addMeasurements() {
var i, doc, imp, met, txtItems, txt, nub;
doc = app.activeDocument;
imp = doc.layers.getByName( 'Numbers' );
met = doc.layers.add(); met.name = 'New';
txtItems = imp.textFrames;
for ( i = 0; i < txtItems.length; i++ ) {
txtItems.duplicate( met, ElementPlacement.PLACEATEND )
};
txtItems = met.textFrames;
for ( i = 0; i < txtItems.length; i++ ) {
var nub = parseFloat( txtItems.contents ); // now parseFloat()
nub = nub * 1.123456; // just to check the function rounds…
var rnub = roundToDP( nub, 2 );
txtItems.contents = String( rnub ); // replaces all content…?
};
};
//
function roundToDP( n, dP ) {
return Math.round( n * Math.pow( 10, dP ) ) / Math.pow( 10, dP );
};
Copy link to clipboard
Copied
Try this on a single textFrame with numbers and following Letter:
#target Illustrator
// RoundNumBeforeLetter.jsx
// http://forums.adobe.com/thread/1095744?tstart=0
// rundet Zahlen vor einem Buchstaben
var myDoc = app.activeDocument;
var myTxt = myDoc.textFrames[0]; //the first TextFrame in document
var myTxtCharLen = myTxt.characters.length;
var myTxtNum, j, LastChar = null;
for (i=0; i <=myTxtCharLen-1;i++) {
myTxtChar = myTxt.characters.contents;
if (SearchText(myTxtChar) == true) {
j = i;
myTxtNum = myTxt.contents.slice (0,j);
myTxtNum = Math.round (myTxtNum);
LastChar = myTxt.contents.slice (j);
myTxt.contents = myTxtNum+LastChar;
}
}
function SearchText(x)
{
var myRegExp = /(^-?[A-Z]$)/;
return myRegExp.test(x);
}
Maybe it's not as elegant. On the contrary, it probably is "complicated and a long way around," but should work.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now