Skip to content

Commit

Permalink
Added support for tomatobar://startStop url, closes #56 and #29
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoronin committed Sep 5, 2023
1 parent ece5578 commit 1c5f607
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions TomatoBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = TomatoBar/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2022 Ilya Voronin. All rights reserved. https://github.com/ivoronin/TomatoBar";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -387,6 +388,7 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = TomatoBar/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2022 Ilya Voronin. All rights reserved. https://github.com/ivoronin/TomatoBar";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
17 changes: 15 additions & 2 deletions TomatoBar/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSUIElement</key>
<true/>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLIconFile</key>
<string></string>
<key>CFBundleURLName</key>
<string>com.github.ivoronin.TomatoBar</string>
<key>CFBundleURLSchemes</key>
<array>
<string>tomatobar</string>
</array>
</dict>
</array>
</dict>
</plist>
32 changes: 32 additions & 0 deletions TomatoBar/Timer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ class TBTimer: ObservableObject {

KeyboardShortcuts.onKeyUp(for: .startStopTimer, action: startStop)
notificationCenter.setActionHandler(handler: onNotificationAction)

let aem: NSAppleEventManager = NSAppleEventManager.shared()
aem.setEventHandler(self,
andSelector: #selector(handleGetURLEvent(_:withReplyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
}

@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor,
withReplyEvent: NSAppleEventDescriptor) {
guard let urlString = event.forKeyword(AEKeyword(keyDirectObject))?.stringValue else {
print("url handling error: cannot get url")
return
}
let url = URL(string: urlString)
guard url != nil,
let scheme = url!.scheme,
let host = url!.host else {
print("url handling error: cannot parse url")
return
}
guard scheme.caseInsensitiveCompare("tomatobar") == .orderedSame else {
print("url handling error: unknown scheme \(scheme)")
return
}
switch host.lowercased() {
case "startstop":
startStop()
default:
print("url handling error: unknown command \(host)")
return
}
}

func startStop() {
Expand Down

0 comments on commit 1c5f607

Please sign in to comment.