Create Zoom Shortcut in Mac OS X Terminal

in Programming

So lately I’ve been getting into this flow of wanting to make a terminal screen full-screen when I need to focus or read long lines. And I’ve been getting sick of having to use the mouse to push the green plus sign to toggle this.

I did a quick search and found an Apple Script that will work for Terminal and TextMate, which are my bread and butter, respectively.

-- A script to automatically zoom the frontmost window.
-- © 2006 by Daniel Jalkut, Inspired by:
--
-- http://www.macosxhints.com/article.php?story=20051227001809626&lsrc=osxh
--

tell application "System Events"
	set frontAppName to name of first application process whose frontmost is true
end tell

set tryZoomedAttribute to true
set tryMenuScripting to false
set tryButtonScripting to false
set allMethodsFailed to false

-- Special cases. For Applications whose behavior we know responds to one or the other
-- method, force that method here.
if (frontAppName is equal to "iTunes") then
	set tryZoomedAttribute to false
	set tryMenuScripting to true
end if

if (tryZoomedAttribute is equal to true) then
	tell application frontAppName
		try
			set zoomed of window 1 to not (zoomed of window 1)
		on error
			set tryMenuScripting to true
		end try
	end tell
end if

-- Make sure the user has UI scripting enabled before we go on...
if ((tryMenuScripting is equal to true) or (tryButtonScripting is equal to true)) then
	tell application "System Events"
		if UI elements enabled is false then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.universalaccess"
				display dialog "UI element scripting is not enabled. Check 'Enable access for assistive devices'"
			end tell
		end if
	end tell
end if

if (tryMenuScripting is equal to true) then
	tell application "System Events"
		tell process frontAppName
			try
				click menu item "Zoom" of menu of menu bar item "Window" of menu bar 1
			on error
				set tryButtonScripting to true
			end try
		end tell
	end tell
end if

if (tryButtonScripting is equal to true) then
	-- UI Scripting method:
	tell application "System Events"
		try
			tell process frontAppName
				click button 2 of window 1
			end tell
		on error
			set allMethodsFailed to true
		end try
	end tell
end if

if (allMethodsFailed is equal to true) then
	display dialog "I'm sorry, I couldn't figure out how to zoom this window."
end if

As they suggest you’ll want to add it as a trigger via Quicksilver. This is where the article lacks information about how to do this so I give that here. I assume you’re using Quicksilver.

  1. Create a folder ~/Library/Scripts (if you don’t already have one)
  2. Copy the above script and paste it into Script Editor.
  3. Save that script as Green Zoomer.scpt in your ~/Library/Scripts folder.
  4. Use the Quicksilver keyboard shortcut Command + ; to bring up the Catalog.
  5. Click Custom on the left hand side, then on the bottom right you’ll see the plus sign to add a Source.
  6. Click that plus sign and choose File & Folder Scanner.
  7. Use the keyboard shortcut Command + Shift + G to type in a manual path.
  8. Type in ~/Library/Scripts, click Open.
  9. Click on the refresh icon in the bottom menu to make sure Quicksilver picks up the new script you added.
  10. Use the Quicksilver keyboard shortcut Command + ‘ to bring up the Triggers.
  11. Click the plus sign at the bottom and choose HotKey.
  12. The Select an item dialog box will appear or you can make it appear by clicking on the new trigger you’ve added.
  13. Type in the first field Green Zoomer and Quicksilver will pickup the file you created, and give it a default action of Run. That’s what we want.
  14. Click Save and you’re ready to zoom!

My Firefox browser would give me the error ‘UI element scripting is not enabled. Check ‘Enable access for assistive devices’ when I tried to zoom it.

Daniel’s script even apologizes for the error. But no apology is necessary, because OS X pops up Universal Access and you can just click on the Enable access for assistive devices check box and you’re good.

Thanks Simon for the original and Daniel for the rewrite!

2 Comments

2 Comments

  1. Alternative, and with much less work, you could…

    1. Open the “Keyboard & Mouse” preference pane.
    2. Click on the “Keyboard Shortcuts” tab.
    3. Scroll down in the list to “Application Keyboard Shortcuts”.
    4. Hit + to create a new one.
    5. In the dialog:
    – Application: Choose the application of your choice (in this case, it sounds like you want it for all applications… though you could create two entries for Terminal and TextMate if you only wanted it for those).
    – Menu Title: Zoom
    – Keyboard Shortcut: Press Cmd+Shift+G (or whatever you want)

    There you go.

Leave a Reply

You must be logged in to post a comment.

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>