Changing state names to postal abbreviations in bulk

The macro at Abbreviations: States, Months Macro requires you to select the state names, one-at-a-time, and run the macro. Is there a way to have a script change all state names in one series of find/replace steps, if you have a long list of addresses?

I guess I could build a macro with 50 find/replace actions, one for each state, but I'm hoping there's an easier way.


I'm the culprit behind that macro. Yes, it was designed to convert a single selection. I'm not adverse to providing a batched version but first I'd like a peek at the data you would like to feed it. It doesn't have to be the actual data, just resemble the actual data.

My first thought is the new macro would prompt you for a file to convert state names (as well as the format to convert to). And after conversion write a new file with the edited names.


The macro you made is great for small instances, but I had nearly 300 records.

Data sample:
Linda,Alterwitz,Las Vegas,Nevada
Carol,Andrews,Missouri City,Texas

I asked my son about this this afternoon, and he sent me a shell script that starts like this, and repeats to Wyoming:


cat $1 | sed
-e s'/Alabama/AL/'
-e s'/Alaska/AK/'
-e s'/American Samoa/AS/'
-e s'/Arizona/AZ/' \

...with instructions on running it from the Terminal. It did great, so I have something that works for future needs. But it would be so much nicer to just do it from the Finder with Keyboard Maestro.

He's not very familiar with Keyboard Maestro, but thought I might have AppleScript call the script. However, looking at the the "Execute a Shell Script" action, perhaps KM can run it directly. As you said, it would probably have to work on a closed file (selected file in the Finder).

Yes, Keyboard Maestro can run that directly with the Execute a Shell Script action but I believe you'll have to use a variable name for the file where $1 appears after picking a file.

The only caveat with the script is you might want to anchor the search to the end of the line to only affect that occurence. Arizona$ for example.

Convert State Names to Postal Abbrevs Macro (v9.0.6)

This macro returns:

Linda,Alterwitz,Las Vegas,NV
Carol,Andrews,Missouri City,TX

on your original sample data in an open file. No need to select a file. The macro selects all the text, copies it to the Clipboard, passes that as a variable to the Perl code, which steps through each line making the substitution.

It could be optimized but it seemed pretty quick banging out one line at a time.

Convert State Names to Postal Abbrevs.kmmacros (4.9 KB)

[Revised with ComplexPoint's corrections]


FWIW a couple of small things in the data there, I think.

  • D.C. needs a third field (DC)
  • The delimiter between Aug and 08 is not a tab.
1 Like

Thanks very much for finding those! Corrected here and I'll hunt down the original macro to fix that next.

1 Like

Thanks, but when I run this on a BBEdit file, the state names don't change, but the number "12" is appended to each line. What might be the cause of that?

I see a set of date changes at the end of the states, and the last one ends with "12." Could that be involved?

What's the data look like?

This macro looks at the last field of each line where it expects to see a state name. It's not a general purpose macro like the original.

When I run it on your sample data, this is what I get:

Linda,Alterwitz,Las Vegas,NV
Carol,Andrews,Missouri City,TX

So I suspect you have differently formatted data.

Yes, I didn't realize the state name had to be at the end of each line. I thought it might find them anywhere. Now that I know, I can make good use if it. Thanks!

Or you can strip the $ in the Perl code that indicates end-of-line in the substitution operator:

	$contact =~ s/$state_name/$replace/;

That will find it anywhere in the line. But only once. If you want to find it more than once:

	$contact =~ s/$state_name/$replace/g;

Excellent! Thank you so much.