Run Applescript : Find/Search & Replace

Hi guys,

I’m just starting with Automator / Applescript, and have been amazed at what Automator can accomplish!

I wanted my first project to be a simple app to convert windows file paths & open the corresponding folder on the mac. In our office, windows people constantly send file paths with L:\ at the front and ''s littered through them.

After purchasing this: http://www.automatedworkflows.com/software/automator_actions/textedit.html

I was able to accomplish the find and replace part using the “Find and Replace in Text” action.

So my workflow went:

1. Ask for text

2. Find and Replace in Text (3rd party)
Find “L:” & replace with “/Volumes/Data”

3. Find and Replace in Text (same as above)
Find "" & replace with “/”

4. Copy to clipboard

5. Open Finder Items (open with default application)

And it worked!

But the only clincher is that I was hoping to send this around the office, and make everyones lives a little easier when sharing local server links. But that 3rd party action to find and replace in the text requires every user to purchase the pack.

As it is such a tiny little task to accomplish, I was wondering if anyone has the expertise to help me write a custom automator “run applescript” i can use to replace steps 2 and 3?

I’ve found a bunch of pages with applescripts to find and replace on this site, but I can’t seem to get them to run in automator. I keep getting "expected ‘end’, but found ‘property’ " and similar errors. Would anyone be able to help me out in the context of Automator on how to accomplish this?

In the office we have the majority of people using Mac 10.5, with one or two running 10.6 and 10.4.

Many thanks for taking the time to read, and on any help you might be able to offer!

Hi Nelga!

Yes, this problem has been solved in Applescript many times. You might look in your script menu for the original Apple script “Replace Text in Item Names.” It used to be included with the OS. If you don’t have it, here’s a callable handler I wrote that uses some of Apple’s code but allows you to call it from inside another script:


to swapText(theText, swapOut, swapIn)
	(* This bit comes from Apple's own "Replace Text in Item Names" script
with some of the variables changed to make it a call-able handler *)
	if the theText contains the swapOut then
		-- replace target string using delimiters
		set AppleScript's text item delimiters to the swapOut
		set the text_item_list to every text item of theText
		set AppleScript's text item delimiters to the swapIn
		set the theText to the text_item_list as string
		set AppleScript's text item delimiters to ""
	end if
	return theText
end swapText

Wow thanks for the quick response Kevin!

If i can just ask. Can you just check if i’m doing this right in my workflow.

  1. Added “Run Applescript” to the workflow

  2. Pasted the code you suggested in between the cues as such:

on run {input, parameters}

to swapText(theText, swapOut, swapIn)
		(* This bit comes from Apple's own "Replace Text in Item Names" script with some of the variables changed to make it a call-able handler *)
	if the theText contains the swapOut then
	-- replace target string using delimiters
	set AppleScript's text item delimiters to the swapOut
	set the text_item_list to every text item of theText
	set AppleScript's text item delimiters to the swapIn
	set the theText to the text_item_list as string
	set AppleScript's text item delimiters to ""
	end if
	return theText
end swapText

return input
end run

…So sorry to be so newbie (first look at applescript) about it, but after this, I’m getting an Expected “end” but found “to”. message. Also, how would i integrate the two terms I wish to replace?

Many thanks!

Hi again!

I’d have to see the whole workflow to know how to integrate the script portion properly with the Automator actions you’re using. If you send me the workflow (my email addy is under my name out to the side of this post), I’ll see if I can make the changes then send it back to you so you can see what I did.