From f08ce7a38117825bfa2359b6aeb24e2b98b260d5 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Wed, 11 Apr 2018 01:07:58 +0100 Subject: [PATCH] add FlexSpace. A bit refactor structure. Correct loading scripts from preset --- MTMR.xcodeproj/project.pbxproj | 78 +++++++++++++++---- .../Battery.scpt} | 18 ++--- MTMR/AppleScripts/Finder.scpt | 5 ++ MTMR/AppleScripts/PlaySmart.scpt | 11 +++ MTMR/AppleScripts/Spotify.next.scpt | 7 ++ MTMR/AppleScripts/Spotify.nowPlaying.scpt | 10 +++ MTMR/AppleScripts/Vox.next.scpt | 7 ++ MTMR/AppleScripts/Vox.nowPlaying.scpt | 10 +++ MTMR/AppleScripts/Weather.scpt | 32 ++++++++ MTMR/AppleScripts/iTunes.next.scpt | 7 ++ MTMR/AppleScripts/iTunes.nowPlaying.scpt | 10 +++ .../TouchBarPrivateApi-Bridging.h | 0 MTMR/{ => CBridge}/TouchBarPrivateApi.h | 0 MTMR/{ => CBridge}/TouchBarSupport.h | 0 MTMR/{ => CBridge}/TouchBarSupport.m | 0 MTMR/ItemsParsing.swift | 15 +++- MTMR/TouchBarController.swift | 8 +- MTMR/defaultPreset.json | 33 ++++---- MTMR/weather.scpt | 32 -------- 19 files changed, 209 insertions(+), 74 deletions(-) rename MTMR/{battery.scpt => AppleScripts/Battery.scpt} (61%) create mode 100644 MTMR/AppleScripts/Finder.scpt create mode 100644 MTMR/AppleScripts/PlaySmart.scpt create mode 100644 MTMR/AppleScripts/Spotify.next.scpt create mode 100644 MTMR/AppleScripts/Spotify.nowPlaying.scpt create mode 100644 MTMR/AppleScripts/Vox.next.scpt create mode 100644 MTMR/AppleScripts/Vox.nowPlaying.scpt create mode 100644 MTMR/AppleScripts/Weather.scpt create mode 100644 MTMR/AppleScripts/iTunes.next.scpt create mode 100644 MTMR/AppleScripts/iTunes.nowPlaying.scpt rename MTMR/{ => CBridge}/TouchBarPrivateApi-Bridging.h (100%) rename MTMR/{ => CBridge}/TouchBarPrivateApi.h (100%) rename MTMR/{ => CBridge}/TouchBarSupport.h (100%) rename MTMR/{ => CBridge}/TouchBarSupport.m (100%) delete mode 100644 MTMR/weather.scpt diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index 364bd6e..be90502 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -24,10 +24,18 @@ B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */; }; B09EB1E6207C0F8E00D5C1E0 /* MultitouchSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */; }; B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */; }; - B0A8BF9E207B84160086F74D /* weather.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0A8BF9D207B84160086F74D /* weather.scpt */; }; + B0B17431207D6B590004B740 /* PlaySmart.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B17427207D6B580004B740 /* PlaySmart.scpt */; }; + B0B17432207D6B590004B740 /* Weather.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B17428207D6B580004B740 /* Weather.scpt */; }; + B0B17433207D6B590004B740 /* Finder.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B17429207D6B580004B740 /* Finder.scpt */; }; + B0B17434207D6B590004B740 /* Battery.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742A207D6B580004B740 /* Battery.scpt */; }; + B0B17435207D6B590004B740 /* Spotify.next.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742B207D6B590004B740 /* Spotify.next.scpt */; }; + B0B17436207D6B590004B740 /* iTunes.next.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742C207D6B590004B740 /* iTunes.next.scpt */; }; + B0B17437207D6B590004B740 /* Spotify.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742D207D6B590004B740 /* Spotify.nowPlaying.scpt */; }; + B0B17438207D6B590004B740 /* Vox.next.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742E207D6B590004B740 /* Vox.next.scpt */; }; + B0B17439207D6B590004B740 /* Vox.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B1742F207D6B590004B740 /* Vox.nowPlaying.scpt */; }; + B0B1743A207D6B590004B740 /* iTunes.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0B17430207D6B590004B740 /* iTunes.nowPlaying.scpt */; }; B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0C1CFC9205C97D30021C862 /* WindowController.swift */; }; B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */; }; - B0F8771D207AD35400D6E430 /* battery.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0F8771C207AD35400D6E430 /* battery.scpt */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -75,11 +83,19 @@ B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HapticFeedback.swift; sourceTree = ""; }; B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MultitouchSupport.framework; path = ../../../../../System/Library/PrivateFrameworks/MultitouchSupport.framework; sourceTree = ""; }; B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPress.swift; sourceTree = ""; }; - B0A8BF9D207B84160086F74D /* weather.scpt */ = {isa = PBXFileReference; lastKnownFileType = text; path = weather.scpt; sourceTree = ""; }; + B0B17427207D6B580004B740 /* PlaySmart.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = PlaySmart.scpt; sourceTree = ""; }; + B0B17428207D6B580004B740 /* Weather.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Weather.scpt; sourceTree = ""; }; + B0B17429207D6B580004B740 /* Finder.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Finder.scpt; sourceTree = ""; }; + B0B1742A207D6B580004B740 /* Battery.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Battery.scpt; sourceTree = ""; }; + B0B1742B207D6B590004B740 /* Spotify.next.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Spotify.next.scpt; sourceTree = ""; }; + B0B1742C207D6B590004B740 /* iTunes.next.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = iTunes.next.scpt; sourceTree = ""; }; + B0B1742D207D6B590004B740 /* Spotify.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Spotify.nowPlaying.scpt; sourceTree = ""; }; + B0B1742E207D6B590004B740 /* Vox.next.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Vox.next.scpt; sourceTree = ""; }; + B0B1742F207D6B590004B740 /* Vox.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Vox.nowPlaying.scpt; sourceTree = ""; }; + B0B17430207D6B590004B740 /* iTunes.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = iTunes.nowPlaying.scpt; sourceTree = ""; }; B0C1CFC9205C97D30021C862 /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = ""; }; B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TouchBarSupport.m; sourceTree = ""; }; B0F8771B207AC92700D6E430 /* TouchBarSupport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarSupport.h; sourceTree = ""; }; - B0F8771C207AD35400D6E430 /* battery.scpt */ = {isa = PBXFileReference; lastKnownFileType = text; path = battery.scpt; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -144,8 +160,8 @@ B082B251205C7D8000BC04DC /* MTMR */ = { isa = PBXGroup; children = ( - B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */, - B059D62A205F0E7D006E6B86 /* TouchBarPrivateApi-Bridging.h */, + B0B1743B207D6ED40004B740 /* CBridge */, + B0B17426207D6AFE0004B740 /* AppleScripts */, B082B252205C7D8000BC04DC /* AppDelegate.swift */, B082B254205C7D8000BC04DC /* ViewController.swift */, B082B256205C7D8000BC04DC /* Assets.xcassets */, @@ -154,16 +170,12 @@ 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */, 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */, B059D621205E03F5006E6B86 /* TouchBarController.swift */, + 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */, B0C1CFC9205C97D30021C862 /* WindowController.swift */, B082B258205C7D8000BC04DC /* Main.storyboard */, B082B25B205C7D8000BC04DC /* Info.plist */, B082B25C205C7D8000BC04DC /* MTMR.entitlements */, - B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */, - B0F8771B207AC92700D6E430 /* TouchBarSupport.h */, - B0F8771C207AD35400D6E430 /* battery.scpt */, - B0A8BF9D207B84160086F74D /* weather.scpt */, B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */, - 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */, 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */, ); path = MTMR; @@ -187,6 +199,34 @@ path = MTMRUITests; sourceTree = ""; }; + B0B17426207D6AFE0004B740 /* AppleScripts */ = { + isa = PBXGroup; + children = ( + B0B1742A207D6B580004B740 /* Battery.scpt */, + B0B17429207D6B580004B740 /* Finder.scpt */, + B0B1742C207D6B590004B740 /* iTunes.next.scpt */, + B0B17430207D6B590004B740 /* iTunes.nowPlaying.scpt */, + B0B17427207D6B580004B740 /* PlaySmart.scpt */, + B0B1742B207D6B590004B740 /* Spotify.next.scpt */, + B0B1742D207D6B590004B740 /* Spotify.nowPlaying.scpt */, + B0B1742E207D6B590004B740 /* Vox.next.scpt */, + B0B1742F207D6B590004B740 /* Vox.nowPlaying.scpt */, + B0B17428207D6B580004B740 /* Weather.scpt */, + ); + path = AppleScripts; + sourceTree = ""; + }; + B0B1743B207D6ED40004B740 /* CBridge */ = { + isa = PBXGroup; + children = ( + B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */, + B059D62A205F0E7D006E6B86 /* TouchBarPrivateApi-Bridging.h */, + B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */, + B0F8771B207AC92700D6E430 /* TouchBarSupport.h */, + ); + path = CBridge; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -299,11 +339,19 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - B0A8BF9E207B84160086F74D /* weather.scpt in Resources */, + B0B17434207D6B590004B740 /* Battery.scpt in Resources */, + B0B1743A207D6B590004B740 /* iTunes.nowPlaying.scpt in Resources */, B082B257205C7D8000BC04DC /* Assets.xcassets in Resources */, + B0B17437207D6B590004B740 /* Spotify.nowPlaying.scpt in Resources */, + B0B17436207D6B590004B740 /* iTunes.next.scpt in Resources */, B082B25A205C7D8000BC04DC /* Main.storyboard in Resources */, + B0B17435207D6B590004B740 /* Spotify.next.scpt in Resources */, + B0B17433207D6B590004B740 /* Finder.scpt in Resources */, + B0B17439207D6B590004B740 /* Vox.nowPlaying.scpt in Resources */, + B0B17438207D6B590004B740 /* Vox.next.scpt in Resources */, 36C2ECE0207CB1B0003CDA33 /* defaultPreset.json in Resources */, - B0F8771D207AD35400D6E430 /* battery.scpt in Resources */, + B0B17432207D6B590004B740 /* Weather.scpt in Resources */, + B0B17431207D6B590004B740 /* PlaySmart.scpt in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -511,7 +559,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Toxblh.MTMR; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_OBJC_BRIDGING_HEADER = "MTMR/TouchBarPrivateApi-Bridging.h"; + SWIFT_OBJC_BRIDGING_HEADER = "MTMR/CBridge/TouchBarPrivateApi-Bridging.h"; SWIFT_VERSION = 4.0; }; name = Debug; @@ -531,7 +579,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Toxblh.MTMR; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_OBJC_BRIDGING_HEADER = "MTMR/TouchBarPrivateApi-Bridging.h"; + SWIFT_OBJC_BRIDGING_HEADER = "MTMR/CBridge/TouchBarPrivateApi-Bridging.h"; SWIFT_VERSION = 4.0; }; name = Release; diff --git a/MTMR/battery.scpt b/MTMR/AppleScripts/Battery.scpt similarity index 61% rename from MTMR/battery.scpt rename to MTMR/AppleScripts/Battery.scpt index a3a5848..71e4b1e 100644 --- a/MTMR/battery.scpt +++ b/MTMR/AppleScripts/Battery.scpt @@ -3,27 +3,27 @@ set percent to word 6 of theList's item 2 set charge to word 7 of theList's item 2 if (charge = "charging") then -set iconC to "⚡️" + set iconC to "⚡️" else -set iconC to "" + set iconC to "" end if set remainingRaw to my split(theList's item 2, " ") set remainingTime to remainingRaw's item 5 if (remainingTime = "(no") then -set strTime to " (?)" + set strTime to " (?)" else if (remainingTime = "0:00") then -set strTime to "" + set strTime to "" else -set strTime to " (" & remainingTime & ")" + set strTime to " (" & remainingTime & ")" end if return iconC & percent & "%" & strTime to split(someText, delimiter) -set AppleScript's text item delimiters to delimiter -set someText to someText's text items -set AppleScript's text item delimiters to {""} -return someText + set AppleScript's text item delimiters to delimiter + set someText to someText's text items + set AppleScript's text item delimiters to {""} + return someText end split diff --git a/MTMR/AppleScripts/Finder.scpt b/MTMR/AppleScripts/Finder.scpt new file mode 100644 index 0000000..41f97b5 --- /dev/null +++ b/MTMR/AppleScripts/Finder.scpt @@ -0,0 +1,5 @@ +tell application "Finder" + make new Finder window + set target of front window to path to home folder as string + activate +end tell diff --git a/MTMR/AppleScripts/PlaySmart.scpt b/MTMR/AppleScripts/PlaySmart.scpt new file mode 100644 index 0000000..eed3e02 --- /dev/null +++ b/MTMR/AppleScripts/PlaySmart.scpt @@ -0,0 +1,11 @@ +if application "iTunes" is running then + tell application "iTunes" to playpause +end if + +if application "Spotify" is running then + tell application "Spotify" to playpause +end if + +if application "VOX" is running then + tell application "VOX" to playpause +end if diff --git a/MTMR/AppleScripts/Spotify.next.scpt b/MTMR/AppleScripts/Spotify.next.scpt new file mode 100644 index 0000000..04b8eb1 --- /dev/null +++ b/MTMR/AppleScripts/Spotify.next.scpt @@ -0,0 +1,7 @@ +if application "Spotify" is running then + tell application "Spotify" + if player state is playing then + next track + end if + end tell +end if diff --git a/MTMR/AppleScripts/Spotify.nowPlaying.scpt b/MTMR/AppleScripts/Spotify.nowPlaying.scpt new file mode 100644 index 0000000..387695f --- /dev/null +++ b/MTMR/AppleScripts/Spotify.nowPlaying.scpt @@ -0,0 +1,10 @@ +if application "Spotify" is running then + tell application "Spotify" + if player state is playing then + return (get artist of current track) & " – " & (get name of current track) + else + return "" + end if + end tell +end if +return "" diff --git a/MTMR/AppleScripts/Vox.next.scpt b/MTMR/AppleScripts/Vox.next.scpt new file mode 100644 index 0000000..f5d2bdf --- /dev/null +++ b/MTMR/AppleScripts/Vox.next.scpt @@ -0,0 +1,7 @@ +if application "VOX" is running then + tell application "VOX" + if player state is 1 then + next + end if + end tell +end if diff --git a/MTMR/AppleScripts/Vox.nowPlaying.scpt b/MTMR/AppleScripts/Vox.nowPlaying.scpt new file mode 100644 index 0000000..032ee3f --- /dev/null +++ b/MTMR/AppleScripts/Vox.nowPlaying.scpt @@ -0,0 +1,10 @@ +if application "VOX" is running then + tell application "VOX" + if player state is 1 then + return (get artist) & " – " & (get track) + else + return "" + end if + end tell +end if +return "" diff --git a/MTMR/AppleScripts/Weather.scpt b/MTMR/AppleScripts/Weather.scpt new file mode 100644 index 0000000..846bd4c --- /dev/null +++ b/MTMR/AppleScripts/Weather.scpt @@ -0,0 +1,32 @@ +# This script requires two libs. Download them: +# https://itunes.apple.com/ru/app/json-helper-for-applescript/id453114608?l=en&mt=12 +# https://itunes.apple.com/ru/app/location-helper-for-applescript/id488536386?mt=12 +tell application "Location Helper" + set clocation_coords to get location coordinates + tell application "JSON Helper" + set weather to fetch JSON from "http://api.openweathermap.org/data/2.5/weather?lat=" & item 1 of clocation_coords & "&lon=" & item 2 of clocation_coords & "&units=metric&appid=32c4256d09a4c52b38aecddba7a078f6" + set temp to temp of main of weather as string + set cond_icon to icon of item 1 of weather of weather as string + if cond_icon is in ["01d", "01n"] then + set cond to "☀️" + else if cond_icon is in ["02d", "02n"] then + set cond to "⛅️" + else if cond_icon is in ["03d", "03n", "04d", "04n"] then + set cond to "☁️" + else if cond_icon is in ["09d", "09n"] then + set cond to "🌧" + else if cond_icon is in ["10d", "10n"] then + set cond to "🌦" + else if cond_icon is in ["11d", "11n"] then + set cond to "🌩" + else if cond_icon is in ["13d", "13n"] then + set cond to "❄️" + else if cond_icon is in ["50d", "50n"] then + set cond to "🌫" + else + set cond to "" + end if + set temp_round to round (temp * 1.0) + return cond & " " & temp_round & "°C" + end tell +end tell diff --git a/MTMR/AppleScripts/iTunes.next.scpt b/MTMR/AppleScripts/iTunes.next.scpt new file mode 100644 index 0000000..6d8c7f1 --- /dev/null +++ b/MTMR/AppleScripts/iTunes.next.scpt @@ -0,0 +1,7 @@ +if application "iTunes" is running then + tell application "iTunes" + if player state is playing then + next track + end if + end tell +end if diff --git a/MTMR/AppleScripts/iTunes.nowPlaying.scpt b/MTMR/AppleScripts/iTunes.nowPlaying.scpt new file mode 100644 index 0000000..1902221 --- /dev/null +++ b/MTMR/AppleScripts/iTunes.nowPlaying.scpt @@ -0,0 +1,10 @@ +if application "iTunes" is running then + tell application "iTunes" + if player state is playing then + return (get artist of current track) & " – " & (get name of current track) + else + return "" + end if + end tell +end if +return "" diff --git a/MTMR/TouchBarPrivateApi-Bridging.h b/MTMR/CBridge/TouchBarPrivateApi-Bridging.h similarity index 100% rename from MTMR/TouchBarPrivateApi-Bridging.h rename to MTMR/CBridge/TouchBarPrivateApi-Bridging.h diff --git a/MTMR/TouchBarPrivateApi.h b/MTMR/CBridge/TouchBarPrivateApi.h similarity index 100% rename from MTMR/TouchBarPrivateApi.h rename to MTMR/CBridge/TouchBarPrivateApi.h diff --git a/MTMR/TouchBarSupport.h b/MTMR/CBridge/TouchBarSupport.h similarity index 100% rename from MTMR/TouchBarSupport.h rename to MTMR/CBridge/TouchBarSupport.h diff --git a/MTMR/TouchBarSupport.m b/MTMR/CBridge/TouchBarSupport.m similarity index 100% rename from MTMR/TouchBarSupport.m rename to MTMR/CBridge/TouchBarSupport.m diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index ca1f5d8..e2ab6f8 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -53,7 +53,7 @@ class SupportedTypesHolder { enum CodingKeys: String, CodingKey { case refreshInterval } let container = try decoder.container(keyedBy: CodingKeys.self) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) - let scriptURL = Bundle.main.url(forResource: "weather", withExtension: "scpt")! + let scriptURL = Bundle.main.url(forResource: "Weather", withExtension: "scpt")! let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0) return (item: item, action: .none) }, @@ -61,7 +61,8 @@ class SupportedTypesHolder { enum CodingKeys: String, CodingKey { case refreshInterval } let container = try decoder.container(keyedBy: CodingKeys.self) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) - let scriptURL = Bundle.main.url(forResource: "battery", withExtension: "scpt")! + let scriptURL = Bundle.main.url(forResource: "Battery", withExtension: "scpt")! + print(try! String(contentsOf: scriptURL)) let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0) return (item: item, action: .none) }, @@ -91,6 +92,7 @@ enum ItemType: Decodable { case staticButton(title: String) case appleScriptTitledButton(source: String, refreshInterval: Double) case timeButton(formatTemplate: String) + case flexSpace() private enum CodingKeys: String, CodingKey { case type @@ -104,6 +106,7 @@ enum ItemType: Decodable { case staticButton case appleScriptTitledButton case timeButton + case flexSpace } init(from decoder: Decoder) throws { @@ -113,13 +116,15 @@ enum ItemType: Decodable { case .appleScriptTitledButton: let source = try container.decode(String.self, forKey: .titleAppleScript) let interval = try container.decode(Double.self, forKey: .refreshInterval) - self = .appleScriptTitledButton(source: source, refreshInterval: interval) + self = .appleScriptTitledButton(source: try String(contentsOfFile: source), refreshInterval: interval) case .staticButton: let title = try container.decode(String.self, forKey: .title) self = .staticButton(title: title) case .timeButton: let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm" self = .timeButton(formatTemplate: template) + case .flexSpace: + self = .flexSpace() } } } @@ -173,7 +178,9 @@ enum ActionType: Decodable { extension ItemType: Equatable {} func ==(lhs: ItemType, rhs: ItemType) -> Bool { switch (lhs, rhs) { - case let (.staticButton(a), .staticButton(b)): + case let (.staticButton(a), .staticButton(b)): + return a == b + case let (.flexSpace(a), .flexSpace(b)): return a == b case let (.appleScriptTitledButton(a, b), .appleScriptTitledButton(c, d)): return a == c && b == d diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 53d0546..4dbe5d6 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -23,6 +23,8 @@ extension ItemType { return "com.toxblh.mtmr.appleScriptButton." case .timeButton(formatTemplate: _): return "com.toxblh.mtmr.timeButton." + case .flexSpace(): + return "NSTouchBarItem.Identifier.flexibleSpace" } } @@ -61,7 +63,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate { for item in jsonItems { let identifierString = item.type.identifierBase.appending(UUID().uuidString) - let identifier = NSTouchBarItem.Identifier(identifierString) + let identifier = item.type == ItemType.flexSpace() + ? NSTouchBarItem.Identifier.flexibleSpace + : NSTouchBarItem.Identifier(identifierString) items[identifier] = item touchBar.defaultItemIdentifiers += [identifier] } @@ -100,6 +104,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) case .timeButton(formatTemplate: let template): barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template) + case .flexSpace: + barItem = nil } for parameter in item.additionalParameters { if case .width(let value) = parameter, let widthBarItem = barItem as? CanSetWidth { diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json index b4c87e5..31cdebd 100644 --- a/MTMR/defaultPreset.json +++ b/MTMR/defaultPreset.json @@ -1,15 +1,22 @@ [ - { "type": "escape" }, - { "type": "exitTouchbar" }, - { "type": "brightnessDown" }, - { "type": "brightnessUp" }, - { "type": "volumeDown", "width": 44 }, - { "type": "volumeUp", "width": 44 }, - { "type": "previous" }, - { "type": "play" }, - { "type": "next" }, - { "type": "weather", "refreshInterval": 1800 }, - { "type": "sleep" }, - { "type": "battery" }, - { "type": "timeButton" }, + { "type": "escape" }, + { "type": "exitTouchbar" }, + { "type": "brightnessDown" }, + { + "type": "staticButton", + "title": "🔆", + "action": "keyPress", + "keycode": 113, + "width": 36, + }, + { "type": "flexSpace" }, + { "type": "volumeDown", "width": 44 }, + { "type": "volumeUp", "width": 44 }, + { "type": "previous" }, + { "type": "play" }, + { "type": "next" }, + { "type": "weather", "refreshInterval": 1800 }, + { "type": "sleep" }, + { "type": "battery" }, + { "type": "timeButton" }, ] diff --git a/MTMR/weather.scpt b/MTMR/weather.scpt deleted file mode 100644 index 55bb4a6..0000000 --- a/MTMR/weather.scpt +++ /dev/null @@ -1,32 +0,0 @@ -# This script requires two libs. Download them: -# https://itunes.apple.com/ru/app/json-helper-for-applescript/id453114608?l=en&mt=12 -# https://itunes.apple.com/ru/app/location-helper-for-applescript/id488536386?mt=12 -tell application "Location Helper" -set clocation_coords to get location coordinates -tell application "JSON Helper" -set weather to fetch JSON from "http://api.openweathermap.org/data/2.5/weather?lat=" & item 1 of clocation_coords & "&lon=" & item 2 of clocation_coords & "&units=metric&appid=32c4256d09a4c52b38aecddba7a078f6" -set temp to temp of main of weather as string -set cond_icon to icon of item 1 of weather of weather as string -if cond_icon is in ["01d", "01n"] then -set cond to "☀️" -else if cond_icon is in ["02d", "02n"] then -set cond to "⛅️" -else if cond_icon is in ["03d", "03n", "04d", "04n"] then -set cond to "☁️" -else if cond_icon is in ["09d", "09n"] then -set cond to "🌧" -else if cond_icon is in ["10d", "10n"] then -set cond to "🌦" -else if cond_icon is in ["11d", "11n"] then -set cond to "🌩" -else if cond_icon is in ["13d", "13n"] then -set cond to "❄️" -else if cond_icon is in ["50d", "50n"] then -set cond to "🌫" -else -set cond to "" -end if -set temp_round to round (temp * 1.0) -return cond & " " & temp_round & "°C" -end tell -end tell