Setting Window Width with a Script

30 views
Skip to first unread message

Jeff Shepherd

unread,
Jul 22, 2024, 7:26:31 PMJul 22
to iterm2-discuss
Is iTerm2 AppleScript-able? I'm trying to set the width of a window using an AppleScript like the following

tell application "/Applications/iTerm.app"

  activate

    tell current window

      set width of current session to 800 -- Set the desired width in pixels

    end tell

end tell

but am getting an error "iTerm got an error: Can’t make width of current session of current window into type specifier."

Is it possible to set the iTerm2 window with a script? 

George Nachman

unread,
Jul 22, 2024, 7:42:46 PMJul 22
to iterm2-...@googlegroups.com
You can’t directly resize a session to a pixel size. Instead, resize the window by modifying its bounds:

tell application "iTerm2"

set current_window to current window

tell current session of current window

set new_width to 800

set current_window_bounds to bounds of current_window

set current_height to (item 4 of current_window_bounds) - (item 2 of current_window_bounds)

set bounds of current_window to {item 1 of current_window_bounds, item 2 of current_window_bounds, (item 1 of current_window_bounds) + new_width, (item 2 of current_window_bounds) + current_height}

end tell

end tell

Alternately, if you know the number of pixels per column, you can change the number of columns:

tell application "iTerm2"

set columns of current session of current window to 100

end tell


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/5fb6ea4f-d6fa-4ec9-bb92-4d387ca16007n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages