Skip to content

Commit

Permalink
NC: support launchbundle (#117)
Browse files Browse the repository at this point in the history
* NC: support launchbundle

* NC: apply for_where syntactic sugar
  • Loading branch information
zhen-zen committed Feb 11, 2021
1 parent 9355444 commit bf5a331
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
50 changes: 44 additions & 6 deletions YogaSMCNC/NotificationHandler.swift
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2020 Zhen. All rights reserved.
//

import Foundation
import AppKit
import os.log

func registerNotification(_ conf: inout SharedConfig) -> Bool {
Expand Down Expand Up @@ -87,15 +87,53 @@ func eventActuator(_ desc: EventDesc, _ data: UInt32, _ conf: inout SharedConfig
if let scpt = desc.option {
_ = scriptHelper(scpt, desc.name, desc.display ? desc.image : nil)
} else {
os_log("%s: script not found", type: .error)
os_log("%s: script not found", type: .error, desc.name)
}
case .launchapp:
if let name = desc.option {
let scpt = "tell application \"" + name + "\" to activate"
_ = scriptHelper(scpt, desc.name, desc.display ? desc.image : nil)
if let name = desc.option,
NSWorkspace.shared.launchApplication(name) {
if desc.display { showOSD(desc.name, desc.image) }
} else {
os_log("%s: app name not found", type: .error, desc.name)
}
case .launchbundle:
guard let identifier = desc.option else {
os_log("%s: bundle not found", type: .error)
return
}
let collection = NSRunningApplication.runningApplications(withBundleIdentifier: identifier)
if collection.isEmpty {
if #available(OSX 10.15, *) {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: identifier) else {
os_log("%s: app url not found", type: .error, desc.name)
return
}
NSWorkspace.shared.openApplication(at: url, configuration: .init(), completionHandler: nil)
} else {
guard NSWorkspace.shared.launchApplication(withBundleIdentifier: identifier,
options: .default,
additionalEventParamDescriptor: nil,
launchIdentifier: nil) else {
os_log("%s: app identifier not found", type: .error, desc.name)
return
}
}
} else {
os_log("%s: script not found", type: .error)
var hide = false
for app in collection where !app.isHidden {
hide = true
break
}
for app in collection {
if hide {
app.hide()
} else {
app.activate(options: .activateIgnoringOtherApps)
}
}
if hide { return }
}
if desc.display { showOSD(desc.name, desc.image) }
case .airplane:
airplaneModeHelper(desc.name, desc.display)
case .bluetooth:
Expand Down
7 changes: 5 additions & 2 deletions YogaSMCUtils/Configuration.swift
Expand Up @@ -12,7 +12,7 @@ import os.log

enum EventAction: String {
// Userspace
case nothing, script, launchapp
case nothing, script, launchapp, launchbundle
case airplane, wireless, bluetooth, bluetoothdiscoverable
case prefpane, spotlight, search, siri, sleep, micmute
case mission, launchpad, desktop, expose
Expand Down Expand Up @@ -132,7 +132,10 @@ let thinkEvents: [UInt32: [UInt32: EventDesc]] = [
TP_HKEY_EV_SEARCH.rawValue: [0: EventDesc("Search", act: .siri, display: false)], // 0x101E
TP_HKEY_EV_MISSION.rawValue: [0: EventDesc("Mission Control", act: .mission, display: false)], // 0x101F
TP_HKEY_EV_APPS.rawValue: [0: EventDesc("Launchpad", act: .launchpad, display: false)], // 0x1020
TP_HKEY_EV_STAR.rawValue: [0: EventDesc("Custom Hotkey", .kStar, act: .script, opt: prefpaneAS)], // 0x1311
TP_HKEY_EV_STAR.rawValue: [
0: EventDesc("Custom Hotkey", .kStar, act: .script, opt: prefpaneAS),
optionFlag: EventDesc("System Prefereces", .kStar, act: .launchbundle, opt: "com.apple.systempreferences")
], // 0x1311
TP_HKEY_EV_BLUETOOTH.rawValue: [
0: EventDesc("Bluetooth", act: .bluetooth),
optionFlag: EventDesc("BT Discoverable", act: .bluetoothdiscoverable)
Expand Down

0 comments on commit bf5a331

Please sign in to comment.