Excluding commented strings from search results?

I wonder if there’s an easy way to exclude commented strings from search results in SD?

In other words, I want to make sure that specific string isn’t used in “live code” anymore. I don’t care if it’s anywhere in the comments.

I looked through Script Debugger’s AppleScript dictionary but couldn’t find an obvious way to achieve this.

I realize that I can analyze every found string for preceding comment marks -- and (* in the parent and preceding paragraphs. But I wonder if there’s an easier way to achieve this.

Thanks!

I think came up here before, and I believe the solution was to use RegEx to search the script. You may want to search these forums for RegEx. (If you’re familiar with Regular Expressions, you can use the RegEx checkbox in the search field and enter your search expression, then put that into a script in the scripts menu).

Thanks Ed,

I tried to search forums for RegEx but didn’t find anything relevant to my quest.

My knowledge of RegEx is rudimentary. I’ll see if I can figure out how to at least exclude paragraphs that start with --.

It would be helpful if SD’s AppleScript dictionary allowed to retrieve the font properties of text selection (such as font name and/or color) - but, as far as I understand, it doesn’t. I’ll submit a feature suggestion (although I’m sure it’s easier said than done).

I’ve included below a regex solution. The script prompts the user for a search string and then creates a regex pattern, which is copied to the clipboard. This can then be pasted into the Script Debugger Find bar.

display dialog "Enter a search string" default answer ""
set searchString to text returned of result
if searchString is "" then error number -128
set regexPattern to "^.*" & searchString & ".*--.*$|^(?!.*--).*" & searchString & ".*$"
set the clipboard to regexPattern

I used the following script to test the regex pattern:

set s to "search string" --good afternoon
set s to "good afternoon" --a search string
set s to "another search string" --good afternoon
set s to "good afternoon" --another search string
set s to "search string"
--a search string

The regex pattern breaks if a code line contains two sets of comment characters.

Thanks @peavine, I’ll give it a try!

So @peavine’s solution does the job (with the known exception of lines with two sets of comment characters as well as the block comments).

Also, out of curiosity I asked both ChatGPT and Copilot to create this regex:

“regex: find string string1 only in paragraphs that do not contain string2”

Both AI tools produced some worthless nonsense every time. Copilot’s regex just found string1 regardless of any conditions. While the regex from ChatGPT simply stalled Script Debugger (it invoked the endless beachball, and I had to force-quit SD).

With Nigel’s help, I was able to resolve this issue and to simplify the regex pattern. The revised script that creates the regex pattern is:

display dialog "Enter a search string" default answer ""
set searchString to text returned of result
if searchString is "" then error number -128
set regexPattern to "(?m)^.*(?<!--.{0,100})" & searchString & ".*$"
set the clipboard to regexPattern

The script that I used to test the regex pattern is:

set s to "search string" --good afternoon
set s to "good afternoon" --a search string
set s to "another search string" --good afternoon
set s to "good afternoon" --another search string
set s to "search string"
--a search string --a search string
--a search string

The regex pattern requires that any comment characters that precede the search string be separated by no more than 100 characters. This can be increased if necessary.

Oh yeah this one works perfectly.

Thanks a lot @peavine - and @NigelGarvey of course.