Digital Media, Web Design, and Web Development by Ethan Kent | ethanmultimedia.com

ethan -at- ethanmultimedia -dot- com
six one nine - two four six - four one two zero

AppleScript String Replace

I’ve just recently taken some interest in the AppleScript language. Even though the language itself is somewhat anemic, it gives access to scripting some really good software. Safari, iTunes, Photoshop, and Mail to name a couple.

As a web developer, I found myself testing out the shell scripting interface right away. For quite a while, I’ve been in love with OSX unix utilities. The idea of souping up AppleScripts with commandline tools seemed like a necessity. I’m also pretty intrigued by the idea of accessing AppleScript through shell scripting. I may look into creating some web interfaces for some of my mac apps.

Here’s an example of an AppleScript that uses a commandline utility.

--
-- Similar to php str_replace or ruby gsub
--
on eak_gsub(str_search, str_replace, str_subject)
	set awk_script to "BEGIN {
	str = " & quote & str_subject & quote & "
        gsub(" & quote & str_search & quote & ", " & quote & str_replace & quote & ", " & "str)
        print str}"
	do shell script "awk  '" & awk_script & "'"
end eak_gsub
--
-- example that calls the function 
-- should display "foo foo baz"
--
display dialog eak_gsub("bar", "foo", "foo bar baz")

Filed Under: , , , , , , , , , , , , ,


Comment

Textile Help

Previous Article

Next Article

About This Site

The contents, design, and development of this site are all done by me, Ethan Kent.

The contents of my site, ethanmultimedia.com, are for general information only and provided strictly as is. I do my best to be truthful and accurate, but I must expressly disclaim any implied warranties of non-infringement, merchantability and fitness for a particular purpose. Browse this site at your own risk. If you’re looking for safety and assurance you should probably seek the advice of an appropriate professional.

Contact Digital Media and Web Designer Ethan Kent - based in San Diego, CA.