Jump to content

How to make the Alfred search window a frontmost app


Recommended Posts



I use app-specific key remapptings in Karabiner-Elements, Hammerspoon and BetterTouchtool. Those mappings become effective when a relevant app becomes a frontmost app. The problem is that when I open the Alfred search window, it is not recognized as a frontmost app. Is there any way to make Alfred a frontmost app?

Link to comment
  • 2 months later...

I've found I can send Alfred characters if I follow the shell command that opens Alfred with 3 left_control's. More than 2 left-control's are required to achieve the necessary delay. The delay is apparently required so that the focus can change to the newly opened app. The delay is also required when using Alfred's compatibility mode, which seems to have no effect one way or the other.

Link to comment
12 hours ago, deanishe said:


It won't have any effect if the actual problem is that the key events are being sent before the app that's supposed to receive them doesn't have focus yet.



Yeah. Also, my trick to cause a delay doesn't work at all on my other Macbook.


The OP indicates Karabiner-Elements, which is what I'm using, so I'd appreciate any troubleshooting suggestions.

Link to comment
On 12/1/2017 at 6:32 PM, JimmyTheSaint said:

I can't get this to work. I set the option to Compatibility. Then I have Karabiner-Elements open Alfred 3.app, and the Alfred window opens normally. Karabiner then sends some key codes, but the keys all go to the app that last had the focus before Alfred opened. Any suggestions?


What Keycodes are you trying to send? 

If you wanna trigger a workflow then i would do it directly via an external trigger.

Other than that you can also open Alfred with a string or so:


tell application "Alfred 3" to search "{query}"


Link to comment
1 hour ago, JimmyTheSaint said:

I haven't studied up on external triggers yet.


You should: that is the proper way to do what you're trying to achieve.


Simulating typing is a real last-resort kind of automation. It is, as you've noticed, extremely unreliable.


Use an External Trigger: it will Just Work.

Link to comment

Quick newbie question with workflows. I add an external trigger to Rui Xue's Search Safari History workflow, then I have a script call that external trigger like so:


tell application "Alfred 3" to run trigger "hsi-ext" in workflow "com.my.alfred.hsi"

It opens the Safari history, so I can do the same thing with Safari Tabs. The problem is that after Alfred opens and I select a history item (or not), the applet stays around. I tried adding this to it:

tell application "SearchSafariHistory" to if it is running then quit

but then Alfred never opens. How do you get the applet to exit once the Alfred window opens but also leave the Alfred window open? I guess it's a newbie question with scripts.

Link to comment

Because I want to run Alfred workflows via Karabiner-Elements rather than activate the Alfred window. I can then launch any workflow with two keystrokes. Also, by putting the applet's path in Karabiner's json file and associating it with a specific key combination, I can do Alfred things while flying blind, i.e., I can enter a key combo in the middle of a blitz of fast typing with 100% certainty as to what Alfred is going to do with it.

Link to comment

You can call your workflows via external triggers. Just wrap calling the external triggers with applescript inside a Karabiner variable and call those. Here is how I do it in Karabiner.


If you figure out how to do it in Elements, do share. I don't want to stay on El Captain forever. All my workflows have external triggers on them, if you are into that. ? 

Edited by nikivi
Link to comment

Because I'm inexperienced and this was the first time I ever worked with Apple scripts. Once the script was written, the only way I could figure out how to launch it was to save it as an application. The Script Editor only offers three possibilities for saving a file. When I launch a .scpt file or .scptd file, it opens the Script Editor. Only when I save script as .app can I launch it and have it do what I expect.


Link to comment

To run an .scpt file:

/usr/bin/osascript /path/to/applescript.scpt

or to run AppleScript:

/usr/bin/osascript -e 'tell application "Alfred 3" to ...'


This seems to be a textbook case of the XY problem. Ask about your problem ("I want to launch a Script Filter from Karabiner Elements"), not your solution ("I want to simulate keypresses in Alfred").


If you're inexperienced, chances are good that your solution isn't a good one, and you'll waste a lot of time and effort barking up the wrong tree.

Edited by deanishe
Link to comment

Yup, that works quite nicely, thanks.


Here's something OT, but that I've been working on. I've developed a rather extensive KE-based launcher over the last couple of days, but I'll ask this in the n00b way rather than risk falling into the XY problem by describing the partial solutions I've been able to get up to now: in KE, is there a way to execute shell_commands by longpressing a key? For example, I want to open Safari by pressing and holding the 's' key. Safari would launch while the 's' key is down, and there would be no other side effect even when the 's' key is released. If you have suggestions, but prefer to put this in a more appropriate forum, please let me know.

Link to comment

That's more of a question to Karabiner. I would love to experiment with Karabiner Elements myself but I only have one mac and I can't afford to lose my keybindings setup until I am certain I can do everything I can do now in Elements. 


Have you tried out the Karabiner launcher though? It is much better and more robust than what you are trying to do. I mean its a lot better to make a key into a modifier key than have this short-long press interaction.


