Jump to content

[SOLVED] Multiple actions for same hotkey based on focus


Recommended Posts

According to the official documentation, it should be possible to assigned the same hotkey to multiple actions based on what app has focus:

 

Top tip: You can use the same hotkey for multiple actions within a single workflow. For example, you can set a hotkey to perform action A only when your browsers are in focus, while performing a different task when Mail is in focus. This can only be done within a single workflow, not across multiple ones.

 

However, I cannot find a way to make this work. Here is what I've tried:

 

Creating multiple copies of the hotkey

I create a hotkey object and add an app to the "related apps" tab. I link the hotkey to an action.

Then I create another hotkey object. When I try to assign the same hotkey to the second hotkey object, it simply won't work. The "Hotkey" field remains blank as long as I'm pressing a key combination that is already in use.

 

Assigning outputs from the hotkey based on app focus

I thought maybe I could set multiple outputs from the hotkey object. I created a hotkey and added two apps to the "related apps" tab. I put two actions into the workflow and connected the output of the hotkey to both of them. The result is that when either of the two apps is in focus, BOTH actions are triggered.

 

Here is my desired behavior:

I have two applescripts that toggle between versions of a web page by modifying the URL. The script is slightly different for Chrome than for Safari, and I want to use the same hotkey to trigger whichever script applies to the current browser. In simpler terms, if I'm in Chrome the hotkey should launch Applescript-A, and if I'm in Safari the same hotkey should launch Applescript-B.

 

Can Alfred do this? The quoted documentation certainly makes it sound possible.

Link to comment

If you chose "Hotkey is only active when the following apps have focus" in the dropdown, then you may have to use applescript to get which app is topmost in a single script ... then, depending on the app have an if then statement ... "if" for Safari and "then" for Chrome.

Link to comment
  • 3 months later...

Does this functionality require that the hotkeys directly trigger applescripts (which scripts also requires some tinkering to cooperate). Am I right to figure this is no solution if I want identical hotkeys to load Alfred's window with variously predefined text?

 

In my case, depending on which app I have focused, I was hoping a single hotkey could load Alfred with certain characters (differentiated by separate instances of the hotkey). This text input would result in an Alfred dropdown menu of keyword-linked applescripts (already defined in workflows setup for each given application).

 

I already use different text-triggering hotkeys to call on identically key-worded applescripts in various workflows. I was hoping I could just use one hotkey to enter different workflows depending on the app in focus and thereby cut down on friction.

 

So, with that aim in mind I deleted all the hotkeys from their respective workflows and redefined them in a separate workflow (named "Hotkeys") as the official documentation seemed to indicate I must.  Separating text-triggering hotkeys from the workflows containing their associated keyword-launched applescripts does not interfere with functionality.

 

Within the "Hotkeys" workflow I began recreating the hotkeys one by one, first defining their distinct text output and then targeting different "related apps" for each hotkey. Still, I am prevented from defining any two with the same key combo.

 

Why won't this work?

Edited by imurban
Link to comment

It's not entirely clear to me what you're trying to accomplish but two ideas that may be helpful are

 

1) use Dean's AppScripts workflow - http://www.packal.org/workflow/appscripts - which can be triggered by a single hotkey (add it and link it to the Script Filter) and opens Alfred with a list of whatever Applescripts are in the Scripts folder for the active app.  There's Applescript in the source (on github) which can also detect the active application.

 

2) call Alfred via applescript:

tell application "Alfred 2" to search "whatever "
Link to comment

@dfay

 

Thanks for your response. Looking over your two recommended approaches, I don't think I want to change any setup of my applescripts in pursuit of this hotkey behavior.

 

To clarify, I'm after the sort of functionality described in the "Top tip" called out in yellow on this documentation page: http://support.alfredapp.com/workflows:config:triggers-hotkey

 

I've figured out a lot in Alfred, but the hotkey behavior that doc page describes has me really confused. Can anyone attest to having setup two identical hotkeys with "Action: Show Alfred / Argument: Text / Text: {specific to related app(s)}"?

 

If not, can anyone demonstrate how identical hotkeys with different contextual output work out of the box in Alfred?

Edited by imurban
Link to comment
  • 4 weeks later...

Alfred won't let you assign the same shortcut to multiple Hotkey triggers, even if their active apps are different.

 

This is an unfortunate limitation that can make using the feature much harder.

 

You have to connect the Hotkey trigger to an AppleScript that itself figures out which app is active and then takes the appropriate action.

Link to comment

Thank you, deanishe. I'm glad to know I'm not missing something about the core functionality.

 

Would you (or anyone) happen to have an Applescript to share that does what you described? Not knowing how to write applescript myself, I'm still confident I can repurpose such a script to target my applications. I already use a lot of applescripts with Alfred, mostly cobbled from bits an pieces people have shared on these forums.

Link to comment

:D I think that's how we all write AppleScript, except Carlos-Sz, who actually seems to like it.
 
This will grab the name of the frontmost app and show different dialogs (as placeholders):

tell application "System Events"
	set activeApp to name of first application process whose frontmost is true
	log "Active app : " & activeApp
	if activeApp = "Google Chrome" then
		display dialog "Chrome!"
	else if activeApp = "Finder" then
		display dialog "Finder!"
	else
		display dialog "Some other app!"
	end if
end tell

Personally, I'd probably replace the display dialog calls with do shell script calls to get into a proper language like Python or Ruby as soon as possible.

Link to comment

Hey, this helped a lot! I would have gotten nowhere without your instruction to replace the dialog calls, so thanks again.

 

This achieves exactly what I was after – double command keying into various workflows depending on the active app.

Link to comment
  • vitor changed the title to [SOLVED] Multiple actions for same hotkey based on focus

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