Best Practices for GUI Scripting

Wow! I could never remember all of those shortcuts. Of course you do have the FS menu.

I use Default Folder X and the Finder Favorites panel for accessing my frequent folders. LaunchBar also works great for this, since I only have to type a few characters in the folder name to select it.

Chris, I know you prefer FastScripts over Keyboard Maestro for triggering a lot of your scripts, but I prefer KM. For some time I have been using KM to assign almost all of my custom shortcuts/hotkeys for all apps, so that I have all of these in one place: KM. I don’t even use the macOS Preferences any more. Having them in KM makes it easy to have all of my Macs using the same shortcuts.

While there is a small performance hit for executing a script from KM, I have not noticed any material delays or differences for my scripts. All seem to run very fast to me. But then I’m using a very fast Mac. I do generally set the KM Action to execute a script file, rather than text.

I also have lots of scripts and macros with keyboard shortcuts assigned (mostly in Keyboard Maestro or an older launcher called Butler, which is not so well-known but which I love for its ability to create custom pop-up menus). A few years ago I started running into the problem of remembering all the shortcuts I had created, so I gave some thought to systematizing them. What I came up with was a list of “objects” such as files, calendar events, contacts, OmniFocus tasks, etc. Most of these “live” mainly, but not necessarily exclusively, in one given application. For most of them there’s a fairly consistent set of tasks that I might want to do with them: open, make new, find, make with selection, find with selection, plus any number of more specific tasks for each object or application.

