Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Switch to and open new windows in Safari and Terminal Apps
This is kind of a simple idea, but very useful to me (so I thought I'd share it). I like to launch or switch to my applications with keystrokes, using Keyboard Maestro to bind the apps to simple things, like Command-Control-S for Safari, Command-Control-T for Terminal, etc.

One problem with switching to apps like this is that you don't get the nice benefit that clicking on them in the Dock gives you -- namely, if there are no open windows, a new one will be created for you (saving the oh-so-tedious trip to the File menu).

So, I wrote the following AppleScripts (which can also be launched from Keyboard Maestro) to active Safari and Terminal, and if there are no open windows, to then open one.

For Safari ... this doesn't count windows which are minimized to the Dock, so you'll get a new Window if there are no "open" windows:
tell application "Safari"
  activate
  set winCount to count of windows
  
  if winCount = 0 then
    make new document
  else
    set activeWindows to winCount
    repeat with i from 1 to winCount
      set wRef to window i
      if miniaturized of wRef then
        set activeWindows to activeWindows - 1
      end if
      if name of wRef is "Downloads" then
        set activeWindows to activeWindows - 1
      end if
    end repeat
    if activeWindows = 0 then
      make new document
    end if
  end if
end tell
For Terminal ... I couldn't find a "make new window" command, so this resorts to "GUI Scripting." Turn on "Enable access for assistive devices" in the the Universal Access preferences panel:
tell application "Terminal"
  activate
  set winCount to count of windows
  if winCount = 0 then
    try
      tell application "System Events"
        tell process "Terminal"
          tell menu bar 1
            tell menu bar item "File"
              tell menu "File"
                click menu item "New Shell"
              end tell
            end tell
          end tell
        end tell
      end tell
    on error
      return false
    end try
  end if
end tell
[robg adds: I haven't tested these scripts.]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (5 votes cast)
 
[10,717 views]  

Switch to and open new windows in Safari and Terminal | 5 comments | Create New Account
Click here to return to the 'Switch to and open new windows in Safari and Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Funny, the opposite of my desired behavior
Authored by: mal0rd on Nov 01, '04 01:08:18PM

I have recently wanted to be able to switch apps without launching a new window, but I didn't think it was possible until I read this hint. Luckily, cmd-tab works this way.



[ Reply to This | # ]
Alternative Applescript solution for Terminal
Authored by: haighy on Nov 01, '04 07:59:22PM

Terminal is a bit weird in that (as far as I know) there's no explicit way to create a new window. However, just running a script without specifying a window will create a new one.

I wrote this little script, which I've attached to a trigger with Quicksilver. It creates a new window in the Terminal if it needs to, and switches to the directory of the frontmost window in the Finder.


tell application "Finder" to get POSIX path of (target of front window as string)
set mypath to the result
tell application "Terminal"
	if ((count windows) is 0) then
		do script ("cd " & quoted form of mypath)
		activate
		return
	end if
	
	if front window is busy then
		do script ("cd " & quoted form of mypath)
	else
		do script ("cd " & quoted form of mypath) in front window
	end if
	activate
end tell

Combined with Default Folder's "Reveal in Finder" command, you can jump from an open dialog box to the same directory in the terminal in just two keystrokes. UI and CLI cooperating in a symphony of...uh...goodness. Oh yeah, and if you bind a key in the shell to type something like "open ." you can jump from the terminal to the current working directory in the Finder, i.e. the opposite direction. This is what I use in my .zshenv:


bindkey -s "^[l" " open .\012"
Putting \012 (octal for enter) on the end of the binding means you can make the leap in one keystroke, in this case opt-L (technically meta-L I guess). Note that this works for zsh, I don't know how to do it in bash.



[ Reply to This | # ]
Alternative Applescript solution for Terminal
Authored by: Djehuti on Mar 08, '06 06:10:35PM
In bash, it's

	bind '"\M-l": "open .\012"'
Or you can just put

	"\M-l": "open .\012"
in your ~/.inputrc file.

[ Reply to This | # ]
Switch to and open new windows in Safari and Terminal
Authored by: chabig on Nov 01, '04 08:42:40PM

What's so hard about switching to Terminal and then typing command-N?



[ Reply to This | # ]
Switch to and open new windows in Safari and Terminal
Authored by: hypert on Nov 02, '04 12:37:44PM

Haven't you been paying attention?!? It's all about saving keystrokes! :-)



[ Reply to This | # ]