A Quick Tip For Using Moom in Bunch
05 Oct 2024I was posting a reply to a forum post about Moom (the macOS Window Manager by Many Tricks) this morning and was about to make a reference about using it with Bunch (the automation tool by Brett Terpstra). As I was composing the reply I realised I was using a small modification to the standard set up that I don’t think I’d shared before; and so I figured I’d share it out in a quick post.
Using Moom with Bunch
When you are triggering a bunch from Bunch, you might find you want to arrange the windows of any applications that you launch. Bunch does not include any window management functionality, but on the Bunch website, it does include a page about integrating with Moom.
The primary suggestion on this page is to add a line of AppleScript to instruct Moom to apply a window snapshot, now called layouts in Moom 4.
Example from docs:
* tell application "Moom" to arrange windows according to snapshot "Podcasting" ~10
While Moom 4’s AppleScript seems to be backwards compatible, the newer equivalent should be as follows.
* tell application "Moom" to apply layout "Podcasting" ~10
Don’t Delay
The ~10
at the end of those AppleScript lines above is an instruction to Bunch to wait 10 seconds before executing the AppleScript. This is to give the applications being started by Bunch time to start.
It is simply given as an example and the expectation is you would amend it to a value that works for you. That’s great, except things don’t start in the same amount of time every time. The time it takes for an app to launch fully depends on what state the app is in, potentially network speeds, Mac resources available, relative priority of other running processes, etc. Basically it can take different times to launch. Sometimes substantially different times.
As a result you have to push that delay high enough to minimise the risk of failure because the apps have not launched. This can result in slow arrangements a lot of the time.
At the end of the page on integrating with Moom, there is a reference to “waiting snippets”. Here’s an excerpt on them from the documentation.
A “Waiting Snippet” is a snippet line indented by 4 spaces or 1 tab.
A Waiting Snippet will try to wait until all of the apps in the bunch have launched (or quit, if they’re
!apps
). There’s a timeout in case not all apps properly report their launch/termination to the OS.Skype Audio Hijack <useful.snippets#Position Podcast
This is especially handy for running window management scripts (a la Moom) that need all of the apps to have windows present. It’s more flexible than just putting a hard delay on the script, as it will take into account unusually long (or short) launch times. Just put the script line into a snippet or fragment and call it with an indented line.
That’s great. It gets rid of the dependency on the delay. My advice is to use this approach over the delay as if gives a more reliable option as well as a more efficient one.
But I noted right at the start I’d got a small modification to the standard set up. So what is my little modification you ask.
Make it a Function/Routine
Creating a new fragment each time in a ‘library’ snippet and then calling that by the same name felt like it was a little repetitive and redundant. My modification was to make this a little easier to maintain by setting a variable with the name of the snapshot to launch and then having the snippet use that. This means I have just one snippet to launch snapshots/layouts, and I specify the name of the snapshot just at the time of using it.
Keeping with the Moom documentation example, I would replace the “ <useful.snippets#Position Podcast
” entry with the entries below.
snapshot_name="Position Podcast"
<moom_snapshot.snippet
The content of my moom_snapshot.snippet
file is a single and familiar looking line of AppleScript for Bunch to execute, but I’m using the snapshot_name
variable for the name of the snapshot to apply.
* tell application "Moom" to arrange windows according to snapshot "${snapshot_name}"
For completeness, at some point I expect I’ll update my Moom AppleScript to the version 4 terms, and I’ll start using something like this:
Bunch File
layout="Position Podcast"
<moom_snapshot.snippet
Snippet File
* tell application "Moom" to apply layout "${layout}"
Note: For any developers or Bunch power users, please rest assured that I do realise I can use the hyphenated variable syntax to make the variable local to the snippet, and I will use that approach if I need to apply combinations of snapshots/layouts, but there seemed no benefit to doing this in general for this particular workflow and it makes the syntax a little harder to understand for anyone coming across this for the first time.
Conclusion
So that’s the background and the modification. As I noted, just a quick one today. To me the approach just seems like a logical extension to the information provided in the Bunch documentation, which is where the real work is done.
If you do happen to think of any ways to take this another step further in any way, do reach out and let me know.