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.]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20041030224514431