set CR to ASCII character 13
set NL to ASCII character 10
tell application "Script Editor"
tell front document
set the target_string to "--XXXX"
set the selected_text to contents of selection
if the selected_text is "" then
set the selected_text to the target_string
set the script_text to ""
set the script_text to the script_text & "tell application \"System Events\"" & CR
set the script_text to the script_text & tab & the selected_text & CR
set the script_text to the script_text & "end tell" & CR
set the replacement_string to "-- insert actions here"
else
set the script_text to ""
set the script_text to the script_text & "tell application \"System Events\"" & CR
if last character of selected_text is in {CR, NL} then
set the script_text to the script_text & tab & the selected_text
else
set the script_text to the script_text & tab & the selected_text & CR
end if
set the script_text to the script_text & "end tell" & CR
set the replacement_string to ""
end if
set contents of selection to script_text
try
check syntax
end try
my replace_and_select(target_string, replacement_string)
try
check syntax
end try
end tell
end tell
on replace_and_select(target_string, replacement_string)
tell application "Script Editor"
tell the front document
set this_text to the contents
set this_offset to the offset of the target_string in this_text
if this_offset is not 0 then
set selection to characters this_offset thru (this_offset + (length of the target_string) - 1)
set the contents of the selection to the replacement_string
else
set selection to {}
end if
end tell
end tell
end replace_and_select
wtf???
