BBEDIT 6.5 bizarre behaviour with a script...

I have written a script that calls BBedit to do a find a replace across multiple files. I got the BBedit Applescript code from recording within the program itself. I did a manual search and replace for example code, but I want to replace the search string and replace with string with variable values within applescript. Seems pretty straightforward.

Here is a code snippet:

(tell application “BBEdit 6.5”
activate
replace “CBB91F15-10D9-4A91-9C6D-84F29E93F44F” using “entry1” saving yes searching in alias “Macintosh HD:Users:old_bfindlay:Sites:” options {search mode:grep, case sensitive:true, match words:false, extend selection:false, showing results:true}
end tell
)
tell application “BBEdit 6.5”
activate
display dialog (current_name)
replace current_name using new_item_name saving yes searching in alias “Macintosh HD:Users:old_bfindlay:Sites:” options {search mode:grep, case sensitive:true, match words:false, extend selection:false, showing results:true}

the commented out bit is the recorded from BBEDIT code (it works when I uncomment it) and the other is the same code with current_name and new_item_name taking the place of the strings CBB91F15-10D9-4A91-9C6D-84F29E93F44F and entry1. When I run this - it fails. BBedit activates, runs through the files, but fails to find or replace anything. I have done a few error checking procedures:

A: I did a comparison in Applescript with :

if current_name = “CBB91F15-10D9-4A91-9C6D-84F29E93F44F” then
beep 5
display dialog (“They match!”)
end if

just before passing current_name to the BBedit tell statement. (The dialogue pops up - indicating that current_name contains the proper string value)

B: I tried parsing " symbols before and after the string value of current_name - again no luck.

Yet if I replace just the current_name in the BBedit code with its string value - the search works!! How do I get this bit of BBEdit code to accept variable input??

AAAARRRGGHHHH- two hours of troubleshooting on this, and my eyes are spirals! To me, this code snippet looks just fine!!! What is the problem??

Ok folks - I am going insane here! Just to make sure that the problem was not in some surrounding code (like where the BBedit tell was placed), I moved all of the BBedit stuff to its own handler, passed the variables to that handler then tried it there.

on grep_it(current_name, new_item_name)
tell application “BBEdit 6.5”
activate
if current_name = “CBB91F15-10D9-4A91-9C6D-84F29E93F44F” then
beep 2
display dialog (“The string is correct”)
end if
–set current_name to “CBB91F15-10D9-4A91-9C6D-84F29E93F44F”

	replace current_name using new_item_name saving yes searching in alias "Macintosh HD:Users:old_bfindlay:Sites:" options {search mode:grep, case sensitive:true, match words:false, extend selection:false, showing results:true}
	
end tell

end grep_it

The above code does display the dialog - indicating that this handler sees the correct string value, yet fails on the replace. (BBedit activates, cranks through the files, but fails to find or replace anything - no results window pops up.)

However, if i uncomment set current_name to “CBB91F15-10D9-4A91-9C6D-84F29E93F44F”, just before passing it to the replace command, it works! Holy Crap Batman - it is THE SAME value!! Tested by the if logic. If I set it manually, it works, if I set it by passing the variable into the handler, it doesn’t! (Course without accepting the variable, the entire script is useless!!)

-Brick walled on this one.

Hmm - progress, of a sorts. I have found the part in the script that is screwing things up. Just don’t understand WHY or HOW it is screwing it up.

Before I call the BBedit code, I trim the extension and the “.” off of the filename that I am replacing, as per:

tell application “Finder”
set extension to name extension of this_item
set trim_count to (count of characters in extension) + 1
set current_name to (characters 1 thru ((count of characters in current_name) - trim_count) of current_name) as string --trims the extension off the filename
set the parent_container_path to (the container of this_item) as text
–set grep_target to (container of (container of (parent_container_path as alias))) as string --use two levels up from current folder as grep path
set new_item_name to new_item_name as string
end tell

If I call the BBedit code BEFORE this tell statement, it DOES work - mind you it is finding “CBB91F15-10D9-4A91-9C6D-84F29E93F44F.html” and replacing it with 'entry1". (Not the behaviour I want, but it does demonstrate that the BBEdit code appears to function)

However, If I place the call AFTER I strip the extension and dot from the filename, it fails.

the line that appears to screw things up is

	set current_name to (characters 1 thru ((count of characters in current_name) - trim_count) of current_name) as string --trims the extension off the filename

Now - to my mind, this first turns current_name into a list of characters, but it then coerces it back to a string.

Recall, that I put a checking if statement in the code

if current_name = “CBB91F15-10D9-4A91-9C6D-84F29E93F44Fl” then
beep 2
display dialog (“The string is correct”)
end if

just before the BBedit replace command, and Applescript does indeed see this as the correct string! However, BBedit does not.

is this a program bug in BBedit? Applescript? My brain? What?

Feels like I am having a diologue with myself, but for the interested reader, I solved this puzzle:

Ok, that was weird!

I finally figured out that BBedit was seeing something different in the replace text, than what Applescript was displaying on the screen. I clued in that setting the current_name to characters, and then coercing it back to string was the problem. Turns out that before doing this it was class “utxt” and after it was class “TEXT”. That was the problem. Coerced it back to unicode, and all went well.

Dang - that was subtle, and to me still appears to be a fault in BBedit - it states that it wants a string input in the BBedit dictionary.

Oh well, on to the next stage…