-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
78 lines (60 loc) · 2.07 KB
/
AppDelegate.swift
File metadata and controls
78 lines (60 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// AppDelegate.swift
// MacBarButton
//
// Created by Renan Greca on 06/07/18.
// Copyright © 2018 renangreca. All rights reserved.
//
import Cocoa
/**
Run arbitrary shell commands using this function.
*Example:* `shell("ping", "192.168.0.1")`
- warning: you must disable App Sandbox in the project settings for this to work.
- note: Ref. [Stack Overflow](https://stackoverflow.com/questions/26971240/how-do-i-run-an-terminal-command-in-a-swift-script-e-g-xcodebuild#26973384)
*/
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
var statusButton: NSStatusBarButton!
var isEnabled: Bool = true
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Aquire an NSStatusItem from the NSStatusBar
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.isVisible = true
statusItem.behavior = [.removalAllowed, .terminationOnRemoval]
// Aquire a button related to the NSStatusItem
statusButton = statusItem.button!
// Configure the button's properties
statusButton.image = #imageLiteral(resourceName: "green_circle")
statusButton.target = self
statusButton.action = #selector(didTapButton)
// Make sure the button always starts enabled
enable()
}
@objc func didTapButton() {
if isEnabled {
disable()
} else {
enable()
}
}
func enable() {
// Run anything here
statusButton.image = #imageLiteral(resourceName: "green_circle")
isEnabled = true
}
func disable() {
// Run anything here
statusButton.image = #imageLiteral(resourceName: "white_circle")
isEnabled = false
}
}