Replacing a text string in BBEdit?

In theory, this should be very easy, but I’m stumped and can’t seem to find the answer in the BBEdit dictionary. What I want to do is create a script that will replace a particular string of text with another string of text that will be inputed by the user. That is, when the script starts, it will ask for a word via a dialog box. That will be saved as a variable.

Then the script will open a BBEdit file, lood for a particular string, then replace it with the word that the user input before.

If any one has any ideas, or can point me in the right direction, I would appreciate it.

TIA

Kevin

This code was generated by recording a search and replace in BBEdit 6.5. It should provide a starting point. Note: If Bare Bones still uses the version number in the application’s name (dumb move from a great company), you’ll need to enter the correct version number in the script. The script works on BBEdit’s front window.

set DD to (display dialog "Enter replacement text for the serach." default answer "")

tell application "BBEdit 6.5"
	replace "search string" using (text returned of DD) searching in text 1 of text window 1 options ¬
		{search mode:literal, starting at top:true, wrap around:false, reverse:false, case sensitive:false, match words:false, extend selection:false}
end tell

– Rob

Rob, thanks for the start. So this is what I have now.

set mainFile to choose file
set DD to (display dialog "Enter replacement text for the serach." default answer "")

tell application "BBEdit"
	activate
	open mainFile
	replace "newUser" using DD searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:true, backwards:true, case sensitive:false, match words:true, extend selection:false}
end tell

The problem is after runnning the script, I get the following error:

“BBEdit got an error: A multi-byte non-Roman search string can’t be used to search a single-byte test buffer (application error code 25002).”

Any idea what that means? BTW I am using BBEdit 7.1.3 and OS X (10.3.3)

Thank you again for your help.

Kevin

It’s might be because, in the BBEdit code, you have: using DD

It needs to be: using (text returned of DD)

DD is a record that holds various values returned by the dialog. You want BBEdit to use only a part of the record (text returned, which is the text entered by the user).

– Rob

I get it now. It works after I made the change.

Thank you for the help.