From ab4aef4e2ac7ecffde7574364997f0dd590c5fb5 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Mon, 9 Apr 2018 12:22:31 +0100 Subject: [PATCH] added weather --- MTMR.xcodeproj/project.pbxproj | 4 ++++ MTMR/TouchBarController.swift | 5 +++++ MTMR/weather.scpt | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 MTMR/weather.scpt diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index 9525ac5..8f337d8 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ B082B266205C7D8000BC04DC /* MTMRTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B082B265205C7D8000BC04DC /* MTMRTests.swift */; }; B082B271205C7D8000BC04DC /* MTMRUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B082B270205C7D8000BC04DC /* MTMRUITests.swift */; }; B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */; }; + B0A8BF9E207B84160086F74D /* weather.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0A8BF9D207B84160086F74D /* weather.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 */; }; @@ -64,6 +65,7 @@ B082B270205C7D8000BC04DC /* MTMRUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MTMRUITests.swift; sourceTree = ""; }; B082B272205C7D8000BC04DC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPress.swift; sourceTree = ""; }; + B0A8BF9D207B84160086F74D /* weather.scpt */ = {isa = PBXFileReference; lastKnownFileType = text; path = weather.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 = ""; }; @@ -146,6 +148,7 @@ B0F87719207AC1EA00D6E430 /* TouchBarSupport.m */, B0F8771B207AC92700D6E430 /* TouchBarSupport.h */, B0F8771C207AD35400D6E430 /* battery.scpt */, + B0A8BF9D207B84160086F74D /* weather.scpt */, ); path = MTMR; sourceTree = ""; @@ -280,6 +283,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + B0A8BF9E207B84160086F74D /* weather.scpt in Resources */, B082B257205C7D8000BC04DC /* Assets.xcassets in Resources */, B082B25A205C7D8000BC04DC /* Main.storyboard in Resources */, B0F8771D207AD35400D6E430 /* battery.scpt in Resources */, diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 34868ee..978c349 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -79,6 +79,11 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN) case .volumeUp: return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP) + + case .weather: + let url = Bundle.main.url(forResource: "weather", withExtension: "scpt")! + let script = try! String.init(contentsOf: url) + return AppleScriptTouchBarItem(identifier: identifier, appleScript: script, interval: 600) case .prev: return CustomButtonTouchBarItem(identifier: identifier, title: "⏪", HIDKeycode: NX_KEYTYPE_PREVIOUS) diff --git a/MTMR/weather.scpt b/MTMR/weather.scpt new file mode 100644 index 0000000..55bb4a6 --- /dev/null +++ b/MTMR/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