Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prev_application companion to the new open_application #4011

Open
eugenesvk opened this issue Nov 12, 2024 · 8 comments
Open

Add prev_application companion to the new open_application #4011

eugenesvk opened this issue Nov 12, 2024 · 8 comments

Comments

@eugenesvk
Copy link

eugenesvk commented Nov 12, 2024

There is now an action to open an app open_application, so it would be great to also have a companion action to "undo" it so that you can toggle the same app with one conditional shortcut.
This prev_application action should the bring the previously focused app into focus

@tekezo
Copy link
Member

tekezo commented Nov 17, 2024

Sending Command+Tab is probably the easiest method. Alternatively, you could control it using a shell_command and open command instead of open_application.
Incorporating this into Karabiner-Elements might result in somewhat inconsistent behavior, I think.

@eugenesvk
Copy link
Author

Command+Tab wouldn't work as it would switch to another window of the same app if the 2nd window is the most recent one, then there is this complication that the appswitcher stays opened until you release the from modifier since you're sending to as modifiers, not a command.

Not sure I understand the open alternative, it doesn't seem to have a "previous" option

@tekezo
Copy link
Member

tekezo commented Nov 21, 2024

Not sure I understand the open alternative, it doesn't seem to have a "previous" option

This meant that you would manage the history with a shell script while using the open command to switch to the appropriate application.
It seems that achieving the desired behavior will require meticulous and proper programming.

@eugenesvk
Copy link
Author

I don't see why I'd need to do the error-prone keeping of history:

All I need is to "deactivate" the current app, so change its "Z" order from top to top-1
Or if there is no such "deactivate" api, then I'd just need to activate the "next" app in the Z-list
Neither of these two operations requires even knowing what the top/next apps are, just have access to the listof apps

Karabiner-Elements already has some of that info, it's used in the frontmost app condition?

@tekezo
Copy link
Member

tekezo commented Nov 24, 2024

I see, that's interesting. I had thought the open_application history alone wouldn't be enough, but using the frontmost_application history does seem like it could work. I'll give it a try and see how it works.

@eugenesvk
Copy link
Author

The history might not be enough since there is a chance something else will close an app present in history (or maybe if it's launched hidden it gets to the list of apps, but not to your history of apps that have been at the front).

But from some brief searching it seems macOS is rather deficient here: there is no simple way to get a list of apps in order, e.g., this seems to work, but only within the current space

https://stackoverflow.com/questions/46946860/macos-get-running-apps-ordered-by-most-recently-used-first

So this API would mostly be useful at startup when KE has not seen any history yet, but later on it seems that tracking history which KE already does is indeed the only approach (and given that KE is usually launched at startup, that's a very minor issue)

@tekezo
Copy link
Member

tekezo commented Dec 3, 2024

I added history_index option to open_application in the latest beta version.
I believe it's implemented quite nicely, so feel free to try it out.

Note: Applications opened before Karabiner-Elements started are omitted.

https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to/software_function/open_application/

@eugenesvk
Copy link
Author

After some testing: it seems to work with just one bug - when you hit the same shortcut twice quickly, you don't end up at the app you've started with (though it works in Keyboard Maestro), guess either the list is not updated quick enough or maybe the second key doesn't wait till the list is updated or something else

And maybe the following could be a more useful example for the new command's docs: toggling your file manager with the same key d (though the part of assigning 4 modifiers to isn't shown here and could be linked to another part of the docs)

{"description" : "Toggle an app with a single keybind ✦d: focus (open if no app running) / unfocus (switch to the previously opened app) (✦key=⇧⎈⎇⌘, can be assigned to ⇪)",
  "manipulators" : [
  {"from":{"key_code":"d","modifiers":{"mandatory":["left_command","left_control","left_option","left_shift"]}},
    "to":[{"software_function":{"open_application":{"bundle_identifier":"com.apple.finder"}}}],
    "conditions":[{"type":"frontmost_application_unless","bundle_identifiers":["^com\\.apple\\.finder$"]}],
    "type":"basic"}
  ,
  {"from":{"key_code":"d","modifiers":{"mandatory":["left_command","left_control","left_option","left_shift"]}},
    "to":[{"software_function":{"open_application":{"history_index":1}}}],
    "conditions":[{"type":"frontmost_application_if","bundle_identifiers":["^com\\.apple\\.finder$"]}],
    "type":"basic"}
  ]
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants