Doing Find & Replace with AppleScript

As a newbie to AppleScript I sometimes wonder if I'm wasting a lot of time trying to do something that AppleScript can't do. In this case I'm trying to find & replace a string in a bunch of text documents and I want to automate the process. It's the same string for all documents. I tried to get AppleScript to access TextEdit's Find & Replace function and either I don't know how to do it or it can't be done.

I don't really need someone to provide me with a script that will do that but it would be nice to know if it can be done. And is it possible to get AppleScript to go through the documents in a folder and do them all? Do I need to do it with a shell script? Any suggestion would be appreciated.

Ken

Posted on Nov 8, 2005 2:10 PM

Reply
10 replies

Nov 8, 2005 3:18 PM in response to Ken Spiker

Hi Ken

Although TextEdit is scriptable, its Find and Replace functionality is not. It's a sad example of Apple's inconsistent attitude to developing AppleScript in their own applications.

But that doesn't mean it can't be done. If the documents you're searching are simply text documents, then there a couple of ways to approach things:

Use a better (or more scriptable) text editor. Tex-Edit Plus from http://www.tex-edit.com/ is my personal favourite. It has enormous scripting capability, its developer is committed to AppleScript, and it's very simple to use. Other options are TextWrangler or BBEdit.

It would also be possible (though unnecessarily complicated IMO) to script a word processor such as Word or AppleWorks to do the job.

The quickest and slickest way though would be to use AppleScript's file read-write commands from the Standard Additions scripting addition to modify the files. It's perfectly possible - and very quick - to work through hundreds of files in this way without even opening them on screen.

None of the above would need a shell script.

Post back if you want to pursue it.

H

Nov 8, 2005 9:45 PM in response to HD

Dear H,

Thanx for the help. I downloaded Tex-Edit Plus (even paid for it) and yes, it's very scriptable. It even has a script to do exactly what I want, find and replace a string for every text file in a folder. However I can't seem to make it work. I set all the documents to open with Tex-Edit Pro. There are several hundred of these text documents in my folder and one time it did convert about 14 of them properly and left the others alone. All other times it just doesn't replace anything in any files. The string I'm replacing is actually an integer; do you think that could have something to do with it? But I tried replacing a word and that didn't work either.

Because I'm a newby at AppleScript I can't see what's wrong on casual inspection. That's a sort of complicated script with globals and strange usages. I'll try to email Tex-Edit and ask them if they know. I will continue to experiment and maybe I can write my own script to do that.

Thank you for pointing me to this resource. The more I play with it the more I learn about AppleScript.

KEN

Nov 9, 2005 1:03 AM in response to Ken Spiker

I posted a recent message inquiring into why the "replace everything in folder" script didn't work.

Well, I got it to work.

It seems that the script would not recognize the form of text encoding the files were saved in. However Tex-Edit Pro also had a script that would convert my files to SimpleText format and that did the trick. Now the "replace everything" works.

I was able to do a search and replace on 661 text documents in half a minute!

Thank you, HD

Ken

Nov 15, 2005 1:15 AM in response to HD

allow me to prolong this discussion. I also would like to script a find and replace. I have succeeded in creating a macro in Word and a Text Factory in BBEdit that both do exactly what I want them to do. However I can't quite get Applescript to implement them in a script.

In this script BBEdit open the document but does not apply the text factory even though this definition exists in the dictionary

tell application "BBEdit"
activate
open "/Users/geoffreydyson/Desktop/Formulaire provenant du web.txt"
apply text factory "provenant du web factory"
end tell

And in this one, Word doesn't even open the document let alone implement the macro.

tell application "Microsoft Word"
activate
open "/Users/geoffreydyson/Desktop/Formulaire provenant du web.txt"
run VB macro "open formulaire"
end tell

Any ideas on getting either one of these to work

Nov 15, 2005 2:06 AM in response to Geoffrey Dyson

This really is a separate discussion since it doesn't relate to the original poster's question in any way, but...

The problem with your script is twofold. First, in your open statement you're trying:

open "/Users/geoffreydyson/Desktop/Formulaire provenant du web.txt"


This won't work. You're trying to open a string which won't work. You need to coerce this to a file reference first. Additionally, you'll need to use Mac-style paths not unix paths, try:

open file "HD:Users:geoffreydyson:Desktop:Formulaire provenant du web.txt"


Using Mac-style paths along with the 'file' object should enable the file to open.

As to why BBEdit doesn't apply the text factory, if you look at the dictionary you'll see that 'apply text factory' requires a 'to' parameter indicating where the text factory should be applied. Therefore:

apply text factory "provenant du web factory" to document 1

Nov 15, 2005 1:07 PM in response to Camelot

Thanks for your reply. Unfortuantely it hasn't reseoved my problem. The script

tell application "BBEdit"
activate
open "/Users/geoffreydyson/Desktop/Formulaire provenant du web.txt"
apply text factory "provenant du web factory" to "Formulaire provenant du web.txt"
end tell

still opens the document but does not execute the text factory and the script

tell application "Microsoft Word"
activate
open "Bruce:Users:geoffreydyson:Desktop:Formulaire provenant du web.txt"
run VB macro "open formulaire"
end tell

now succeeds in opening hte document but still fails to execute the macro. Any help would be appreciated

Nov 15, 2005 1:13 PM in response to Geoffrey Dyson

Shame on me. Sorry about that let me just correct the spelling.

Thanks for your reply. Unfortunately it hasn't resolved my problem. The script:

tell application "BBEdit"
activate
open "/Users/geoffreydyson/Desktop/Formulaire provenant du web.txt"
apply text factory "provenant du web factory" to Document 1"
end tell

still opens the document but does not execute the text factory and the script:

tell application "Microsoft Word"
activate
open "Bruce:Users:geoffreydyson:Desktop:Formulaire provenant du web.txt"
run VB macro "open formulaire"
end tell

now succeeds in opening the document but still fails to execute the macro. Any help would be appreciated

Nov 17, 2005 10:45 AM in response to Ken Spiker

Ken,
Do I need to do it with a shell script?


If your files are all text files then a shell script would be the fastest. For ease of use you could wrap in in AS.

This hint gives a nice example of using perl to change line unix/mac line endings. You can modify it to suit your needs.
It's nice that it changes the file. It's dangerous because it changes the file.

http://www.macosxhints.com/article.php?story=20001206164827794


Reese

Nov 17, 2005 12:15 PM in response to Ken Spiker

If the files are just plain text you don't need any application, you can just use AppleScript. You'd have to use the read file command to load the file text into a variable then send it to the handler below with the text to search for (searchText) and the text to replace it with (replaceText). The handler will return the new text with every instance of the search text replaced with the replacement text.

<pre>on searchAndReplace(targetText, searchText, replaceText)
set newText to ""
set {tid, my text item delimiters} to {my text item delimiters, searchText}
try
set textList to every text item of targetText
set my text item delimiters to replaceText
set newText to textList as text
set my text item delimiters to tid
on error
set my text item delimiters to tid
set newText to targetText
end try
return newText
end searchAndReplace</pre>

Is this what you were looking for?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Doing Find & Replace with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.