Script to spell out state names not working in CC
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to spell out state names not working in CC
- This topic has 6 replies, 3 voices, and was last updated 7 years, 9 months ago by Cindy Kelley.
-
AuthorPosts
-
-
July 24, 2016 at 10:59 am #86879Cindy KelleyParticipant
Hi there,
This script worked fine for CS6 but I can’t get it to work for CC. Any ideas why?
Thanks,
Cindy//DESCRIPTION:Expand US State Abbreviations
if (app.version < ‘6’)
doReplace();
else
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.” ],
[ “Wash.”, “Washington” ],
[ “West Virginia”, “W.Va.” ],
[ “Wisconsin”, “Wis.” ],
[ “Wyoming”, “Wyo.” ] ];app.findGrepPreferences = app.changeGrepPreferences = null;
for (a in abbrevs)
{
app.findGrepPreferences.findWhat = “\\b”+abbrevs[a][1]+”\\b”;
app.changeGrepPreferences.changeTo = abbrevs[a][0];
app.findGrepPreferences.appliedParagraphStyle = “scholarship winner”;
app.activeDocument.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
} -
July 24, 2016 at 11:14 am #86880Ari SingerMember
Does your document have the “scholarship winner” paragraph style applied?
Try this:
//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." ], [ "Wash.", "Washington" ], [ "West Virginia", "W.Va." ], [ "Wisconsin", "Wis." ], [ "Wyoming", "Wyo." ] ]; app.findGrepPreferences = app.changeGrepPreferences = null; for (a in abbrevs) { app.findGrepPreferences.findWhat = "\\b" + abbrevs[a][1] + "\\b"; app.changeGrepPreferences.changeTo = abbrevs[a][0]; app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("scholarship winner"); app.activeDocument.changeGrep(); } app.findGrepPreferences = app.changeGrepPreferences = null; }
If it does still not work, please spell me out the error that pops up.
-Ari
-
July 25, 2016 at 3:38 am #86887Cindy KelleyParticipant
Hi there,
Yes, I do have a paragraph style of “scholarship winner”.
This script still doesn’t work. It appears to do nothing so I don’t get an error code of any sort.
Thank you for taking your time to look at this Ari. :)
Cindy
-
July 25, 2016 at 7:48 am #86889Matt IsaacParticipant
The problem was in the findGrepPreferences with the word boundary. Removing the boundaries worked for me. Also the single quotes around the 6 were removed (with the quotes it would not use the undo feature.)
This code should work//DESCRIPTION:Expand US State Abbreviations if (app.version < 6) doReplace(); else 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) { app.findGrepPreferences.findWhat = abbrevs[a][1]; app.changeGrepPreferences.changeTo = abbrevs[a][0]; app.findGrepPreferences.appliedParagraphStyle = "scholarship winner"; app.activeDocument.changeGrep(); } app.findGrepPreferences = app.changeGrepPreferences = null; }
-
July 25, 2016 at 8:04 am #86892Matt IsaacParticipant
After posting i realized that all of the periods need to be escaped in the abbreviations..
//DESCRIPTION:Expand US State Abbreviations if (app.version < 6) doReplace(); else 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) { app.findGrepPreferences.findWhat = abbrevs[a][1]; app.changeGrepPreferences.changeTo = abbrevs[a][0]; app.findGrepPreferences.appliedParagraphStyle = "scholarship winner"; app.activeDocument.changeGrep(); } app.findGrepPreferences = app.changeGrepPreferences = null; }
This code will be the one you should use unless Ari comes up with something better. =P
-
-
July 25, 2016 at 8:04 am #86894Ari SingerMember
The issue seemed to be the Word Boundary after a period (
.\b
). 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; }
-
July 28, 2016 at 7:16 am #86964Cindy KelleyParticipant
HI gang,
This works GREAT! It is going to save me a lot of time.
Thanks!
…and this another reason I love indesignsecrets.com. :)
Cindy
-
-
AuthorPosts
- You must be logged in to reply to this topic.