2

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?

1

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
  • With exec command q I get a Syntax Error: Expected end of line, etc. but found identifier. – jherran Nov 28 '14 at 11:41
  • Please paste the entire script please. – f01 Nov 28 '14 at 11:43
  • Replace do script line with exec command "ls" from my question. – jherran Nov 28 '14 at 11:47
  • Syntax Error: Expected end of line but found identifier. on line set _session to current session of current terminal, the error appears on the word terminal. – jherran Nov 29 '14 at 9:41
1

From the Alfred blog post: "You can find some excellent iTerm integration AppleScripts for the older and newer versions of iTerm on Alfred user Stuart C Ryan's Custom iTerm Applescripts for Alfred Github page."

Script for iTerm 2.1.1:

-- This is v0.3 of the custom script for AlfredApp for iTerm 2.1.1
-- Please see https://github.com/stuartcryan/custom-iterm-applescripts-for-alfred/
-- for the latest changes.

on is_running(app_name)
    tell application "System Events" to (name of processes) contains app_name
end is_running

-- Please note, if you store the iTerm binary in any other location than the Applications Folder
-- please ensure you update the two locations below (in the format of : rather than / for folder dividers)
-- this gets around issues with AppleScript not handling things well if you have two iTerm binaries on your system... which can happen :D

on alfred_script(q)
    if is_running("iTerm") then
        run script "
            on run {q}
                tell application \":Applications:iTerm.app\"
                    activate
                    try
                        set myterm to the first terminal
                    on error
                        set myterm to (make new terminal)
                    end try
                    tell myterm
                        set mysession to (launch session \"Default Session\")
                        tell mysession to write text q
                    end tell
                end tell
            end run
        " with parameters {q}
    else
        run script "
            on run {q}
                tell application \":Applications:iTerm.app\"
                    activate
                    tell the first terminal
                        tell the last session to write text q
                    end tell
                end tell
            end run
        " with parameters {q}
    end if
end alfred_script
0

I'm sure there is a better way, but, from now, this is the only way that works for me:

tell application "iTerm"
    activate
    set the clipboard to q
    delay 0.5
    tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke "v" using command down
    tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke return
end tell
0

As of Alfred v3.7 and iTerm2 v3.2.0 and MacOS v10.13.6, this solution works:

on alfred_script(q)

    tell application "iTerm"
        activate

        set _profile to "Default"

        -- if there are no terminal windows...
        if (current window is missing value) then
            set _window to (create window with profile _profile)

        -- otherwise get the current window and open a new tab
        else
            set _window to current window
            tell _window
                create tab with profile _profile
            end tell
        end if

        -- execute the command
        tell current session of _window
            write text q
        end tell

    end tell

end alfred_script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .