How to search for curly braces in search strings in AS

This should have a very simple solution but I just ain’t seeing it.

In AppleScript I want to search within text strings for the open and close curly brace characters.

Typing the search term as “{” or “}” doesn’t work. Nor does escaping it as “{” or “}” or using “ascii character” or “character id” or as Unicode text.

Not really sure what you’re attempting to do. Applescript doesn’t really search that way.

But here is an example of how to replace braces with brackets — eg “{” with “[”. I used your final paragraph as source text.

set tb to "Typing the search term as “{” or “}” doesn’t work. Nor does escaping it as “{” or “}” or using “ascii character” or “character id” or as Unicode text."

set text item delimiters to "{"
set split to text items of tb
set text item delimiters to "["
set joined to split as text


set text item delimiters to "}"
set split to text items of joined
set text item delimiters to "]"
set joined to split as text

What is your intention here?

joecab, please post some code, so we can see what you were doing.

Here’s the code. I’m doing this in BBEdit. Basically it looks for any text in curly braces and deletes that text:

				set tempTextString to ""
				set lengthOfThisString to length of currentPara
				set thisPartIsCurlyBraced to false
				set openCurlyBrace to "{"
				set closeCurlyBrace to "}"
				
				repeat with k from 1 to lengthOfThisString
					
					set testChar to character k of currentPara
					
					if thisPartIsCurlyBraced is false and testChar is not openCurlyBrace then
						set tempTextString to tempTextString & testChar
					end if
					
					if thisPartIsCurlyBraced is false and testChar is openCurlyBrace then
						set thisPartIsCurlyBraced to true
					else if testChar is closeCurlyBrace then
						set thisPartIsCurlyBraced to false
					end if
					
				end repeat	

Change the above line to

set testChar to contents of character k of currentPara

BBEdit has its own nomenclature.

The BBEdit command to do a simple search and replace usually looks like this:

tell application "BBEdit"
   set mySearchOptions to {starting at top:true, showing results:false}
   tell document "scratch.txt"
      replace "search for this" using "replace with this" options mySearchOptions
   end tell
end tell

But you want to match everything inside a block of curly braces, which calls for regex. The script you want is:

tell application "BBEdit"
   set mySearchOptions to {starting at top:true, showing results:false, search mode:grep}
   tell document "scratch.txt"
      replace "\\{[^{}]*\\}" using "" options mySearchOptions
   end tell
end tell

Note the search options need to specify search mode: regex.

Here, the regex to match a pair of curly braces and everything inside is

  \{[^{}]*\}

but the backslashes themselves need to be escaped inside the AppleScript string, which leads to

  \\{[^{}]*\\}