Copy link to clipboard
Copied
I'm trying to create a script in Illustrator that will find any numbers and add a percent symbol to the end. I tried deconstructing a grep chain script I wrote for InDesign and use it for Illustrator but its not working.
I'm basically trying add percents to my chart labels, If I ungroup my charts and use a script to add a percent symbol it will be all I need to finalize my charst fast!
Try the following
var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
var text = frames[i].contents;
if (!isNaN(text) && frames[i].paragraphs[0].paragraphStyles[0].name == "data_bars_right"){
frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!")
-Manan
Copy link to clipboard
Copied
Please show what you have. And a dummy ai file and/or a screenshot.
Copy link to clipboard
Copied
I created the chart with column designs. I was able to run a script to add ".0" to the whole numbers by ungrouping the charts first. I need to also callout a specific paragraph style "data_bars_right" so that the category labels don't change if they are numbers, like below. the javascript below is what i've been playing with.
main();
function main() {
if (app.layoutWindows.length == 0) return;
var changeObject = app.documents[0];
if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
var doc = app.documents[0];
var style;
var scriptVersion = app.scriptPreferences.version;
app.scriptPreferences.version = 17.0;
var options = app.findChangeGrepOptions.properties;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// Query [[Add Percent to Numbers]] -- If you delete this comment you break the update function
try {
app.findChangeGrepOptions.properties = ({includeFootnotes:true, widthSensitive:true});
app.findGrepPreferences.properties = ({findWhat:"\\(?([\\d.,]+)\\)?"});
app.changeGrepPreferences.properties = ({changeTo:"$1%"});
changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findChangeGrepOptions.properties = options;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.scriptPreferences.version = scriptVersion;
};
function getStyleByString(docOrGroup, string, property) {
if (string == 'data_bars_right') return docOrGroup[property][0];
if (string == 'data_bars_cntrd') return docOrGroup[property][1];
stringResult = string.match (/^(.*?[^\\]):(.*)$/);
var styleName = (stringResult) ? stringResult[1] : string;
styleName = styleName.replace (/\\:/g, ':');
remainingString = (stringResult) ? stringResult[2] : '';
var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
return styleOrGroup;
};
Copy link to clipboard
Copied
Wish I could edit this post... I figured out some script and it works to add the % labels, but its ignoring that I only want it done to numbers with the data_bars paragraph style. Any help would be appreciated!
var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
var myStyle = myDoc.paragraphStyles.getByName("data_bars_right");
//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
var text = frames[i].contents;
if (!isNaN(text) && myStyle){
frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!");
Copy link to clipboard
Copied
Try the following
var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
var text = frames[i].contents;
if (!isNaN(text) && frames[i].paragraphs[0].paragraphStyles[0].name == "data_bars_right"){
frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!")
-Manan
Copy link to clipboard
Copied
works perfectly! thank you!
Copy link to clipboard
Copied
how would I adjust this so it would ignore a text frame that doesnt have text in it? is that possible?
Copy link to clipboard
Copied
Try this
var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
for (var i = 0; i < frames.length; i++) {
var text = frames[i].contents;
if (!isNaN(text) && text != "" &&
frames[i].paragraphs[0].paragraphStyles[0].name == "data_bars_right") {
frames[i].contents += "%";
}
}