How to Change the Background Image in the Terminal with a Command in MacOS

Change background picture in iterm on MacOS automaticaly

Fullstack CTO
2 min readMar 7, 2023

This guide is specifically for MacOS users. The primary application I use for working in the terminal is iTerm, so I will be changing the background image using it. My terminal is already customized to my liking, and you can find the complete configuration on my GitHub page:

Why change the background image? I wanted to have the background image change when I switched to root mode or when I logged in remotely through SSH. This way, I would always know where I was and when. Plus, it just looks cool and trendy — a bit hipster, if you will.

To change the background image in the terminal in MacOS, I used AppleScript:

--Change the background picture in iTerm by arguments--
on run argv
tell application "iTerm"
tell current session of current window
set background image to "/Users/mayorov/Pictures/iTerm/" & (argv as text)
end tell
end tell
end run

By default, all scripts are saved in the directory:

~/Library/Mobile\ Documents/com~apple~ScriptEditor2/Documents

I placed the images in the standard directory for images, in a subfolder named iTerm.

Next, I created a shell function:

function chbg()
{
osascript /Users/mayorov/Library/Mobile\ Documents/com~apple~ScriptEditor2/Documents/bgImgIterm.scpt $@
}

Then, in my profile scripts, I added:

if [ $(id -u) = 0 ]
then
chbg "root.jpg"
else
chbg "user.jpg"
fi

function logout() { chbg "user.jpg" }
trap logout EXIT

--

--

Fullstack CTO
Fullstack CTO

Written by Fullstack CTO

CTO and co-founder at NEWHR & Geekjob

No responses yet