Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTerm2 Get Current Session Profile?

So I know there is a way to set the session profile via echo -e "\033]50;SetProfile=Foo\a" but is there a way to get the current session's profile?

like image 268
gdoubleod Avatar asked May 27 '13 07:05

gdoubleod


3 Answers

You can get the session by checking the $ITERM_PROFILE variable (For those looking now). It can help to look at the printout from printenv to see things like this.

like image 149
Dragoe Avatar answered Sep 28 '22 05:09

Dragoe


I didn't find any way to get the name of the profile apart from something like this:

tell application "System Events" to tell process "iTerm"
    keystroke "i" using command down
    set p to value of text field 1 of tab group 1 of group 1 of window 1
    click button 1 of window 1
end tell
p

You might be able to identify profiles by some properties:

tell application "iTerm" to tell current session of terminal 1
    background color is {0, 0, 0} and transparency is 0.0
end tell

Properties documented in the dictionary:

background color, background image path, bold color, contents, cursor color, cursor_text color, foreground color, id, name, number, selected text color, selection color, transparency, tty

like image 37
Lri Avatar answered Sep 28 '22 05:09

Lri


This is the simplest Applescript I have come up with to do this:

tell application "iTerm-2"
    get profile name of current session of current window
end tell

And here's a simple bash script which uses this to set a profile, run a command, then set the profile back again. It uses the first argument as the profile, and the rest as the command (e.g. script < profile > < command >).

#/bin/bash

#get the current window settings
CUR_SETTINGS=`osascript -e 'tell application "iTerm-2"
get profile name of current session of current window
end tell'`

#Restore the current settings if the user ctrl-c's out of the command
trap ctrl_c INT
function ctrl_c() {
        echo -e "\033]50;SetProfile=$CUR_SETTINGS\a"
        exit
}

#set profile
echo -e "\033]50;SetProfile=$1\a"
shift

#run command
$@

#Restore settings
echo -e "\033]50;SetProfile=$CUR_SETTINGS\a"
like image 22
Paul Rayner Avatar answered Sep 28 '22 03:09

Paul Rayner