Toggling Transparency in iTerm2

I recently started developing full-time in Vim again because all my code has to run on remote virtual machines. I like using iTerm2 with some transparency enabled so I can see what’s going on in my browser, but it’s started giving me a headache now that I’m spending all day in my terminal. I couldn’t find a built-in hotkey for toggling transparency in iTerm2, so I cooked something up with AppleScript.

  1. Launch Automator, which comes pre-installed with your Mac.
  2. When prompted for a new document type, select “Service”.step-2
  3. In the “Actions” tab on the left, search for “applescript”.step-3
  4. Drag the “Run AppleScript” result into the window on the right.step-4
  5. Copy and paste this script into the text area, then hit the hammer icon to build it.

    tell application "iTerm"
        if the transparency of the current session of the current window > 0 then
            repeat with aWindow in windows
                tell aWindow
                    repeat with aTab in tabs of aWindow
                        repeat with aSession in sessions of aTab
                            tell aSession
                                set transparency to 0
                            end tell
                        end repeat
                    end repeat
                end tell
            end repeat
        else
            repeat with aWindow in windows
                tell aWindow
                    repeat with aTab in tabs of aWindow
                        repeat with aSession in sessions of aTab
                            tell aSession
                                set transparency to 0.3
                            end tell
                        end repeat
                    end repeat
                end tell
            end repeat
        end if
    end tell
    

    step-5

  6. Change the dropdown boxes on top so that they read “Service receives no input in iTerm”. You may have to click “Other…” in order to select iTerm.step-6
  7. Save this as “Toggle Transparency”.step-7
  8. Open System Preferences, and go to the Keyboard section, then the Shortcuts tab, then the Services category.step-8
  9. Click on “Add Shortcut” next to “Toggle Transparency”, and record a key combination. I use Cmd + Shift + U.

Try it out! Open iTerm2, and hit your key combination a few times. You can also run the script by click on iTerm2 in your menu bar and going to Services. You may want to tweak the AppleScript I provided if your default profile has transparency because my default profile does not have transparency. Just flip the transparency values of 0.3 and 0.