Find text and rename document

Hi hi
I have been trying to do search in text files and use the text I find to rename the file.
I can use BBedit, open each file and search for the text with grep, copy what I find and save the file with that name. But that is not fast enough for me. When the files are over 1000 this is way to slow.
Does anyone know a better way?

Here is my problem in a nutshell:
I have lot of textfiles
I need to search each for text f.ex anytext (using (.*))
Rename the file to “anytext.txt”

It should be so simple… :slight_smile:

Best regards


assuming the pattern exists only once in the files
and the text encoding is MacRoman, put all files in a folder and try this script,
You will be prompted for the folder. The error handling is quite poor. If any error occurs, the file will be skipped

set theFolder to choose folder
tell application "Finder" to set theFiles to files of theFolder

set TID to text item delimiters
repeat with oneFile in theFiles
		set theText to read (oneFile as alias)
		set text item delimiters to "<html>"
		set theName to text item 2 of theText
		set text item delimiters to "</html>"
		set theName to text item 1 of theName & ".txt"
		tell application "Finder" to set name of contents of oneFile to theName
	end try
end repeat
set text item delimiters to TID

Hi thordis,
Hi Stefan,

Being a Python nut I could not resist to write a small script using Python’s re module. You can inspect and download the script here. Once you put it on your Desktop, you can execute the AppleScript code below. The script will ask you to choose a folder containing the text files and then process every file in this folder, searching for ((.*)) and renaming the file accordingly if possible.

Please test-drive the script on a dummy folder first!

property mytitle : "Renamy"

on run
		set folderpath to (choose folder with prompt "Please choose a folder to process" without invisibles and multiple selections allowed)
		set folderpath to quoted form of (POSIX path of folderpath)
		set pyscriptpath to quoted form of (POSIX path of (((path to desktop) as Unicode text) & ""))
		set command to "/usr/bin/python " & pyscriptpath & " " & folderpath
		do shell script command
	on error errmsg number errnum
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end run

on dsperrmsg(errmsg, errnum)
	tell me
		display dialog "Sorry, an error occured:" & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 20 with title mytitle
	end tell
end dsperrmsg

The script does not work well in case it finds special chars in the tags (e.g. German Umlauts), because you did not specify any text encoding (e.g. UTF-8, MacRoman).

I can also offer a Obj-C Foundation tool command line solution ;),
but I think in an AppleScript forum we should prefer AppleScript solutions,
especially if they are as easy to create as this one.

So no ruby appscript then? :slight_smile:

I have no idea at all about ruby :stuck_out_tongue:

Please, I beg for it :smiley:

You are right, that’s why I waited for you to post an excellent AppleScript solution first :wink:

And the Mac now features such a rich scripting environment that it’s really fun to demonstrate and learn the many ways of Mac scripting possible.

Since you asked for it. :wink:

EDIT: Made some slight modifications



So here we have Martin Michel and StefanK. One from Germany and one from Switzerland. They can communicate really well in the real world with English, but not so well in the scripting world. That just seems wrong to me! :smiley:

OK, here it is, it’s called renameFileByTag, you can download it here (source file included)

The syntax is: /path/to/renameFileByTag /path/to/sourceDirectory tag encoding
sourceDirectory must be a valid path
tag can be any tag. For example html takes the first found value between and
encoding is optional. No value is MacRoman, valid expressions are MacRoman, ISO8859-1, UTF8 and UTF16

Have fun :slight_smile:

My dear, we can even communicate perfectly in german :lol:
In the scripting world we have AppleScript as the least common denominator, this is sufficient

Very nice Stefan!


Thanks, but this should be a singular case.
I don’t want to mutate this forum to a Obj-C forum :wink:

There goes Stefan, ruining the fun :smiley: