Skip to content

Commit

Permalink
Amend "launch at login" logic
Browse files Browse the repository at this point in the history
Use LaunchAtLogin package
  • Loading branch information
Reeywhaar committed Jun 19, 2022
1 parent 08c1bcf commit cc3e1a1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 88 deletions.
45 changes: 30 additions & 15 deletions MiddleMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
462ACF873765FF8340B5BA99 /* Pods_MiddleMe_MiddleMeUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D5266272282338629CE9A05 /* Pods_MiddleMe_MiddleMeUITests.framework */; };
89FE7C16169BDA1C79D75267 /* Pods_MiddleMeTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C1B74FC90C5BE7FD4343F065 /* Pods_MiddleMeTests.framework */; };
932E88975982F977FC9B9E97 /* Pods_MiddleMe.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6ED3EC866C0990A9CD0B12B /* Pods_MiddleMe.framework */; };
F3364A3A285F82F20004B4D2 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = F3364A39285F82F20004B4D2 /* LaunchAtLogin */; };
F3663A7A2856A362001BD4F3 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F3663A7C2856A362001BD4F3 /* Localizable.strings */; };
F372A4D828417C3100C7BCA7 /* LaunchAgent+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F372A4D728417C3100C7BCA7 /* LaunchAgent+Extensions.swift */; };
F388F6CA2841650A00DA8F11 /* LaunchAgent in Frameworks */ = {isa = PBXBuildFile; productRef = F388F6C92841650A00DA8F11 /* LaunchAgent */; };
F3CE2CAA283FFCB30081B57D /* MiddleMeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CE2CA9283FFCB30081B57D /* MiddleMeApp.swift */; };
F3CE2CAC283FFCB30081B57D /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CE2CAB283FFCB30081B57D /* ContentView.swift */; };
F3CE2CAE283FFCB40081B57D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F3CE2CAD283FFCB40081B57D /* Assets.xcassets */; };
Expand Down Expand Up @@ -55,7 +54,6 @@
F303CA8E28403A61002B603D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
F3663A7B2856A362001BD4F3 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
F3663A7D2856A36E001BD4F3 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = "<group>"; };
F372A4D728417C3100C7BCA7 /* LaunchAgent+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LaunchAgent+Extensions.swift"; sourceTree = "<group>"; };
F3CE2CA6283FFCB30081B57D /* MiddleMe.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MiddleMe.app; sourceTree = BUILT_PRODUCTS_DIR; };
F3CE2CA9283FFCB30081B57D /* MiddleMeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MiddleMeApp.swift; sourceTree = "<group>"; };
F3CE2CAB283FFCB30081B57D /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand All @@ -77,8 +75,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
F3364A3A285F82F20004B4D2 /* LaunchAtLogin in Frameworks */,
932E88975982F977FC9B9E97 /* Pods_MiddleMe.framework in Frameworks */,
F388F6CA2841650A00DA8F11 /* LaunchAgent in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -152,7 +150,6 @@
children = (
F303CA8E28403A61002B603D /* Info.plist */,
F3CE2CA9283FFCB30081B57D /* MiddleMeApp.swift */,
F372A4D728417C3100C7BCA7 /* LaunchAgent+Extensions.swift */,
F3CE2CD828400E250081B57D /* GlobalEventMonitor.swift */,
F3CE2CD628400C7B0081B57D /* Settings.swift */,
F3CE2CD4283FFFE40081B57D /* Box.swift */,
Expand Down Expand Up @@ -200,6 +197,7 @@
F3CE2CA2283FFCB30081B57D /* Sources */,
F3CE2CA3283FFCB30081B57D /* Frameworks */,
F3CE2CA4283FFCB30081B57D /* Resources */,
F3364A3B285F832E0004B4D2 /* Launch at login helper */,
C69B2D432C63391AB208D63E /* [CP] Embed Pods Frameworks */,
);
buildRules = (
Expand All @@ -208,7 +206,7 @@
);
name = MiddleMe;
packageProductDependencies = (
F388F6C92841650A00DA8F11 /* LaunchAgent */,
F3364A39285F82F20004B4D2 /* LaunchAtLogin */,
);
productName = MiddleMe;
productReference = F3CE2CA6283FFCB30081B57D /* MiddleMe.app */;
Expand Down Expand Up @@ -287,7 +285,7 @@
);
mainGroup = F3CE2C9D283FFCB30081B57D;
packageReferences = (
F388F6C82841650A00DA8F11 /* XCRemoteSwiftPackageReference "LaunchAgent" */,
F3364A38285F82F20004B4D2 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */,
);
productRefGroup = F3CE2CA7283FFCB30081B57D /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -428,14 +426,31 @@
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-MiddleMe-MiddleMeUITests/Pods-MiddleMe-MiddleMeUITests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
F3364A3B285F832E0004B4D2 /* Launch at login helper */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Launch at login helper";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Type a script or drag a script file from your workspace to insert its path.\n\"${BUILT_PRODUCTS_DIR}/LaunchAtLogin_LaunchAtLogin.bundle/Contents/Resources/copy-helper-swiftpm.sh\"\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
F3CE2CA2283FFCB30081B57D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F372A4D828417C3100C7BCA7 /* LaunchAgent+Extensions.swift in Sources */,
F3CE2CD5283FFFE40081B57D /* Box.swift in Sources */,
F3CE2CD928400E250081B57D /* GlobalEventMonitor.swift in Sources */,
F3CE2CAC283FFCB30081B57D /* ContentView.swift in Sources */,
Expand Down Expand Up @@ -782,21 +797,21 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
F388F6C82841650A00DA8F11 /* XCRemoteSwiftPackageReference "LaunchAgent" */ = {
F3364A38285F82F20004B4D2 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/emorydunn/LaunchAgent";
repositoryURL = "https://github.com/sindresorhus/LaunchAtLogin";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.3.0;
branch = v4.2.0;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
F388F6C92841650A00DA8F11 /* LaunchAgent */ = {
F3364A39285F82F20004B4D2 /* LaunchAtLogin */ = {
isa = XCSwiftPackageProductDependency;
package = F388F6C82841650A00DA8F11 /* XCRemoteSwiftPackageReference "LaunchAgent" */;
productName = LaunchAgent;
package = F3364A38285F82F20004B4D2 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */;
productName = LaunchAtLogin;
};
/* End XCSwiftPackageProductDependency section */
};
Expand Down
8 changes: 4 additions & 4 deletions MiddleMe.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"pins" : [
{
"identity" : "launchagent",
"identity" : "launchatlogin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/emorydunn/LaunchAgent",
"location" : "https://github.com/sindresorhus/LaunchAtLogin",
"state" : {
"revision" : "9f6d33abfa4a0a45a877e9b884391981c1f4152b",
"version" : "0.3.0"
"branch" : "v4.2.0",
"revision" : "e8171b3e38a2816f579f58f3dac1522aa39efe41"
}
}
],
Expand Down
2 changes: 2 additions & 0 deletions MiddleMe/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<dict>
<key>LSUIElement</key>
<true/>
<key>NSSupportsSuddenTermination</key>
<true/>
</dict>
</plist>
30 changes: 0 additions & 30 deletions MiddleMe/LaunchAgent+Extensions.swift

This file was deleted.

42 changes: 3 additions & 39 deletions MiddleMe/MiddleMeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import SwiftUI
import LaunchAgent
import LaunchAtLogin

@main
struct MiddleMeApp: App {
Expand All @@ -19,13 +19,7 @@ struct MiddleMeApp: App {
handler.value?.start()
handler.value?.register()

do {
let agent = try getAgent()
try LaunchControl.shared.write(agent)
LaunchControl.shared.enable(agent)
} catch {
print("Unexpected error:", error)
}
LaunchAtLogin.isEnabled = true

NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: .main, using: self.handleTerminate)
}
Expand All @@ -45,37 +39,7 @@ struct MiddleMeApp: App {
}

private func handleTerminate(notification: Notification) {
let agent = try? getAgent()

if let agent = agent {
LaunchControl.shared.disable(agent)
LaunchControl.shared.remove(agent)
}

do {
if let url = agent?.url {
try FileManager.default.removeItem(at: url)
}
} catch {
print("Unexpected error:", error)
}
}

private func getAgent() throws -> LaunchAgent {
guard let bundleId = Bundle.main.bundleIdentifier else { throw AgentError.noId }
guard let path = Bundle.main.executablePath else { throw AgentError.noPath }
let agent = LaunchAgent(label: bundleId, program: path )
agent.runAtLoad = true
agent.processType = .interactive

agent.url = getAgentsDirectory().appendingPathComponent("\(agent.label).plist")
return agent
}

private func getAgentsDirectory() -> URL {
var url = try! FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
url.appendPathComponent("LaunchAgents")
return url
LaunchAtLogin.isEnabled = false
}
}

Expand Down

0 comments on commit cc3e1a1

Please sign in to comment.