MacOS – Alfred Terminal / Shell Command does not work

alfredapplescriptitermmacos

In Alfred, if I type > ls must open iTerm2 with that command. Well, does not work for me.

In Application if I select Terminal instead of iTerm2, works.

enter image description here

If I select Application → Custom, it shows the applescript code you can see below and works.

enter image description here

If I select Application → Custom and modify the tell application line replacing Terminal with iTerm, does not work.

enter image description here

If I open Script Editor and type the following applescript code, does not work:

tell application "iTerm"
    activate
    do script "ls"
end tell

If I remove do script line open iTerm.

Any ideas of why do script line does not work with iTerm?

Best Answer

This is a working example that I derived from the example in https://code.google.com/p/iterm2/wiki/AppleScript

Check out the comment from stefan.v...@gmail.com

tell application "iTerm"
    activate

    try
        set _session to current session of current terminal
    on error
        set _term to (make new terminal)
        tell _term
            launch session "Default"
            set _session to current session
        end tell
    end try

    tell _session
        write text "ls"
    end tell
end tell