Skip to content

Instantly share code, notes, and snippets.

@kawaz
Last active July 25, 2025 05:20
Show Gist options
  • Select an option

  • Save kawaz/bc64d887eb8a51067ad4a2c5b1eb0ef1 to your computer and use it in GitHub Desktop.

Select an option

Save kawaz/bc64d887eb8a51067ad4a2c5b1eb0ef1 to your computer and use it in GitHub Desktop.
Script to set iTerm2 background image. iTerm2の背景画像を設定するスクリプト。
#!/usr/bin/env bash
usage() {
echo "Usage: $(basename "$0") ...file" >&2
echo " Randomly select one file and set it as iTerm2 background image" >&2
echo " -u Unset background image (remove it)" >&2
echo "Example:" >&2
echo " iterm2-set-background-image.sh ~/.config/iterm2/images/*.{png,jpg}" >&2
exit 1
}
# Check arguments
files=()
for f in "$@"; do
if [[ $f == -u ]]; then
files=("")
break
fi
if [[ -f $f ]]; then
files+=("$f")
fi
done
if (( ${#files[@]} == 0 )); then
usage
fi
# Randomly select one
idx=$(( RANDOM % ${#files[@]} ))
file=${files[$idx]}
# Set background image
script='
on run argv
set arg1 to item 1 of argv
tell application "iTerm2"
tell current session of current window
set background image to arg1
end tell
end tell
end run
'
osascript -e "$script" "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment