diff --git a/Air Drop Assistant.xcodeproj/project.pbxproj b/Air Drop Assistant.xcodeproj/project.pbxproj index 510216a..41cde6d 100644 --- a/Air Drop Assistant.xcodeproj/project.pbxproj +++ b/Air Drop Assistant.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 736C80F92C70302200E5E780 /* PrefWatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736C80F82C70302200E5E780 /* PrefWatcher.swift */; }; 736C80FE2C70EC6800E5E780 /* PreferencesWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736C80FD2C70EC6800E5E780 /* PreferencesWindow.swift */; }; 736C81002C70EC9E00E5E780 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736C80FF2C70EC9E00E5E780 /* PreferencesViewController.swift */; }; + 73B614B92CF77F5500C3A14F /* PreferenceObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73B614B82CF77F5500C3A14F /* PreferenceObserver.swift */; }; 73FFDCFB2C70210B00244D57 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73FFDCFA2C70210B00244D57 /* AppDelegate.swift */; }; 73FFDCFD2C70210B00244D57 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73FFDCFC2C70210B00244D57 /* ViewController.swift */; }; 73FFDCFF2C70210C00244D57 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 73FFDCFE2C70210C00244D57 /* Assets.xcassets */; }; @@ -51,6 +52,7 @@ 736C80FD2C70EC6800E5E780 /* PreferencesWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindow.swift; sourceTree = ""; }; 736C80FF2C70EC9E00E5E780 /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = ""; }; 737B8A342C8BE5D1006A4653 /* Air-Drop-Assistant-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "Air-Drop-Assistant-Info.plist"; sourceTree = SOURCE_ROOT; }; + 73B614B82CF77F5500C3A14F /* PreferenceObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceObserver.swift; sourceTree = ""; }; 73FFDCF72C70210B00244D57 /* Air Drop Assistant.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Air Drop Assistant.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 73FFDCFA2C70210B00244D57 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 73FFDCFC2C70210B00244D57 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -94,6 +96,7 @@ 736C80FD2C70EC6800E5E780 /* PreferencesWindow.swift */, 73FFDCFA2C70210B00244D57 /* AppDelegate.swift */, 736C80F82C70302200E5E780 /* PrefWatcher.swift */, + 73B614B82CF77F5500C3A14F /* PreferenceObserver.swift */, 736C80FF2C70EC9E00E5E780 /* PreferencesViewController.swift */, 7328CDA42C8BA87B0042A517 /* com.ttinc.Air-Drop-Assistant.plist */, 73FFDCFC2C70210B00244D57 /* ViewController.swift */, @@ -179,6 +182,7 @@ buildActionMask = 2147483647; files = ( 736C81002C70EC9E00E5E780 /* PreferencesViewController.swift in Sources */, + 73B614B92CF77F5500C3A14F /* PreferenceObserver.swift in Sources */, 736C80FE2C70EC6800E5E780 /* PreferencesWindow.swift in Sources */, 73FFDCFD2C70210B00244D57 /* ViewController.swift in Sources */, 73FFDCFB2C70210B00244D57 /* AppDelegate.swift in Sources */, @@ -330,7 +334,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 311; + CURRENT_PROJECT_VERSION = 17; DEVELOPMENT_TEAM = 2WUMX954UB; ENABLE_HARDENED_RUNTIME = YES; GENERATE_INFOPLIST_FILE = YES; @@ -345,7 +349,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 13.0; - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2; PRODUCT_BUNDLE_IDENTIFIER = "com.ttinc.Air-Drop-Assistant"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -365,7 +369,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 311; + CURRENT_PROJECT_VERSION = 17; DEVELOPMENT_TEAM = 2WUMX954UB; ENABLE_HARDENED_RUNTIME = YES; GENERATE_INFOPLIST_FILE = YES; @@ -380,7 +384,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 13.0; - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2; PRODUCT_BUNDLE_IDENTIFIER = "com.ttinc.Air-Drop-Assistant"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/Air Drop Assistant/AppDelegate.swift b/Air Drop Assistant/AppDelegate.swift index 286f361..df1b041 100644 --- a/Air Drop Assistant/AppDelegate.swift +++ b/Air Drop Assistant/AppDelegate.swift @@ -12,7 +12,49 @@ import ServiceManagement @NSApplicationMain -class AppDelegate: NSObject, NSApplicationDelegate, DataModelDelegate, PrefDataModelDelegate { +class AppDelegate: NSObject, NSApplicationDelegate, DataModelDelegate, PrefDataModelDelegate, AppPrefObserverDelegate { + func newPreferenceValue() { + menuIcon() + + guard let appBundleID = Bundle.main.bundleIdentifier else { return } + let hideMenuIconValue = UserDefaults.standard.bool(forKey: "hideMenuIcon") + let isForced = CFPreferencesAppValueIsForced("hideMenuIcon" as CFString, appBundleID as CFString) + + if hideMenuIconValue && isForced { + adaMenu.menu?.removeAllItems() + NSStatusBar.system.removeStatusItem(adaMenu) + } else { + guard adaMenu.menu != nil else { return } + adaMenu = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) + + adaMenu.menu = NSMenu() + + menuIcon() + + adaMenuListing() + let prefs = NSMenuItem(title: "Preferences", action: #selector(Preferences), keyEquivalent: "") + let softwareUpdate = NSMenuItem(title: "Check for Update", action: #selector(updateCheckFunc), keyEquivalent: "") + + var IncreaseByOne: Int = 0 + if let menuItems = adaMenu.menu { + for item in menuItems.items { + + if item.title == "AirDrop: Incoming Only"{ + IncreaseByOne += 1 + } + if item.title == "AirDrop: Outgoing Only" { + IncreaseByOne += 1 + } + } + } + + adaMenu.menu?.insertItem(prefs, at: 1 + IncreaseByOne) + adaMenu.menu?.insertItem(softwareUpdate, at: 2 + IncreaseByOne) + let quit = NSMenuItem(title: "Quit", action: #selector(QuitApp), keyEquivalent: "") + adaMenu.menu?.insertItem(quit, at: 3 + IncreaseByOne) + } + } + func checkAirDrop() { if domain!.string(forKey: "DiscoverableMode") == UserDefaults.standard.string(forKey: "airDropSetting") || domain!.string(forKey: "DiscoverableMode") == "Off" { return @@ -81,12 +123,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, DataModelDelegate, PrefDataM } let domain = UserDefaults(suiteName: "com.apple.sharingd") - let adaMenu = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) + var adaMenu = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) var airDropStatus = "" let prefWatcher = PrefWatcher() let updater = UpdateCheck() - let prefViewController = PreferencesViewController() - + let observer = AppPreferencesObserver() func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { // Ensure that the app doesn't show the menu bar or Dock icon when reopened NSApp.setActivationPolicy(.accessory) @@ -94,6 +135,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, DataModelDelegate, PrefDataM } func applicationDidFinishLaunching(_ aNotification: Notification) { NSLog("ADA Launched") + NSApp.setActivationPolicy(.accessory) if isAppAlreadyRunning() { NSApp.terminate(nil) @@ -106,7 +148,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, DataModelDelegate, PrefDataM } let arguments = CommandLine.arguments let stringarguments = String(describing: arguments) - NSLog(stringarguments) if arguments[1] == "--register" { do { @@ -181,7 +222,7 @@ AirDrop is disabled by an MDM Profile. Please contact your MDM administrator. UserDefaults.standard.set(15, forKey: "timing") } prefWatcher.delegate = self - prefViewController.delegate = self + observer.delegate = self if domain?.string(forKey: "DiscoverableMode") != UserDefaults.standard.string(forKey: "airDropSetting") && domain?.string(forKey: "DiscoverableMode") != "Off" { Task { @@ -260,7 +301,6 @@ AirDrop is disabled by an MDM Profile. Please contact your MDM administrator. func adaMenuListing(){ var PFADAStatus: String? -// let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String guard let bundleID = Bundle.main.bundleIdentifier else { return } let path = "/Library/Preferences/\(bundleID).plist" @@ -332,6 +372,8 @@ AirDrop is disabled by an MDM Profile. Please contact your MDM administrator. return } } + let prefViewController = PreferencesViewController() + prefViewController.delegate = self var window: PreferencesWindow? let windowSize = NSSize(width: 415, height: 200) let screenSize = NSScreen.main?.frame.size ?? .zero diff --git a/Air Drop Assistant/PreferenceObserver.swift b/Air Drop Assistant/PreferenceObserver.swift new file mode 100644 index 0000000..de25402 --- /dev/null +++ b/Air Drop Assistant/PreferenceObserver.swift @@ -0,0 +1,45 @@ +// +// PreferenceObserver.swift +// Air Drop Assistant +// +// Created by Bob Gendler on 11/27/24. +// +import Combine +import Foundation + + + +extension UserDefaults { + @objc dynamic var hideMenuIcon: String? { + return string(forKey: "hideMenuIcon") + } + + @objc dynamic var icon_mode: String? { + return string(forKey: "icon_mode") + } +} + +protocol AppPrefObserverDelegate { + func newPreferenceValue() +} + +class AppPreferencesObserver { + + var delegate: AppPrefObserverDelegate? + private var cancellables = Set() + + init() { + let hideMenuIconPref = UserDefaults.standard.publisher(for: \.hideMenuIcon) + let icon_modePref = UserDefaults.standard.publisher(for: \.icon_mode) + + // Combine the two publishers + Publishers.CombineLatest(hideMenuIconPref, icon_modePref) + .sink { _, _ in + self.delegate?.newPreferenceValue() + } + .store(in: &cancellables) + } +} + +// Usage + diff --git a/Air Drop Assistant/com.ttinc.Air-Drop-Assistant.plist b/Air Drop Assistant/com.ttinc.Air-Drop-Assistant.plist index 0830428..8ac17f5 100644 --- a/Air Drop Assistant/com.ttinc.Air-Drop-Assistant.plist +++ b/Air Drop Assistant/com.ttinc.Air-Drop-Assistant.plist @@ -2,6 +2,8 @@ + KeepAlive + Label com.ttinc.Air-Drop-Assistant RunAtLoad @@ -10,7 +12,7 @@ Contents/MacOS/Air Drop Assistant ProcessType Interactive - LimitLoadToSessionType - Aqua + LimitLoadToSessionType + Aqua