for a case sensitive search / regular expression support

Hello everyone,

I’m working on this find/replace app and I was wondering how I could make a search case-sensitive (optionally). I based some of the actions on the finder scripts that can be found in -Library/Scripts/Finder Scripts and they’re the ones I’m trying to add that option to.

Also, I’d like to add a regular expression support option to my app. I’ve read some stuff about it online, and here I found a reference to Satimage, which I have no clue how to use.

Could anyone help with these 2 things?

Thanks :slight_smile:

Hi,

#1
From AppleScript Release Notes for Mac OS X version 10.5
Because text item delimiters now respect considering and ignoring attributes, they now are case-insensitive by default. Formerly, they were always case-sensitive. To get the same behavior with 2.0 and pre-2.0, add an explicit considering case statement.

#2
For speed reasons I would accomplish those things with the shell (see e.g. the man pages for grep, awk, sed), or a perl or ruby solution

Jeez, didn’t know it was THAT simple to implement the case sensitive thing.
This is one of the reasons why Applescript totally ROCKS :wink:

Regarding the regular expression thing, it seems a lot more complicated than I thought.

So here’s a bunch of dumb questions for ya !

– First of all, what are the man pages? Looked online but only found references to Linux, and I’m kinda confused for a change.
– Second of all, I read (here or some place on the net) that satimage.osax, a scripting addition, could help in some way with the support of regular expressions. How do I use that thing ?
– Third of all :

… WHAT? :confused: haha!

I don’t know jack about either perl or ruby …

thanks :wink:

up !

I’ve been surfin the web and documenting myself about regular expressions. (this link for example). But I found nothing about how I can work with regexp and applescript …

I’d like to at least be able to use wildcards, but I have no idea how to: the resources on applescript and regexp are almost non-existent.
I noticed, once more, that in the few links I found, Satimage is mentionned.

Can anyone tell me how to use satimage with AS ? Their doc is too confusing for me …
Thanks

¢ open Terminal.app
¢ type (for example) man awk
¢ press return

or search google for tutorial awk or similar keywords

Hi!

Here is some code from a little project of mine, the variables should be self-explaining. For simple string search, AppleScript does the trick, here you can add the “considering”. Basic and extended regex are first cleaned of single quotes, then passed to the shell using sed (not awk), then the return gets the single quotes back. (They would cause errors, I use a character as temporary stand-in that I expect to never see in an iTunes track entry – the “per ten thousand sign”.)


on textreplace()
	
	-- PREPARE COMMANDS FOR THE SHELL
	if SearchString contains "'" then set SearchString to NoQuote(SearchString) -- avoid single quotes that would confuse 'do shell command'
	if ReplaceString contains "'" then set ReplaceString to NoQuote(ReplaceString)
	
	if SearchType is "Basic Regular Expressions" then -- assemble commands
		set ShellCommand to " | sed 's/" & SStrg & "/" & RStrg & "/g'" -- plain sed
	else if SearchType is "Extended Regular Expressions" then
		set ShellCommand to " | sed -E 's/" & SStrg & "/" & RStrg & "/g'" -- sed with -E
	end if
	
		-- LOOP THROUGH THE TEXTS
	set TIDs to AppleScript's text item delimiters -- store the defaults
	repeat with J from 1 to (count items of TextList) -- process every text
		set OldText to (item J of TextList)
		if OldText contains "'" then -- avoid single quotes 
			set OldTextNoQuote to NoQuote(OldText)
		else
			set OldTextNoQuote to OldText
		end if
		if SearchType is "String Search/Replace" then
			set AppleScript's text item delimiters to SearchString
			set tempText to text items of OldTextNoQuote
			set AppleScript's text item delimiters to ReplaceString
			set NewText to (tempText as string)
		else
			try
				set NewText to (do shell script "echo " & quoted form of OldTextNoQuote & ShellCommand) -- search/replace using the shell
			on error
				display dialog "Error. Go back to coding."
				return
			end try
		end if
		if NewText contains "±" then set NewText to requote(NewText) -- get the single quotes back
	end repeat
	set AppleScript's text item delimiters to TIDs -- back to defaults
end textreplace

on NoQuote(Strg)
	set AppleScript's text item delimiters to "'"
	set NewStrg to every text item of Strg
	set AppleScript's text item delimiters to "±" -- a character to stand in for the single quote
	return (NewStrg as string)
end NoQuote

on requote(Strg)
	set AppleScript's text item delimiters to "±"
	set NewStrg to every text item of Strg
	set AppleScript's text item delimiters to "'"
	return (NewStrg as string)
end requote

Note: the script above was not tested, it is copied out of something. Thanks to Chris for helping me in an early stage getting there!

Good luck with your project,
LG

Correction: RStrg and SStrg should be ReplaceString and SearchString everywhere. But I guess you can figure it out despite the typos.
LG

Thanks a lot man,

but I’m not getting what your script does: does it simply enable to use regexp when searching ? :confused:

edit: i guess the source of my “not understanding” your script is that i don’t know SED syntax at all … so I would not know how to use it!

Hi, BS0D,

It’s not a complete script. It is a code snippet that was meant to show you one way how you could approach your task.

For this code to work, you’d have to pass it some variables. “OldText” would hold the text that you want to change. What you want to search for goes into “searchString”, what you want it to be replaced with in “ReplaceString”. In “SearchType” you’d give it the type of replacing you want (simple text replacing, or one of the kinds of regular expressions). All should be global variables.

You should be familiar with handlers/subroutines and variables, though. If not, you will soon find that this is almost essential for more complex AppleScripts. :slight_smile:

Here’s a trick in case you are a learner (like me): print out the code, if necessary several copies, then use color pens to circle and connect the variables, for each variable a different color. You’ll quickly begin to see the structure. And then you’d be off improving on it or writing your own – better – code in an instant.

Re sed:
All you need to know for this command to work is that the sed command is broken down into parts that don’t change ( “sed -E 's/”, “/” , and “/g’”) and in between goes the stuff that varies.

If you intend to do some shell command or other once in a while (it’ll become damn useful quickly!), I recommend a book for reference. I found this one very good in explaining: Sobell & Seebach, A Practical Guide to UNIX for Mac OS X Users.

BTW, the part with the NoQuote subroutines can be avoided with some alterations… But that’s left as an exercise to the reader. :smiley:

Good luck,
LG