Search code examples
applescriptiterm2

Applescript to change shortcut to be able to clear lines in iTerm2


I am trying to make an applescript to be able to cmd+L to get ctrl+L to clear lines in iTerm as suggested by this answer.

After copying a working, example applescript from here shown below,

tell application "System Events" to keystroke "l" using control down

I am trying to change the application to "iTerm2" as shown below,

tell application "iTerm2" to keystroke "l" using control down

so the shortcut in not global, but I get a Syntax error:

A identifier can’t go after this “"”.

Removing the quotes (this also works in the working example) brings up a different Syntax error:

"A identifier can’t go after this identifier."

What am I doing wrong?


Solution

  • Thanks to @gurkensaas's comment for the correct answer. I post the complete working script below for other people.

    tell application "iTerm" to activate
    delay 0.1
    tell application "System Events"
        try
            keystroke "l" using control down
        end try
    end tell
    

    Edit: I ended up using Better Touch Tool for this shortcut since it was much more responsive and fast.