0

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.

1
  • key code is a System Events command. Commented Jun 14, 2022 at 18:24

1 Answer 1

0

BBedit can replace text without keystrokes

tell application "BBEdit"
   tell front document
      replace "===" using "---" & return & "===" options {wrap around:true}
   end tell
end tell
Sign up to request clarification or add additional context in comments.

1 Comment

Thx 4 this. Can BBEdit also directly accept arrow keys? For what I need is not to replace the found "====", but to insert a tab two lines above "====", leaving the line directly above in place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.