Xamarin MacOS 应用程序在登录时启动
Xamarin MacOS app launch at login
我正在尝试让我的 Xamarin MacOS 应用程序在登录时达到 运行。环顾四周,我发现只有很老的主题,没有解决方案。
有什么解决办法吗?可行吗?
我的应用程序 运行s,默认具有管理员权限
谢谢
这篇文章是我前段时间写的。它应该仍然有效 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);
}
此方法也适用于 "non sandboxed" 应用。我不太喜欢它,但目前它正在运行:
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);
}
}
我正在尝试让我的 Xamarin MacOS 应用程序在登录时达到 运行。环顾四周,我发现只有很老的主题,没有解决方案。
有什么解决办法吗?可行吗?
我的应用程序 运行s,默认具有管理员权限
谢谢
这篇文章是我前段时间写的。它应该仍然有效 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);
}
此方法也适用于 "non sandboxed" 应用。我不太喜欢它,但目前它正在运行:
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);
}
}