I've enjoyed a lot of things since switching to a Mac a few years ago, for all the typical reasons: It's intuitive nature, the relative security, the fact that most of the software you will need comes with the operating system, how well it works with the other Apple devices I've started to accumulate.

On the nerdier side of things, I've really enjoyed some of the under-the-hood tools that can be used to automate jobs that you find yourself doing over and over again.  As the macosxautomation site describes these features:

Sooner or later every individual, business, or organization is challenged to perform repetitive or complex procedures on their computers. Whether the task is renaming numerous files, batch processing images, or building documents using data from multiple sources, the need for powerful automation tools is shared by all computer users. Mac OS X is designed, from the ground up, for automation and offers a variety of integrated tools and technologies to solve your automation challenges.

In short, Mac automation gives the user the ability to create little computer programs that run certain jobs for you.  You can do this with a programming language called AppleScript, which is a verbose language where you can use simple English words to tell it what you want it to do.  I actually haven't spent a lot of time using this, however, because there is an even simple way to create your programs called Automator.  With Automator you can drag and drop instructions in a certain order to achieve your desired task.  For example, you could arrange instructions in this order:

  1. start with an image
  2. resize it
  3. rename it 
  4. save it in a certain file

I won't go into more detail than that as there are a lot of great rescource out there.

 

Now on to my recent problem.

I enter a LOT of data on my Web site, EyeDock.  I have some handy web forms that allow me to do this online in a nice tidy way (as opposed to having to always deal directly with the database).    However, a new part of the site is having problems with any quotation marks (") that I enter on these web forms.  Eventually this will be something I need to fix on the back end, but for now I really want to enter this data now.  If I 'escape' the quotes everything works fine, which means I have to type a front slash before every quote (\").  I found myself doing this about 50 times and I realized I had to find a better way.

One potential solution is to copy the text, paste it into a text editor, and then do a find (") and replace (\"). However, this solution didn't sound much more appealing to me than what I was already doing.  I wanted to do a find a and replace within my browser, but unfortunately browsers do not provide this option.  This was the perfect job for Mac automation.

I decided to make a service to find quotation marks and replace them.  Macosautomation describes services thusly:

The Services menu, found in the Application menu of most applications, offers a wealth of powerful automation options. In Mac OS X, many applications and system components publish their abilities as “services,” enabling the functionality of one application to be used with the items selected in another application. For example, using a Mail service from the Services menu, text selected within a Pages document can automatically be used to create a new outgoing message in Mail. Or the text of a long article displayed in Safari can be quickly summarized in a few concise sentences.

So, I opened Automator (found in the applications directory) and chose the 'create a service' option.

I knew I wanted the service to be available when text was selected, and I knew I wanted to replace that text. I'd be doing this online so I wanted to accept text from Safari, but I figured I might as well accept text from any application in case I wanted to use the service in another program.  So, so far I had these options selected:

 

Next, I had to do the search and replace.  Unfortunately this isn't one of the many options available for text manipulation in Automator.  Automator can, however, run scripts in other programming languages, including the aforementioned AppleScript.  Another option is perl.  I've become familiar with a lot of different languages over the years, but I've actually never worked with perl.  After some quick Google searches I found some examples that gave me a sense of how string substitutions work in perl and came up with this short script:

while (<>) {

$_ = ~ s/\"/\\\"/g;

print $_;

}

Which pretty much says go through the given text, and every time you see a quote ("), change it to an escaped quote (\").  The key is in the second line which uses a function that substitutes text like this:

~s/x/y/g;

Which means when you see an "x", replace it with a "y".  So, essentially, find whatever is after the first backslash and replace it with whatever is after the second backslash and before the last one.  The letter "g" at the end means global, which indicates you want this to be done on every letter "x" in the text (not just the first one).

 

So, if you want every instance of the word "hello" to be replaced with "hi" in this sentence:

"hello, my name is Todd, and I say hello"

You'd use this statement:

~s/hello/hi/g;

And get this result:

"hi, my name is Todd, and I say hi"

 

Unfortunately things get more complicated when you want to use special symbols.  Most computer languages use punctuation marks and other characters and it can be confusing if you want to use these things - the compiler needs to know if you're using these items as part of the language or if they're being used as your text.  

For example, I wanted to search for quotation marks (") and replace them with a frontslash (\) and a quotation mark.  If I followed the patterns we were using above it would look like this:

~s/"/\"/g;

However, both quotation marks and backslashes are reserved special characters.  To use them we need to escape them (which is exactly what I'm trying to do with my original text).  We'll do this by adding a frontslash not onlyin front of each quotation mark, but also in front of the frontslash!

~s/\"/\\\"/g;

So, even though it looks like my 1 year old started slapping at my keyboard, this statement actually has meaning.  These sorts of statements can actually get significantly more complicated, but can also be incredibly powerful.  To learn more do a Google search for "regular expressions".

 

So, my completed Automator script looks like this:

 

I saved it under the name "escape quotes".

 

Now, when I'm filling out these text fields in Safari, I can select the text, right click and choose services -> escape quotes.  All my quotes are automagically escaped and life is beautiful.  (As an FYI, I usually don't use Safari, but unfortunately this is the only browser that will work with services).

So, this is pretty simple stuff for a perl (or any other language, for that matter) programmer but I hope it might be helpful for someone without programming experience who wants to do something like this.  Of course you can search and replace anything you want, not just quotation marks.  If you're really ambitious you could create an automator script to ask you what you want to search for, what you want to replace it with, and then hit "go".  It would actually be a more robust service than hard coding it as I did, although for my needs (one simple task that I wanted done quickly) this worked well.

 

 

 

Posted
AuthorTodd Zarwell
CategoriesTech