So, each “object” type is associated with a letter or other key, for example:

  • E = (calendar) event
  • M = e-mail message
  • N = note, i.e. plain-text file
  • U = contact ('uman being)
  • S = script
  • 2 (for “2”-do) = OmniFocus task
  • … etc.

Each type of action I associate with one or more modifier keys:
⌃ plus the letter opens the main application I use for the given object type, e.g.:

  • ⌃E launches/activates Fantastical

  • ⌃M launches Mail.app

  • … etc.

  • ⌃⌥ plus the letter creates a new object, e.g. ⌃⌥M launches Mail and creates a new message, etc.

  • ⇧⌃⌥ plus the letter creates a new object using the current text, image, or file selection (like using the Services menu — in fact a lot of these just call the corresponding Services menu item)

  • ⌃⌘ plus the letter goes to the search field or nearest equivalent in the application for the given object type

  • ⇧⌃⌘ plus the letter copies the current selection, goes to the search field for the object type, and pastes (or uses an equivalent existing Services menu item if one is available)

  • ⇧⌃ plus the letter pops up a menu of actions for the given object type

This leaves ⌃⌥⌘, ⇧⌃⌘, and the infamous finger-tangling ⇧⌃⌥⌘ (as well as combinations that don’t include ⌃ and are thus more likely to clash with built-in shortcuts) for other scripts or macros that don’t fit into this schema.

I prefer to use LaunchBar for launching scripts:

With LaunchBar I don’t need shortcuts. The only thing that LaunchBar understands are Abbreviations. Abbreviations are sequences of letters like MP, MH, FLT, LMGS, and so on.

Compared to keyboard shortcuts, abbreviations have a significant advantage:

You have a huge namespace at your disposal: 26^3 combinations if you limit yourself to three-letter abbreviations, and still 676 two-letter abbreviations. This makes it possible to have really mnemonic abbreviations. With traditional [⌃⌥⇧⌘][a-z]-style shortcuts mnemonics are —at least— difficult.

To make the best use out of it you want to assign a key tap to invoke LB and another key tap to invoke Instant Send to LB. For example I have assigned Single-Shift to invoke LB and Single-Control to invoke Instant Send.

(LB’s default keys are ⌘-Space for LB, and ⌘-Space-hold-down for Instant Send; not so good.)

An example:

Let’s say I have a script or an LB Action called “File Lock Toggle”:

So, to lock/unlock a file I select a file in the Finder, I tap the Control key followed by an F and an L keystroke and Return. Done. Four keystrokes, easy to remember, and I don’t have to hold down any keys simultaneously.

If you do this with traditional shortcuts (via Services, FastScripts or Keyboard Maestro) you likely will have at least two modifier keys plus a letter key, something like ⌃⌘L. Admitted, this is one key less for the shortcut method, but mnemonics are more difficult. (Not so much because of the L but because of the ⌃⌘.). And, with the shortcut method, you have to hold down two keys simultaneously while pressing the letter key, which is a pain if you are not a good touch typist.

In addition: the more scripts you have the more modifier keys you’ll likely have to apply.

I also like running scripts via LaunchBar because…

  • it doesn’t matter where the script is: It can be wrapped into a LB Action, or it can be a standalone script, anywhere on your disk (as long as the directory is indexed by LB, of course).

    • FastScripts (or Apple’s Scripts menulet) forces you to drag an alias or a link into ~/Library/Scripts/
  • I don’t have to worry about the right abbreviation: By default LB generates the abbreviation from the “initials” of the script name (like FL or FLT from “File Lock Toggle” in the above example). If this is not OK, I can always force-apply an arbitrary abbreviation to a script, or I can train LB to understand my preferred abbreviation.

  • If I’ve forgot the mnemonics of my script, I can always type something like “lock” (referring to the example above) and it will still list my script amongst the top ten hits or so.

– Tom

1 Like

Thanks for sharing all that, Tom.
I’ve got to re-look at how I run scripts. I have not been using LB, but I certainly can see the utility.

I’m trying to script making Google Chrome full screen. I had a version using keystrokes, but some users have changed their preferences so those don’t work all the time. Plus, it’s a toggle, so if it’s already full screen, the keystroke will turn that off, so you have to go to the menu anyway (although, I suppose you could get the window size.)

The easy part is going to full screen mode, because the menu item’s name changes to “Exit Full Screen Mode” so it’s not a toggle.

The tricky part is getting the “Always Show Toolbar in Full Screen” right. If there’s a √ at the beginning of the menu item name then it’s on, and I want to turn it off. So I need a way to determine if it’s on or off. Enabled doesn’t work. Not sure what does. (Once this is solved I’ll need to also turn off “Always Show Bookmarks Bar in Full Screen.”

Any suggestions?

tell application "Google Chrome" to activate
delay 1
tell application "System Events"
	tell its application process "Google Chrome"
		tell its menu bar 1
			tell its menu "View"
				tell its menu item "Always Show Toolbar in Full Screen"
					set toolBarShowing to properties
				end tell
			end tell
			if not toolBarShowing then
				click menu item "Always Show Toolbar in Full Screen"
			end if
			click menu item "Enter Full Screen"
		end tell
	end tell
end tell

Tom,

This post has me incredibly intrigued about LaunchBar. To be honest, I have wanted to check it out for years, but I am a committed Alfred user and didn’t see the need for a separate launcher app.

However…

To have a dedicated app to launch scripts, ESPECIALLY in the way that you described by just tapping a few keys (i.e. without developing a massive keyboard shortcut hierarchy) is very interesting. I also appreciated your personal use of the shift and control keys to invoke LB and instant-send. THAT sounds awesome.

I am digging in deep to learn about LaunchBar and I appreciate your contributing your perspective!

Thanks Tom

(Once I get used to LaunchBar, I will probably be hitting you up soon for some more tips and best practices! Thanks)

Just to make things fun, I just discovered that you can search and run Keyboard Maestro actions FROM LAUNCHBAR! This really makes LaunchBar the top choice for activating scripts! I an use Keyboard Maestro or just script files and access them all from LaunchBar! Thanks @Tom for the tip!

The Lauchbar Action that makes this possible is found here:

Hi Bodie,

with the current LaunchBar version this works also out of the box (without mlinzner’s action).

Just type “KM” (or whatever abbreviation LB has learned for Keyboard Maestro), when Keyboard Maestro is selected hit the spacebar. Then you can further forward type to the desired macro.

If you are looking for more tips on LaunchBar have a look at the KM forum, especially this thread, and others.

I would agree that LB offers a lot, and I use it for a lot.

I find this KM Macro to be more useful for triggering KM Macros, since it shows only those Macros that are currently activated:
Execute Macro by Name (Spotlight)

I find that LB is great if you know the name of the item, or at least some characters in the name. But when I don’t remember, LB is not of much help to me.

So, to trigger macros/scripts based on the FrontMost app, I prefer:

The really great thing here is that we have so many great tools to choose from, and each of us can pick the tool we prefer.

Just to mention it: if you have opened the list of KM macros in LB (as described above) you can also scroll thru the entire list with the mouse wheel or the arrow keys. It’s sorted alphabetically. (Only active macros are listed though.)

1 Like