Qt Creator and iTerm2

developmentitermterminal

Qt Creator opens the UGLY Terminal app for running/debugging programs. Using this script:

#! /bin/bash

# ugly escaping: for apple script \ and " need to be escaped, whereas %q takes care of all bash escaping
declare -a args
mydir=`pwd`
mydir=$(printf '%q' "$mydir")
mydir="${mydir//\\/\\\\}"
args[0]="cd ${mydir//\"/\\\"};"
for a in "$@" ; do
    x=$(printf '%q ' "$a")
    x="${x//\\/\\\\}"
    args[${#args[@]}]="${x//\"/\\\"}"
done
mArgs=${args[@]:0}

osascript <<EOF
    --Terminal opens a window by default when it is not running, so check
    on applicationIsRunning(applicationName)
            tell application "System Events" to count (every process whose name is applicationName)
            return result is greater than 0
    end applicationIsRunning
    set terminalWasRunning to applicationIsRunning("Terminal")

    set cdScript to "$mArgs"
    tell application "Terminal"
        --do script will open a new window if none given, but terminal already opens one if not running
        if terminalWasRunning then
            do script cdScript
        else
            do script cdScript in first window
        end if
        set currentTab to the result
        set currentWindow to first window whose tabs contains currentTab
        activate
    end tell
EOF

How can I force it to use iTerm2 instead of Terminal..?

Best Answer

The script you posted is a pretty good base for some edits. The second part is AppleScript, and iTerm2 offers a nice interface with it.

First, create a script (let's say ~/newiTerm.sh) and put the following content

#! /bin/bash

# ugly escaping: for apple script \ and " need to be escaped, whereas %q takes care of all bash escaping
declare -a args
mydir=`pwd`
mydir=$(printf '%q' "$mydir")
mydir="${mydir//\\/\\\\}"
args[0]="cd ${mydir//\"/\\\"};"
for a in "$@" ; do
    x=$(printf '%q ' "$a")
    x="${x//\\/\\\\}"
    args[${#args[@]}]="${x//\"/\\\"}"
done
mArgs=${args[@]:0}

osascript <<EOF
set cdScript to "$mArgs"
tell application "iTerm2"
    set newWindow to (create window with default profile)
    tell newWindow
        select
        set _session to current session
        tell _session
            write text cdScript
        end tell
    end tell
end tell

Then, go to Qt Preferences ( ⌘, ) > Environment > System > Terminal and set the value to ~/newiTerm.sh

Works well for me.

Cheers