Also this looks really cool. I am thinking of making my own generator, specifically for my sticky keys. So I can super fast define variables and then attach them to appropriate modifiers with as much as few lines of code. That Karabiner Elements JSON is super ugly looking.

Edited by nikivi
Link to comment
10 minutes ago, nikivi said:

That's more of a question to Karabiner. I would love to experiment with Karabiner Elements myself but I only have one mac and I can't afford to lose my keybindings setup until I am certain I can do everything I can do now in Elements. 

No doubt. But I've asked about this in a couple of relevant places and have gotten no response up to now, so I thought I'd try here in case anyone's in the mood to throw me a bone.


Indeed, I've now got my whole K-E launcher set up and working exactly how I like it, with a few different layers. I especially like the spacekey to initiate the launcher mode. You were a big help with all that in the thread that got deleted, which was too bad because I was using it as a reference and it's only partially cached by Google. I posted you a big thank you there that I hope you had time to see. Now that I have my launcher mode done, I wanted to go back and see how the longpress-based interface feels. After all, I believe you mentioned at some point---bragged even---that you could switch to apps with a single keystroke. I want that, but I realize that programming the keys for longpresses will require re-vamping typing technique to ensure proper timing to do normal typing. I might hate that, but I wanted to give it a try. Plus, in my standing-up, public presentation mode that I described in that deleted thread, a longpress interface is more desirable and not prone to typing timing errors since I'm only ever doing app switching in that context.

Link to comment
6 minutes ago, JimmyTheSaint said:

in the thread that got deleted


Oh wow, that's really sad that it got deleted. I spent quite a bit of time writing it and even linked to it from my wiki. Makes you not want to write in this forum, since you never know what will get deleted after all. ?


@JimmyTheSaint Can you link to the Google cache of that post please if you still have it.

Edited by nikivi
Link to comment
27 minutes ago, nikivi said:


Oh wow, that's really sad that it got deleted. I spent quite a bit of time writing it and even linked to it from my wiki. ?


I start to get interested in it too. I have been looking at your Github and have to see how to do the simultanious press. I already use sticky keys but 

i could make way more out of them. I however still try to find a good system that fits and let's me remember everything.

A good way however is having the same hotkey everywhere and use Keyboard Maestro Palettes.

Would be cool if you could post a video where idk you show some ways of a hardcore way of using all functions together.





Link to comment

@nikivi, @JimmyTheSaint Here's Niki's epic post (the thread is hidden, not deleted):


On 22/11/2017 at 4:47 PM, JimmyTheSaint said:

Better = fewer keystrokes. Turbo mode = fewer keystrokes.

If you really care about saving keystrokes, I advise you to look into Karabiner.

I have over 500 or so workflows I use and I can access anyone of them from one keystroke. Just one. 


I nearly never use Alfred default (cmd + space) interface (It is single press of right command or jl keys together for me). Because Alfred has this awesome feature of making super focused searches for anything I wish to search and creating external triggers for them I can then call from Karabiner.


So there is literally no use to have default search aside from opening some applications which I also open from single keystroke in Keyboard Maestro.




The most exciting thing about Karabiner and making every key on the keyboard into a modifier key is that there are literally thousands of keys you can fill and customise. Where accessing any one of these keys (which can map to workflows / Keyboard Maestro macros ..) is literally 1 second away. Compared to (1. opening Alfred 2. searching for trigger).


It's kind of insane if you think about what it really enables you to do. You can even create extra virtual layers of keys with Keyboard Maestro palettes. For example I have space key modifier to accessing different palettes. I can then space (held) + e and quickly enter an emoji insert palette:



So inserting ? is space + e + a and emoji is there. And in case I forget what key ? is. The palette can remind me. So you can do anything you can dream of in two-three keystrokes. Of course emoji palette is just an example, you can run some more complex macros or workflows if you so wish.


Just as an example, because this forum uses some weird markup and is not markdown, I am forced to use @vitor's MarkdownTransform workflow. So I can then just select some text and press t + b and it will convert the selected text into BBCode. Where t is my modifier and b is the action key to press if t is held down (b for BBCode). If I didn't have Karabiner, I would either will need to remember some cryptic hotkey like ⇧⌘⌃+B or something. Because hotkeys are scarce and they will run out quickly. Or open Alfred and start searching for a trigger to do this but that takes too many keystrokes.


Setting @deanishe's Searchio to JK pressed together and J + : pressed together to access currently open Safari tabs with Safari Assist is life changing already. Or LM to search my safari bookmarks and LN to search chrome bookmarks. Or setting s + hjkl to mimic arrow keys (How about vim everywhere?). This list goes on.


It's quite strange but I sometimes check on the internet if anyone actually uses Karabiner too in this way and I never found another person. Would be awesome to see other people's setups and what they made.

Edited by deanishe
Link to comment

Thank you very much @deanishe


@FroZen_XI am planning to make some videos about Karabiner and how I use it together with other things. I was just waiting until Karabiner Elements reaches a point where I can myself transfer to it as there is no point in making the video and then saying in the end 'oh, and this only works on El Captain, sorry'. Hopefully this comes sooner than later.

Edited by nikivi
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...