Skip to content

Commit

Permalink
add terminateApplicationsOnLaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
erikng committed May 26, 2022
1 parent 7e24d65 commit c106cb6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Example Assets/com.github.macadmins.Nudge.json
Expand Up @@ -10,7 +10,8 @@
"attemptToBlockApplicationLaunches": false,
"attemptToFetchMajorUpgrade": true,
"blockedApplicationBundleIDs": [],
"enforceMinorUpdates": true
"enforceMinorUpdates": true,
"terminateApplicationsOnLaunch": false
},
"osVersionRequirements": [
{
Expand Down
2 changes: 2 additions & 0 deletions Example Assets/com.github.macadmins.Nudge.mobileconfig
Expand Up @@ -45,6 +45,8 @@
<array/>
<key>enforceMinorUpdates</key>
<true/>
<key>terminateApplicationsOnLaunch</key>
<false/>
</dict>
<key>osVersionRequirements</key>
<array>
Expand Down
3 changes: 2 additions & 1 deletion Example Assets/com.github.macadmins.Nudge.tester.json
Expand Up @@ -13,7 +13,8 @@
"blockedApplicationBundleIDs": [
"com.microsoft.VSCode",
"us.zoom.xos"
]
],
"terminateApplicationsOnLaunch": false
},
"osVersionRequirements": [
{
Expand Down
1 change: 1 addition & 0 deletions Nudge/Preferences/DefaultPreferencesNudge.swift
Expand Up @@ -44,6 +44,7 @@ let attemptToFetchMajorUpgrade = optionalFeaturesProfile?["attemptToFetchMajorUp
let blockedApplicationBundleIDs = optionalFeaturesProfile?["blockedApplicationBundleIDs"] as? [String] ?? optionalFeaturesJSON?.blockedApplicationBundleIDs ?? [String]()
let enforceMinorUpdates = optionalFeaturesProfile?["enforceMinorUpdates"] as? Bool ?? optionalFeaturesJSON?.enforceMinorUpdates ?? true
let disableSoftwareUpdateWorkflow = optionalFeaturesProfile?["disableSoftwareUpdateWorkflow"] as? Bool ?? optionalFeaturesJSON?.disableSoftwareUpdateWorkflow ?? false
let terminateApplicationsOnLaunch = optionalFeaturesProfile?["terminateApplicationsOnLaunch"] as? Bool ?? optionalFeaturesJSON?.terminateApplicationsOnLaunch ?? false

// osVersionRequirements
let osVersionRequirementsProfile = getOSVersionRequirementsProfile()
Expand Down
9 changes: 6 additions & 3 deletions Nudge/Preferences/PreferencesStructure.swift
Expand Up @@ -69,7 +69,8 @@ struct OptionalFeatures: Codable {
attemptToFetchMajorUpgrade: Bool?
var blockedApplicationBundleIDs: [String]?
var disableSoftwareUpdateWorkflow,
enforceMinorUpdates: Bool?
enforceMinorUpdates,
terminateApplicationsOnLaunch: Bool?
}

// MARK: OptionalFeatures convenience initializers and mutators
Expand Down Expand Up @@ -103,7 +104,8 @@ extension OptionalFeatures {
attemptToFetchMajorUpgrade: Bool?? = nil,
blockedApplicationBundleIDs: [String]?? = nil,
disableSoftwareUpdateWorkflow: Bool?? = nil,
enforceMinorUpdates: Bool?? = nil
enforceMinorUpdates: Bool?? = nil,
terminateApplicationsOnLaunch: Bool?? = nil
) -> OptionalFeatures {
return OptionalFeatures(
acceptableApplicationBundleIDs: acceptableApplicationBundleIDs ?? self.acceptableApplicationBundleIDs,
Expand All @@ -118,7 +120,8 @@ extension OptionalFeatures {
attemptToFetchMajorUpgrade: attemptToFetchMajorUpgrade ?? self.attemptToFetchMajorUpgrade,
blockedApplicationBundleIDs: blockedApplicationBundleIDs ?? self.blockedApplicationBundleIDs,
disableSoftwareUpdateWorkflow: disableSoftwareUpdateWorkflow ?? self.disableSoftwareUpdateWorkflow,
enforceMinorUpdates: enforceMinorUpdates ?? self.enforceMinorUpdates
enforceMinorUpdates: enforceMinorUpdates ?? self.enforceMinorUpdates,
terminateApplicationsOnLaunch: terminateApplicationsOnLaunch ?? self.terminateApplicationsOnLaunch
)
}

Expand Down
38 changes: 23 additions & 15 deletions Nudge/UI/Main.swift
Expand Up @@ -43,18 +43,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
// print("applicationDidFinishLaunching")
if !nudgeLogState.afterFirstLaunch {
nudgeLogState.afterFirstLaunch = true
if NSWorkspace.shared.isActiveSpaceFullScreen() {
NSApp.hide(self)
// NSApp.windows.first?.resignKey()
// NSApp.unhideWithoutActivation()
// NSApp.deactivate()
// NSApp.unhideAllApplications(nil)
// NSApp.hideOtherApplications(self)
}
}


// Observe all notifications generated by the default NotificationCenter
// nc.addObserver(forName: nil, object: nil, queue: nil) { notification in
// print("NotificationCenter: \(notification.name.rawValue), Object: \(notification)")
Expand Down Expand Up @@ -88,12 +77,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
object: nil
)


if attemptToBlockApplicationLaunches {
registerLocal()
if !nudgeLogState.afterFirstLaunch && terminateApplicationsOnLaunch {
terminateApplications()
}
snc.addObserver(
self,
selector: #selector(terminateApplication(_:)),
selector: #selector(terminateApplicationSender(_:)),
name: NSWorkspace.didLaunchApplicationNotification,
object: nil
)
Expand All @@ -107,13 +98,30 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return $0
}
}

if !nudgeLogState.afterFirstLaunch {
nudgeLogState.afterFirstLaunch = true
if NSWorkspace.shared.isActiveSpaceFullScreen() {
NSApp.hide(self)
// NSApp.windows.first?.resignKey()
// NSApp.unhideWithoutActivation()
// NSApp.deactivate()
// NSApp.unhideAllApplications(nil)
// NSApp.hideOtherApplications(self)
}
}
}

@objc func logHiddenApplication(_ notification: Notification) {
utilsLog.info("\("Application hidden", privacy: .public)")
}

@objc func terminateApplication(_ notification: Notification) {
@objc func terminateApplicationSender(_ notification: Notification) {
utilsLog.info("\("Application launched", privacy: .public)")
terminateApplications()
}

func terminateApplications() {
if !Utils().pastRequiredInstallationDate() {
return
}
Expand Down
19 changes: 16 additions & 3 deletions Schema/jamf/com.github.macadmins.Nudge.json
Expand Up @@ -192,7 +192,20 @@
}
]
},

"disableSoftwareUpdateWorkflow": {
"description": "When disableSoftwareUpdateWorkflow is true, Nudge will not attempt to run the softwareupdate process. Defaults to false.",
"anyOf": [
{
"type": "null",
"title": "Not Configured"
},
{
"title": "Configured",
"type": "boolean",
"default": false
}
]
}
"enforceMinorUpdates": {
"description": "When enabled, Nudge will enforce minor updates. This should likely never be disabled.",
"anyOf": [
Expand All @@ -207,8 +220,8 @@
}
]
},
"disableSoftwareUpdateWorkflow": {
"description": "When disableSoftwareUpdateWorkflow is true, Nudge will not attempt to run the softwareupdate process. Defaults to false.",
"terminateApplicationsOnLaunch": {
"description": "When enabled, Nudge will terminate the applications listed in blockedApplicationBundleIDs upon initial launch.",
"anyOf": [
{
"type": "null",
Expand Down

0 comments on commit c106cb6

Please sign in to comment.