Jump to content

Run a script whenever Alfred closes


Recommended Posts

Does anyone know if there's a way to have Alfred run a single specific script every time the Alfred window closes? So whenever a workflow finishes, or a file action is performed, or even just when Alfred has been cancelled – I want to send a little command, externally.

 

The reason for this is I'm using Karabiner Elements to put my keyboard into a special "Alfred mode" whenever Alfred is invoked. I'm hoping to find a simple way to get out of this mode. At the moment I have these specific rules in Karabiner to try catch any keyboard / mouse event that could close the Alfred window, but it's getting out of hand. It would be much simpler if Alfred (or the system) could execute a script whenever the Alfred window closes.

 

Does anyone have any thoughts?

Link to comment
6 hours ago, Floating.Point said:

Does anyone have any thoughts?

 

@Mr Pennyworth might have some ideas: he's written a couple of applications that need to know what Alfred's doing. You might be able to use the notification API Andrew added for him, which would be absolutely ideal.

 

The only thing I can think of is an AppleScript that launches Alfred, then waits for it to close:

 

-- Do stuff here to enter "Alfred mode"

-- Open Alfred
tell application id "com.runningwithcrayons.Alfred" to search ""
tell application "System Events"
	tell application process "Alfred"
		-- Wait for Alfred window to appear
		repeat while (count windows) = 0
			delay 0.1
		end repeat
		log "Alfred open"
		
		-- Wait for Alfred window to disappear
		repeat while (count windows) > 0
			delay 0.1
		end repeat
		-- Exit "Alfred mode" here
		log "Alfred closed"
	end tell
end tell

 

You could do your setup and teardown in the script or before it runs and after it exits.

Edited by deanishe
Link to comment

This is fantastic! I can't thank you enough.

 

I needed to adjust this a bit, to make it suitable for my needs. I needed to trigger a workflow's external trigger (instead of just launching Alfred). So building on what I learned from yourself and Vitor about sending variables to AppleScript using ARGV, I was able to save this AppleScript as an executable, then call it from a shell script, feed variables into the trigger, workflow and argument fields… and I kind of knew what I was doing throughout this whole process.

 

Now I have a lovely reusable little script where I can launch Alfred into a specific workflow, and have my system track whether I am in Alfred mode or not.

 

Thank you so much for your really valuable help!

Link to comment

 

21 hours ago, deanishe said:

What's your "Alfred mode"?

 

Im using Alfred to display a bunch of context sensitive modal menus. My Alfred mode remaps keypad numbers to include the command modifier, so I can execute Alfred list items with a single keystroke. I also temporarily remap some close-by keys to page down and page up so I can quickly move through any longer Alfred modals

Edited by Floating.Point
Link to comment
49 minutes ago, Floating.Point said:

My Alfred mode remaps keypad numbers to include the command modifier, so I can execute Alfred list items with a single keystroke.

 

That's a clever idea. You might consider sharing your scripts and Karabiner config on the forum or GitHub (as a gist, perhaps). I'm sure some people will find that really useful.

Link to comment
  • 2 weeks later...

Okay so I posted a walkthrough for getting this set up here.

I also found Andrews post about Alfred's 'Press Secretary' – which as mentioned could be a cleaner way to do all this.

But then I also submitted a feature request to allow numeric keypad numbers to optionally bypass the requisite modifier – because I think these quick modals really could be useful to a broader audience.

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...