From 6ad64d5b44669fd3986543a90cd3c620d6b679b4 Mon Sep 17 00:00:00 2001 From: meeh Date: Fri, 18 Jan 2019 16:25:01 +0000 Subject: [PATCH] Mac OSX Launcher: Adding the "launcher launcher" for startup functionality. --- .../macosx/StartupItemApp/AppDelegate.swift | 54 +++++++++++++++++ .../AppIcon.appiconset/Contents.json | 58 +++++++++++++++++++ .../Assets.xcassets/Contents.json | 6 ++ launchers/macosx/StartupItemApp/Info.plist | 34 +++++++++++ .../StartupItemApp.entitlements | 14 +++++ 5 files changed, 166 insertions(+) create mode 100644 launchers/macosx/StartupItemApp/AppDelegate.swift create mode 100644 launchers/macosx/StartupItemApp/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 launchers/macosx/StartupItemApp/Assets.xcassets/Contents.json create mode 100644 launchers/macosx/StartupItemApp/Info.plist create mode 100644 launchers/macosx/StartupItemApp/StartupItemApp.entitlements diff --git a/launchers/macosx/StartupItemApp/AppDelegate.swift b/launchers/macosx/StartupItemApp/AppDelegate.swift new file mode 100644 index 000000000..71b6002a4 --- /dev/null +++ b/launchers/macosx/StartupItemApp/AppDelegate.swift @@ -0,0 +1,54 @@ +// +// AppDelegate.swift +// StartupItemApp +// +// Created by Mikal Villa on 21/12/2018. +// Copyright © 2018 The I2P Project. All rights reserved. +// + +import Cocoa + +extension Notification.Name { + static let killLauncher = Notification.Name("killStartupLauncher") +} + +@NSApplicationMain +class AppDelegate: NSObject { + + @objc func terminate() { + NSApp.terminate(nil) + } +} + +extension AppDelegate: NSApplicationDelegate { + + func applicationDidFinishLaunching(_ aNotification: Notification) { + + let mainAppIdentifier = "net.i2p.bootstrap-macosx.I2PLauncher" + let runningApps = NSWorkspace.shared.runningApplications + let isRunning = !runningApps.filter { $0.bundleIdentifier == mainAppIdentifier }.isEmpty + + if !isRunning { + DistributedNotificationCenter.default().addObserver(self, + selector: #selector(self.terminate), + name: .killLauncher, + object: mainAppIdentifier) + + let path = Bundle.main.bundlePath as NSString + var components = path.pathComponents + components.removeLast() + components.removeLast() + components.removeLast() + components.append("MacOS") + components.append("I2PLauncher") //main app name + + let newPath = NSString.path(withComponents: components) + + NSWorkspace.shared.launchApplication(newPath) + } + else { + self.terminate() + } + } +} + diff --git a/launchers/macosx/StartupItemApp/Assets.xcassets/AppIcon.appiconset/Contents.json b/launchers/macosx/StartupItemApp/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 000000000..2db2b1c7c --- /dev/null +++ b/launchers/macosx/StartupItemApp/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/launchers/macosx/StartupItemApp/Assets.xcassets/Contents.json b/launchers/macosx/StartupItemApp/Assets.xcassets/Contents.json new file mode 100644 index 000000000..da4a164c9 --- /dev/null +++ b/launchers/macosx/StartupItemApp/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/launchers/macosx/StartupItemApp/Info.plist b/launchers/macosx/StartupItemApp/Info.plist new file mode 100644 index 000000000..d4bd41467 --- /dev/null +++ b/launchers/macosx/StartupItemApp/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2018 The I2P Project. All rights reserved. + NSMainNibFile + MainMenu + NSPrincipalClass + NSApplication + LSBackgroundOnly + + + diff --git a/launchers/macosx/StartupItemApp/StartupItemApp.entitlements b/launchers/macosx/StartupItemApp/StartupItemApp.entitlements new file mode 100644 index 000000000..7ba9c077a --- /dev/null +++ b/launchers/macosx/StartupItemApp/StartupItemApp.entitlements @@ -0,0 +1,14 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.application-groups + + $(TeamIdentifierPrefix) + + com.apple.security.files.user-selected.read-only + + +