Jump to content

Workflow hotkey toggle


Recommended Posts

Is there a way to setup a toggle with a hotkey to flip between these two Apple Shortcut actions? I'm struggling to create the logic flow for this.

 

Goal

1. Press the hotkey, and the app is muted.

2. Press the hotkey again, and the app is unmuted.

 

CleanShot2024-05-30at19_16.46@2x.thumb.png.d32e486fe44d9c630279df6e4d59702a.png

Link to comment

I'm sure someone will have a better idea but (assuming the mute command itself in the shortcut is not a toggle) the only simple way I can think of doing it at the moment is to have a text file in the workflow folder which records the last action (i.e., mute or unmute). If you then read that file when running the shortcut you can:

  • then run the opposite shorcut; and
  • write again to the file recording the latest operation.

Stephen

Link to comment

The other option is to have an action which determines the current state (muted or unmuted) at call time and outputs that information. But it’s unclear if that’s feasible, as screenshots don’t provide any information about your specific case. We have no idea what the Arg and Vars is for or what those shortcuts do internally.

Link to comment

Unfortunately, getting the current state of the application's mute status isn't available. Only setting explicit mute values "Mute" or "Unmute" are available. That's why I have two shortcuts, one for mute and one for unmute. It's not my ideal either. If I could set this up in shortcuts, I would do that and then just invoke the one shortcut with a hotkey.

Is writing and reading a text file the only way to make a toggle in Alfred? I'm surprised.

FWIW, I'm working with SoundSource shortcuts. Setting mute or unmute for an application is possible, but getting mute status for an application is not.
https://www.rogueamoeba.com/support/manuals/soundsource/?page=shortcuts

Link to comment

You are welcome!

 

1 hour ago, BigLaser said:

Why is this an apple script contained in a zsh script instead of just an apple script?

 

This is just to more conveniently access the environment variable '${alfred_workflow_bundleid}' and the argument.

Link to comment

Purely for reference, here’s the AppleScript version:

 

on run argv
  set newState to item 1 of argv
  set workflowBID to system attribute "alfred_workflow_bundleid"
  
  tell application id "com.runningwithcrayons.Alfred"
    set configuration "state" to value newState in workflow workflowBID
    reload workflow workflowBID
  end tell
end run

 

Link to comment
13 hours ago, zeitlings said:

You are welcome!

 

 

This is just to more conveniently access the environment variable '${alfred_workflow_bundleid}' and the argument.

Interesting, thanks for the clarification.

Link to comment
  • 3 weeks later...
Posted (edited)
On 6/1/2024 at 10:21 AM, zeitlings said:

You can just manipulate an environment variable to switch through different states.

https://transfer.archivete.am/E1GxG/Demo | Conditional Switch.alfredworkflow

 

So I challenged myself to write a JXA script to do the same thing as the bash script contained in the workflow from @zeitlings, and the Apple Script version that @vitor graciously provided. Thanks again, fellas. The JXA code works great. It is provided below.

 

However, the last line "return newState;" doesn't actually output anything from the Run Script object. If I plug it into a Large Type object, nothing is displayed. I'm stumped...?

 

#!/usr/bin/env osascript -l JavaScript

function run(argv) {
	const workflowBID = $.NSProcessInfo.processInfo.environment.objectForKey("alfred_workflow_bundleid").js
	var newState = argv[0];

	var Alfred = Application('com.runningwithcrayons.Alfred');
	Alfred.setConfiguration("state", {toValue: newState, inWorkflow: workflowBID});
	Alfred.reloadWorkflow(workflowBID);

	// Ensure to return the newState
	return newState;
}

 

Edited by BigLaser
Link to comment

Of course after posting my question I've discovered something. Commenting out the "Alfred.reloadWorkflow(workflowBID);" line allows newState to return as I expect.

 

I'm still stumped at this behavior, though. Does reloading the workflow clear the variables in the script?

Link to comment
3 hours ago, BigLaser said:

I'm still stumped at this behavior, though. Does reloading the workflow clear the variables in the script?

 

It doesn't clear the variables, but rather stops the script. The variable is never returned or considered, because the workflow is "reset". 

I don't know what actually happens under the hood, but the resulting behaviour is that whatever comes after the reload won't be evaluated. 

Link to comment
Posted (edited)

Thanks fellas. I have a few more follow up questions, if that's alright. If there is documentation about these behaviors, please let me know.

 

  1. First principles question - Is it necessary to reload the workflow in this script? The desired behavior from the workflow seems to function fine without the reload.
    1. When is it necessary to reload workflows?
  2. This may be a moot question since everything seems fine, but now I am curious. Is there a way to reload the workflow and return the workflow environment variable in the same script object? I want to return the variable to pass to another object. What's the optimal approach?
    1. My first thought is to put the reloadWorkflow call in another script object and chain that to run after I've returned everything from the first script. Fine approach? Could that leave the initial script or workflow in a bad state somehow?
Edited by BigLaser
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...