Using Alfred to Open Sets of Folders Quickly through Javascript

Using Alfred to Open Sets of Folders Quickly through Javascript

Finder Lamentations

I’ve used/owned Macs since the early 1990s, but used Windows PCs for my serious work until 2013. Last week I discovered Spotlight could quickly open and find files for me without having to dig around in the Finder. Since I don’t like the Finder at all, I was very excited by this and marveled that it had taken me so long to discover it. Sharing this sentiment on twitter, I got a couple interesting responses regarding Alfred, a supercharged version of Spotlight:


I had heard of Alfred before, but had thought it was somehow related to Ask Jeeves, a search engine from the 1990s that did not impress me. I am easily confused by butler-themed software branding, apparently.

But I digress. In short: Alfred is great. It’s has a lot more features than Spotlight, and I’m only just starting to dig into them. For now, I’m using it to quickly open applications from the keyboard, which has allowed me to declutter my MacOS Dock so only chat apps (Slack, Discord, etc) are there. Previously, my Dock was loaded with the entire Adobe Creative Suite, a dozen development-related programs, and several audio production tools as well. They are now ALL GONE…so much cleaner!

I wondered if I could use Alfred’s Workflows to automate opening my essential folders. Usually I have to dig through Finder windows (annoying) or use use Default Folder X to jump to it. The problem with using Default Folder X is that it still opens a Finder window (UGH). Could I use Alfred as a better folder shortcut utility with a bit of custom coding?

GOAL: Add a ‘GET’ Command to the Alfred Hotbar

It seemed that it should be possible to capture the keyword and an argument from the Alfred Hotbar, and so it is with Alfred’s Workflows. This is part of the paid upgrade from the free version, which I did. The WORKFLOW tab can be found inside Alfred Preferences.


Alfred 4 Workflow Layout Screen

LAYOUT SCREEN

To add a new workflow, I clicked the + sign at the bottom of the left-side panel. In the screenshot (right), there’s a single workflow item called “Sri Utils” (the name is arbitrary).

There is a lot of verbose documentation on using Alfred, and I did not find most of it very helpful. The official docs bury the details in happy boilerplate so you can’t find anything, and most third-party tutorials repeat only fragments of information like “magic spells” to be recited without conveying understanding. What I like are key concepts, succinctly expressed: all the bullshit on the Internet boiled down to this:

  1. To add a command to the Alfred hotkey bar, add an input node of type keyword
  2. To receive the keyword arguments in a script, add an action node of type run script
  3. Your script will receive a ‘query’ variable that corresponds to the arguments from the input node.

NOTE: The layout shows I have two keywords that feed into the same script action node. One for the “get” keyword and the other is for “cd”, because I keep typing that out of habit. There is also a final hide Alfred utility node to make the Hotbar disappear when everything has finished running.

NOTE: It’s possible to open sets of files and folders WITHOUT SCRIPTING, using the available input | action | utility | output nodes to do the same thing. Still, I wanted to write a script because I thought it would be more flexible for my needs in the long run,


Input node setup

1. INPUT KEYWORD NODE(S)

Here’s the input node as I have defined it. The argument is what will be passed to the next node in the chain (my script).


Action node setup

2. ACTION SCRIPT NODE

Here’s the action node as I’ve defined it. Note the settings at the top of the dialog box designating the type of script:

  • You’ll see that I selected the Javascript version of OS Automation (JXA) instead of Applescript, which I despise even more than the Finder. My joy may be short-lived, though, as Apple reportedly is abandoning script automation in general. The documentation for Script Editor is very poor as well. Good luck learning anything from it.
  • I also chose input as argv instead of as {query}, because I don’t like magical macro injections in my source code. The Alfred runtime environment and variables isn’t described succinctly in any place I could find, and I had no idea what a “query” was anyway. Would it be an array? A string including the initial keyword plus arguments? Something ELSE? Now I know it’s an array. Found this by flipping through the input dropdown and seeing the boilerplate.


3. SCRIPT

This is the script I wrote. Hopefully the comments explain what is going on clearly. After installing it, I can run it by popping up the Alfred Bar with the hotkey (default: option-spacebar) and then typing ‘cd km’ and everything designated in the ENTRIES.km object property will be opened.

https://gist.github.com/daveseah/8d84050564c861d0f33649741c378ea9


Debugger setup

DEBUGGING

If you have a syntax error in your code, Alfred does not show the error by default. You need to configure the script editing environment by clicking the debug button and also selecting show all information (screenshot right). This will allow you to see any errors as well as any output from console.log(), though it will be erroneously flagged as an ERROR in the Alfred output window. Ignore it.

Instead of using the terrible Alfred script editor, I edited and tested using Visual Studio Code’s AppleScript Extension.

  • This extension will run the JXA source file by typing alt-shift-R. The active editor window must have the JXA source file opened inside it, otherwise the command fails silently.
  • Alternatively, use the VSCode command palette and chose “JXA: Run script”. Again, the active editor window must have the JXA source open.
  • Make sure you open VSCode’s terminal window (CTRL-tilde) to see your debug output. When everything seems to be working, then copy/paste the code into the script textfield.
  • When using the VSCode approach, remember argv isn’t passed to run(), so you have to work around that by assigning test values to argv.
  • The version of Javascript it runs appears to be ES6 (at least in High Sierra), but I didn’t test to see how compliant it is.
  • The Applescript Javascript environment is apparently very powerful, as it can access system libraries! It’s theoretically possible to write entire applications in it.

SUMMARY

It works and I can now open my special folders without having to move my hands away from the keyboard. This is wonderful. I’d like to figure out how to itemize the system libraries and see what’s in them, but that’s a pretty big “learn how to code in MacOS” topic and will have to wait for another day.

0 Comments