Reply To: Script to spell out state names not working in CC

#86894
Ari Singer
Member

The issue seemed to be the Word Boundary after a period (.). So I change it to a Negative Lookahead ((?!\w)) and it worked.

I also adjusted the GREP search string so that it would be more accurate, because in GREP the period . is a wildcard that stands for every letter, and is only a period if it’s preceded by a backslash. But in the script the period was never escaped out, so it would find “Fla.” but it would also find and change “Flag”! (Not good…).

So now I added a regex at the end to fix that. (As well as fixing that Wash. entry…).

//DESCRIPTION:Expand US State Abbreviations

app.doScript(doReplace, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Expand State Abbreviations");
function doReplace ()
{
var a;
var abbrevs = [
[ "Alabama", "Ala." ],
[ "Arizona", "Ariz." ],
[ "Arkansas", "Ark." ],
[ "California", "Calif." ],
[ "Colorado", "Colo." ],
[ "Connecticut", "Conn." ],
[ "Delaware", "Del." ],
[ "District Of Columbia", "D.C." ],
[ "Florida", "Fla." ],
[ "Georgia", "Ga." ],
[ "Illinois", "Ill." ],
[ "Indiana", "Ind." ],
[ "Kansas", "Kan." ],
[ "Kentucky", "Ky." ],
[ "Louisiana", "La." ],
[ "Maryland", "Md." ],
[ "Massachusetts", "Mass." ],
[ "Michigan", "Mich." ],
[ "Minnesota", "Minn." ],
[ "Mississippi", "Miss." ],
[ "Missouri", "Mo." ],
[ "Montana", "Mont." ],
[ "Nebraska", "Neb." ],
[ "Nevada", "Nev." ],
[ "New Hampshire", "N.H." ],
[ "New Jersey", "N.J." ],
[ "New Mexico", "N.M." ],
[ "New York", "N.Y." ],
[ "North Carolina", "N.C." ],
[ "North Dakota", "N.D." ],
[ "Oklahoma", "Okla." ],
[ "Oregon", "Ore." ],
[ "Pennsylvania", "Pa." ],
[ "Rhode Island", "R.I." ],
[ "South Carolina", "S.C." ],
[ "South Dakota", "S.D." ],
[ "Tennessee", "Tenn." ],
[ "Vermont", "Vt." ],
[ "Virginia", "Va." ],
[ "Washington", "Wash." ],
[ "West Virginia", "W.Va." ],
[ "Wisconsin", "Wis." ],
[ "Wyoming", "Wyo." ] ];

app.findGrepPreferences = app.changeGrepPreferences = null;
for (a in abbrevs)
{
	var myString = abbrevs[a][1].replace(/\./g, '.');
	app.findGrepPreferences.findWhat = "b" + myString+ "(?!w)";
	app.changeGrepPreferences.changeTo = abbrevs[a][0];
	app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("scholarship winner");
	app.activeDocument.changeGrep();
	}
app.findGrepPreferences = app.changeGrepPreferences = null;
}

This article was last modified on July 25, 2016

Comments (0)

Loading comments...