Jump to content

(Hotkey) Toggle Apps


Recommended Posts

Posted

This workflow toggles the visibility (Opens foreront or hides it in dock) and/or opens an app by its name if it is not running already.


To add new apps:

Step 1 - Add a "Hotkey" from the triggers menu, configure it to the shortuct you prefer and if it is to run on an specyfic app or globally. 
Step 2 - Add "args and vars" from the utilities menu. Replace {query} in the "argument" text box with the EXACT NAME (including capitals and spacing) of the app you wish to open.
Step 3 - Sequentially connect the modules you created with one another to the script, like in the example.
Step 4 - thank me :)

Screen Shot 2019-03-22 at 12.48.47 PM.png

  • 2 years later...
Posted

I do thank you @rodri. Very useful workflow.

But I am not able to use it efficiently. Sometimes it crashes Alfred, other times it seems unresponsive. Is there maybe v2 of it?
 

@deanishe, @Vero, @Andrew; Guys could you maybe help with this as well? Maybe a better way to write this script or some way to troubleshoot?

  • 5 months later...
Posted

I've update the code a bit so that it first tries to bring the app in front, if it is not focussed.

Some apps have a different title so i had to hack it a bit.

on alfred_script(q)
  try
	if application q is running then
		tell application "System Events"
			set vis to the visible of process q
             -- get the active window name
			set activeApp to name of first application process whose frontmost is true
		end tell
        -- Code active window seems to be Electron, working around that in a hacky way
        -- similar for Teams
        if activeApp is "Electron" then
            display notification "Code workaround"
            set activeApp to "Code"
        else if activeApp is "Teams" then
            display notification "Teams workaround"
            set activeApp to "Microsoft Teams"
        end if
		if vis then
            if activeApp is q then
    			display notification "hiding focused app"
	    		tell app "System Events" to set visible of process q to false
            else
                display notification "focusing app, currently we have " & activeApp
                tell application "System Events"
                    tell process q
                        set frontmost to true
                    end tell
                end tell                
            end if
		else	
			display notification "showing app"
			tell application q to activate
		end if
	else
		display notification "not running"
		tell application q to activate
	end if
  on error
	display notification "ups, got an error. Contact developer."
  end try
  return q
end alfred_script

 

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