我试图让我的Xamarin MacOS应用程序在登录时运行。环顾四周,我发现只有很老的主题没有解决方案。
有没有可用的解决方案?可行吗?
我的应用程序默认运行,具有管理员权限
由于
答案 0 :(得分:2)
我前段时间写过这篇文章。它应该仍然有效https://shamsutdinov.net/2016/09/27/how-to-launch-at-login-your-xamarin-mac-sandboxed-application/
TL;博士;
public class StartAtLoginOption
{
[DllImport("/System/Library/Frameworks/ServiceManagement.framework/ServiceManagement")]
static extern bool SMLoginItemSetEnabled(IntPtr aId, bool aEnabled);
public static bool StartAtLogin(bool value)
{
CoreFoundation.CFString id = new CoreFoundation.CFString("my.helper.app.bundle.id");
return SMLoginItemSetEnabled(id.Handle, value);
}
}
从帮助应用程序启动您的主应用程序:
public override void DidFinishLaunching(NSNotification notification)
{
if (!NSWorkspace.SharedWorkspace.RunningApplications.Any(a => a.BundleIdentifier == "my.main.app.bundle.id"))
{
var path = new NSString(NSBundle.MainBundle.BundlePath)
.DeleteLastPathComponent()
.DeleteLastPathComponent()
.DeleteLastPathComponent()
.DeleteLastPathComponent();
var pathToExecutable = path + @"Contents/MacOS/LoginItemTestMain";
if (NSWorkspace.SharedWorkspace.LaunchApplication(pathToExecutable)) { }
else NSWorkspace.SharedWorkspace.LaunchApplication(path);
}
NSApplication.SharedApplication.Terminate(this);
}
答案 1 :(得分:0)
此方法也适用于“非沙盒”应用。我真的不喜欢它,但目前它正在发挥作用:
public void SetAtLogin()
{
//Checking if the app is in the login items or not
var script = "tell application \"System Events\"\n get the name of every login item\n if login item \"AppNameTest\" exists then\n return true\n else\n return false\n end if\n end tell";
NSAppleScript appleScript = new NSAppleScript(script);
var errors = new NSDictionary();
NSAppleEventDescriptor result = appleScript.ExecuteAndReturnError(out errors);
var isLoginItem = result.BooleanValue;
if (!isLoginItem)
{
NSAppleScript login;
//AppleScript to add app to login items
script = "tell application \"System Events\"\n make new login item at end of login items with properties {name: \"AppNameTest\", path:\"/Applications/DayOne.app\", hidden:false}\n end tell";
login = new NSAppleScript(script);
var resul = login.ExecuteAndReturnError(out errors);
}
}