I’m trying to write an AppleScript to insert ["some text"] above ["found text"].
Here is what I wrote:
tell application "BBEdit"
activate
find "====" searching in text 1 of text document 1 with selecting match
key code 126 # Up Arrow
key code 126 # Up Arrow
keystroke "<tabhere>"
key code 125 # Down Arrow
key code 125 # Down Arrow
key code 125 # Down Arrow
end tell
I get the following error, with the first instance of "code" highlighted:
"Expected end of line, etc. but found identifier"
Following suggestion by @red_menace, I amended as follows:
tell application "BBEdit"
activate
find "====" searching in text 1 of text document 1 with selecting match
tell application "System Events"
key code 126 # Up Arrow
key code 126 # Up Arrow
keystroke "<tabhere>"
key code 125 # Down Arrow
key code 125 # Down Arrow
key code 125 # Down Arrow
end tell
end tell
… which works, but of course only once.
Now how can I get this into a repeat loop that runs until the end of the document? The following attempt runs amok (it caused my computer to cycle wildly through all open windows in all applications and I had to fore quit):
tell application "BBEdit"
activate
repeat
find "====" searching in text 1 of text document 1 with selecting match
set findClass to find "====" searching in text 1 of text document 1 with selecting match
if not found of findClass then exit repeat
tell application "System Events"
key code 126 # Up Arrow
key code 126 # Up Arrow
key code 48 # Tab
key code 125 # Down Arrow
key code 125 # Down Arrow
key code 125 # Down Arrow
end tell
end repeat
end tell
Thanks for any further help.
key codeis a System Events command.