From 59624fc7c2b3ccf0875bdb77338e339779f9db74 Mon Sep 17 00:00:00 2001 From: Matan Mashraki Date: Tue, 11 Jun 2019 21:12:28 +0300 Subject: [PATCH] Fixed always light bug with Electron apps --- Auto Dark.xcodeproj/project.pbxproj | 4 ---- Auto Dark/GetBundleIDs.scpt | 7 ------- Auto Dark/Info.plist | 2 +- Auto Dark/ViewController.swift | 28 +++++++++------------------- 4 files changed, 10 insertions(+), 31 deletions(-) delete mode 100755 Auto Dark/GetBundleIDs.scpt diff --git a/Auto Dark.xcodeproj/project.pbxproj b/Auto Dark.xcodeproj/project.pbxproj index 7a2adb9..5e919ee 100644 --- a/Auto Dark.xcodeproj/project.pbxproj +++ b/Auto Dark.xcodeproj/project.pbxproj @@ -16,7 +16,6 @@ 908230B021F1FBB600FA2D36 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 908230AF21F1FBB600FA2D36 /* LocationManager.swift */; }; 908230B221F20F6600FA2D36 /* PreferencesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 908230B121F20F6600FA2D36 /* PreferencesController.swift */; }; 90D19C2421F35FF9001C410D /* ScheduleManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90D19C2321F35FF9001C410D /* ScheduleManager.swift */; }; - 90F5E28122AEA5F200DB92D2 /* GetBundleIDs.scpt in Resources */ = {isa = PBXBuildFile; fileRef = 90F5E28022AEA5F200DB92D2 /* GetBundleIDs.scpt */; }; 90F5E28522AEAC1000DB92D2 /* DarkMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90F5E28422AEAC1000DB92D2 /* DarkMode.swift */; }; 90F5E28722AEAC4600DB92D2 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90F5E28622AEAC4600DB92D2 /* App.swift */; }; /* End PBXBuildFile section */ @@ -34,7 +33,6 @@ 908230AF21F1FBB600FA2D36 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = ""; }; 908230B121F20F6600FA2D36 /* PreferencesController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesController.swift; sourceTree = ""; }; 90D19C2321F35FF9001C410D /* ScheduleManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduleManager.swift; sourceTree = ""; }; - 90F5E28022AEA5F200DB92D2 /* GetBundleIDs.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GetBundleIDs.scpt; sourceTree = ""; }; 90F5E28422AEAC1000DB92D2 /* DarkMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DarkMode.swift; sourceTree = ""; }; 90F5E28622AEAC4600DB92D2 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; 90FB494F22022444000D6510 /* LICENSE.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = ""; }; @@ -74,7 +72,6 @@ children = ( 905E4A7821A19E2500D1DBC7 /* AppDelegate.swift */, 905E4A7F21A19E2500D1DBC7 /* Info.plist */, - 90F5E28022AEA5F200DB92D2 /* GetBundleIDs.scpt */, 905E4A7A21A19E2500D1DBC7 /* Assets.xcassets */, 905E4A7C21A19E2500D1DBC7 /* MainMenu.xib */, 90222E9022018C9D004D183C /* Preferences.storyboard */, @@ -155,7 +152,6 @@ 905E4A7B21A19E2500D1DBC7 /* Assets.xcassets in Resources */, 905E4A7E21A19E2500D1DBC7 /* MainMenu.xib in Resources */, 90222E9122018C9D004D183C /* Preferences.storyboard in Resources */, - 90F5E28122AEA5F200DB92D2 /* GetBundleIDs.scpt in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Auto Dark/GetBundleIDs.scpt b/Auto Dark/GetBundleIDs.scpt deleted file mode 100755 index 6541a8d..0000000 --- a/Auto Dark/GetBundleIDs.scpt +++ /dev/null @@ -1,7 +0,0 @@ -set bundles to {} -tell application "System Events" - set names to get the name of every process whose visible is true -end tell -repeat with name in names - set bundles to bundles & id of application name -end repeat \ No newline at end of file diff --git a/Auto Dark/Info.plist b/Auto Dark/Info.plist index 30599e2..eae5552 100644 --- a/Auto Dark/Info.plist +++ b/Auto Dark/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.4 CFBundleVersion - 9 + 10 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) LSUIElement diff --git a/Auto Dark/ViewController.swift b/Auto Dark/ViewController.swift index 5ccbd23..5962415 100644 --- a/Auto Dark/ViewController.swift +++ b/Auto Dark/ViewController.swift @@ -116,33 +116,22 @@ class ViewController: NSObject, ViewControllerDelegate { } } - @discardableResult func getOpenedApps() -> [App] { - let path = Bundle.main.path(forResource: "GetBundleIDs", ofType: "scpt")! - var outstr = "" - let task = Process() - task.launchPath = "/usr/bin/osascript" - task.arguments = [path] - let pipe = Pipe() - task.standardOutput = pipe - task.launch() - let data = pipe.fileHandleForReading.readDataToEndOfFile() - if let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) { - outstr = (output as String).replacingOccurrences(of: "\n", with: "") - } - task.waitUntilExit() + let running = NSWorkspace.shared.runningApplications.filter({$0.activationPolicy == .regular}) var apps = [App]() - for item in outstr.split(separator: " ").joined().split(separator: ",") { - if item != "com.apple.Safari" && item != "com.apple.mail" { - apps.append(App(bundle: String(item))) + for item in running { + if let bundle = item.bundleIdentifier { + if bundle != "com.apple.Safari" && bundle != "com.apple.mail" { + apps.append(App(bundle: bundle)) + } } } return apps } func setAlwaysLightMenu() { - alwaysLightMenu.removeAllItems() let apps = getOpenedApps() + var items = [NSMenuItem]() for app in apps { let item = NSMenuItem(title: app.name, action: #selector(self.changeAlwaysLight), keyEquivalent: "") item.image = app.image @@ -154,8 +143,9 @@ class ViewController: NSObject, ViewControllerDelegate { } else { item.state = .off } - alwaysLightMenu.addItem(item) + items.append(item) } + alwaysLightMenu.items = items } @objc func changeAlwaysLight(_ sender: Any) {