-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ReDetection/master
Basic AppleScript support
- Loading branch information
Showing
5 changed files
with
115 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
|
||
class AppleScriptTouchBarItem: NSCustomTouchBarItem { | ||
let script: NSAppleScript | ||
private var timer: Timer! | ||
private let button = NSButton(title: "", target: nil, action: nil) | ||
|
||
init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) { | ||
guard let script = NSAppleScript(source: appleScript) else { | ||
return nil | ||
} | ||
self.script = script | ||
super.init(identifier: identifier) | ||
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) | ||
self.view = button | ||
button.bezelColor = .clear | ||
refresh() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
@objc func refresh() { | ||
self.button.title = self.execute() | ||
} | ||
|
||
func execute() -> String { | ||
var error: NSDictionary? | ||
let output = script.executeAndReturnError(&error) | ||
if let error = error { | ||
print(error) | ||
return "error" | ||
} | ||
return output.stringValue ?? "empty value" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Cocoa | ||
|
||
class TimeTouchBarItem: NSCustomTouchBarItem { | ||
private let dateFormatter = DateFormatter() | ||
private var timer: Timer! | ||
private let button = NSButton(title: "", target: nil, action: nil) | ||
|
||
init(identifier: NSTouchBarItem.Identifier, formatTemplate: String) { | ||
dateFormatter.setLocalizedDateFormatFromTemplate(formatTemplate) | ||
super.init(identifier: identifier) | ||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true) | ||
self.view = button | ||
button.bezelColor = .clear | ||
updateTime() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
@objc func updateTime() { | ||
button.title = self.dateFormatter.string(from: Date()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters