Running Scripts or Launching Apps via Keyboard Shortcuts

Thursday, 2007-09-20; 17:53:00



So my friend who just switched to the Mac IMed me today and was wondering how to open a new window in Firefox via a keyboard shortcut without switching to the app. I was confused at first, 'cause there doesn't seem to be any reason why you would want to open a new window without interacting with it.

The root cause of his need was that he usually has Gmail open in a Firefox window, and keeps it minimized in the Dock. But when you switch to Firefox, the application naturally assumes that you want to start doing something, so since the only window that's currently open is minimized, it unminimizes it. If that's not what you want to do, you have to re-minimize it, manually open a new window, and only then can you go to the website you were looking for.

I've encountered this problem myself, with Safari. It does the exact same thing. Safari makes it a little easier because you can control-click on the icon in the Dock, and select "New Window". Firefox doesn't offer this. But it's still a little clumsy since you have to use the mouse to target the Dock icon and the Dock menu item. It'd be nice if there was a preference (system-wide perhaps?) that causes applications to treat minimized windows as not there -- i.e.: if you switch to a document-based app that has no windows open, it opens a new window; it might be advantageous if this functionality were duplicated if the only open windows in that app are minimized.

So I figured there'd be an easy way to do that via a keyboard shortcut. Now, let me be up front and say that personally, I don't want to download any third-party software if I don't have to. If I can find a way to activate scripts via the keyboard via built-in Mac OS X features, I'd much rather do it that way. For the sake of this discussion, third-party utilities are completely out the window.

My first instinct was to create an AppleScript that does what he wants -- namely, opens a blank window. That was simple:

tell application "Firefox"
        OpenURL ""
end tell


Then I figured the easiest way to get the keyboard shortcut would be to activate the Script Menu menu extra, place the script in there, and then define a shortcut in the Keyboard and Mouse preference pane. Since the Script Menu is available regardless of which application you're in, this would be especially handy -- you could define a shortcut that would open a new Firefox window, and Firefox wouldn't even have to be the active app.

Unfortunately, this doesn't work. Everything goes well until the last step: you can activate the script menu, place the script in there (which creates a persistent menu item), define a keyboard shortcut in System Preferences, and when you logout and log back in, the keyboard shortcut appears next to the menu item in the Script Menu.

But pressing the keyboard shortcut simply doesn't work. It doesn't produce a beep, it doesn't flash the Script Menu's icon, it doesn't produce an error in the Console. It just silently fails.

OK, fine, so where else can I get a persistent menu item for which the Keyboard and Mouse shortcuts would be able to define a shortcut, and which is accessible from all applications? Why, the Services menu, of course!

I had previously heard of ThisService that creates system-wide-accessible services (with menu items) from any script that you create. So that seemed reasonable. Just put the script into a service, assign the appropriate menu item, create and install the service, and then voilà, you have a keyboard shortcut. I realize this seems to be going against the "no-third-party-utilities" rule that I outlined above, but not really. The service will simply be running a script that I wrote myself, and once I create the service, I can toss out the third-party software that made it.

Again, all seemed to be going well until I tried to use the service. The problem is, services either act on input, produce output, or both. That means that there are restrictions on when services are available. If nothing is selected, then there is no input, and services that require input are grayed out. If your cursor is in a place that is not editable, then the service cannot produce output, and so services that produce output are grayed out in these situations. And if the service both acts on input and produces output, then it's subject to both restrictions.

So what this means is that whatever kind of service you make with ThisService, there will always be times when the service is grayed out, and you won't be able to simply create a new Firefox window.

OK, so services are out the door. There's one other place that's accessible by all applications, and that's the Apple menu. But there's only one place where you're able to modify without mucking around with system resources and potentially hosing the Apple menu: the recent items menu.

I figured since web browsing is something one might do all the time, a script that opens a new window and keeps another one minimized might be used often enough that it's persistent in the recent items menu. You could even increase the number of recent items to 50 to make almost absolutely sure that this is so. Not exactly pretty, but it's clean in the sense that you're not hitting a fly with a sledgehammer, and you're not hacking at system resources manually.

But get this! Recent items have the same problem as the Script Menu: keyboard shortcuts can be defined and they'll appear along with the menu item, but they just don't work. ... the hell?

I also considered multi-stroke key bindings, but that fizzled out 'cause there doesn't seem to be an action you can use that will help with opening apps or launching scripts.


What's weird about this whole saga is that this seems to be an amusing hole in Mac OS X's built-in functionality. If you go to the Mouse tab of the Keyboard and Mouse system preference pane, you can have buttons launch any application you want. That's good enough -- you can save any AppleScript as an application.

Similarly, if you go to the CDs and DVDs preference pane, you can launch specific applications when you insert certain kinds of CDs or DVDs. There's even a dedicated menu item to launch custom scripts, even if they're not applications.

So what's the deal? Why can't you have certain keyboard shortcuts launch applications or scripts?


There is a way to do it. But it's clumsy, and depending on the person, it might completely defeat the purpose of setting up the "keyboard shortcuts" in the first place. You'll see why I put "keyboard shortcuts" in quotes in a sec (I'll call them "extended keyboard shortcuts"). Here's the process:

  1. In the Finder, open the Applications folder, and then open the AppleScript folder.
  2. Launch the "AppleScript Utility" application.
  3. Check the "Show Script Menu in menu bar" box.
  4. If the Script Menu icon is not all the way to the left in the right-hand portion of the menu bar, hold down the command-key, click and drag to bring it as far left as possible, and let go of the mouse. (You may not be able to get it all the way to the left; that's OK. Just get it as far left as possible.)
  5. Quit AppleScript Utility.
  6. Go to your home folder.
  7. Open up the Library folder.
  8. Open the folder named "Scripts". If there isn't a folder named "Scripts" inside your Library folder, create one.
  9. Drop any scripts or apps or documents that you want to be accessible via extended keyboard shortcuts into this Scripts folder.
  10. Open System Preferences from the Apple menu.
  11. Go to the Keyboard and Mouse preference pane.
  12. Go to the "Keyboard Shortcuts" tab.
  13. Click the disclosure triangle next to "Keyboard Navigation" in the list of shortcuts.
  14. Check the "Move focus to status menus in the menu bar" box.
  15. If desired, double-click on the keyboard shortcut and press a new keyboard shortcut to change it. The default is Control-F8. This will only be the initial keyboard shortcut in the extended keyboard shortcut.
  16. Quit System Preferences.

Now, to launch your application or script or document, you just need to press Control-F8, type the first few letters of the item, and press return. Depending on how many items you have in your Script Menu, this may be as few as 4 key presses. But it's such a clumsy workaround to something that should clearly be a built-in feature. Anyone know if this is coming in Leopard or not?

(As a side note, do you "use", "press", "click", or "type" a keyboard shortcut. I never really know what verb to use.)

Thus, in summary: keyboard shortcuts can be defined but cannot be used for the Apple menu or menu extras (CXIV, filed: 5496790). Also, keyboard shortcuts cannot be defined to open a specific application or run a specific script (CXV, filed: 5496802).


Technological Supernova   Apple Bug Friday   Older   Newer   Post a Comment