• 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 add a percent symbol to all numbers

Contributor ,
Nov 29, 2021 Nov 29, 2021

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!

TOPICS
Scripting , Type

Views

725

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

Community Expert , Nov 30, 2021 Nov 30, 2021

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

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

Please show what you have. And a dummy ai file and/or a screenshot.

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
Contributor ,
Nov 29, 2021 Nov 29, 2021

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.

 

Screen Shot 2021-11-29 at 8.06.33 PM.png

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;
};

 

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
Contributor ,
Nov 29, 2021 Nov 29, 2021

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!");

 

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 ,
Nov 30, 2021 Nov 30, 2021

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

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
Contributor ,
Nov 30, 2021 Nov 30, 2021

Copy link to clipboard

Copied

works perfectly! thank you!

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
Contributor ,
Dec 01, 2021 Dec 01, 2021

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?

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 ,
Dec 01, 2021 Dec 01, 2021

Copy link to clipboard

Copied

LATEST

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 += "%";
    }
}

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