Jump to content
wonkeythemonkey

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.

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

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 "

Share this post


Link to post

@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

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

Looking back at your original post, I think what you want to replace the "display dialog…" is:

 

tell application "Alfred 2" to search "your query goes here"

Edited by deanishe

Share this post


Link to post

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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...