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

Make App.launch faster by changing the calling parameter #211

Closed
txchen opened this issue Apr 28, 2018 · 6 comments
Closed

Make App.launch faster by changing the calling parameter #211

txchen opened this issue Apr 28, 2018 · 6 comments
Assignees
Milestone

Comments

@txchen
Copy link
Contributor

txchen commented Apr 28, 2018

  • Version: 2.6.1 (65)
  • macOS: 10.13.4

I am currently using this app to toggle my apps: https://github.com/gbammc/Thor/

It's toggle code is simple: https://github.com/gbammc/Thor/blob/4f86a1768d33f7dbaf61182e2f10179a6166ec6a/Thor/ShortcutMonitor.swift#L17-L23

Now if I want to do the same with phoenix, after I call App.launch(name), I need to make another call to let the app focus, like this:

let app = App.launch('iTerm')
  if (app) {
    app.focus()
  }

Per my feeling (because I have not a good way to measure), toggling with Thor is faster than using Phoenix script. I think it is due to they are calling different API. The difference might come from NSString *appPath = [sharedWorkspace fullPathForApplication:appName]; or the extra app.focus() call.

So, would it be okay to change the launch method (

+ (instancetype) launch:(NSString *)appName {
) to simply calling https://developer.apple.com/documentation/appkit/nsworkspace/1531434-launchapplication, or change the options:NSWorkspaceLaunchWithoutActivation to default, so that app.focus() can be avoided.

Or, maybe add a new method?

@txchen
Copy link
Contributor Author

txchen commented Apr 28, 2018

https://developer.apple.com/documentation/appkit/nsworkspace/1531434-launchapplication returns boolean, so App.launch(name) cannot use this because it will break scripts.

How about add a new API like: App.launchAndShow(appName)
The OC part just call NSWorkspace.shared.launchApplication(appName) and returns boolean.

@kasper
Copy link
Owner

kasper commented Apr 28, 2018

Right! I think we can just add an options object as an additional argument for the App#launch function, i.e. App.launch('Safari', { focus: true }). It would still default to being false without any options.

@txchen
Copy link
Contributor Author

txchen commented Apr 30, 2018

I tried to make this change:

--- a/Phoenix/PHApp.m
+++ b/Phoenix/PHApp.m
@@ -53,7 +53,7 @@ static NSString * const PHAppForceOptionKey = @"force";

     NSError *error;
     NSRunningApplication *app = [sharedWorkspace launchApplicationAtURL:[NSURL fileURLWithPath:appPath]
-                                                                options:NSWorkspaceLaunchWithoutActivation
+                                                                options:NSWorkspaceLaunchDefault
                                                           configuration:@{}
                                                                   error:&error];
     if (error) {

And I can confirm that with this change, opened App can show faster (about same speed of Thor, which uses NSWorkspace.shared.launchApplication(appName))
Although NSWorkspace.shared.launchApplication(appName) looks even cheaper in my use case, I think launchApplicationAtURL with options:NSWorkspaceLaunchDefault is good enough.

@txchen
Copy link
Contributor Author

txchen commented Apr 30, 2018

PR created: #212
Hope new build can be released so that I can reduce one opening app on my laptop, LOL.

@txchen
Copy link
Contributor Author

txchen commented Apr 30, 2018

FYI, my related script for toggling apps:

function toggleApp (appName, bundleName) {
  const foremostApp = App.focused()
  if (foremostApp && foremostApp.bundleIdentifier() === bundleName) {
    foremostApp.hide()
    return
  }
  App.launch(appName, { focus: true })
}

Key.on('1', ['alt'], () => toggleApp('iTerm', 'com.googlecode.iterm2'))
Key.on('1', ['alt', 'shift'], () => toggleApp('Wunderlist', 'com.wunderkinder.wunderlistdesktop'))
Key.on('2', ['alt'], () => toggleApp('Google Chrome', 'com.google.Chrome'))
Key.on('2', ['alt', 'shift'], () => toggleApp('Safari', 'com.apple.Safari'))
Key.on('3', ['alt'], () => toggleApp('Visual Studio Code', 'com.microsoft.VSCode'))
Key.on('3', ['alt', 'shift'], () => toggleApp('Visual Studio Code', 'com.microsoft.VSCode'))

@kasper kasper self-assigned this May 20, 2018
@kasper kasper added this to the 2.6.2 milestone May 20, 2018
@kasper
Copy link
Owner

kasper commented May 20, 2018

Implemented in #212.

@kasper kasper closed this as completed May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants