简体   繁体   English

从主应用程序启动Mac OS X(LoginItem)帮助应用程序

[英]Launching a Mac OS X (LoginItem) helper app from the main application

I have an application Main.app and an application Helper.app within the Main.app/Library/LoginItems folder. 我在Main.app/Library/LoginItems文件夹中有一个应用程序Main.app和一个应用程序Helper.app The Main.app can be started by the user, and the user can then set Helper.app as login item. Main.app可以由用户启动,然后用户可以将Helper.app设置为登录项。 (using SMLoginItemSetEnabled()), this works perfectly. (使用SMLoginItemSetEnabled()),这非常有效。 When the user sets the Helper.app as login item it is also launched. 当用户将Helper.app设置为登录项时,它也会启动。

However, it should also be possible to launch the Helper.app without setting it as a login item. 但是,也可以在不将其设置为登录项的情况下启动Helper.app。 I've tried to do this with 我试过这样做

[[NSWorkspace sharedWorkspace] launchApplication: newPath];

which gives a lsboxd: Not allowing process to launch... error, and with 这给了一个lsboxd:不允许进程启动...错误,并与

[NSTask launchedTaskWithLaunchPath:newPath arguments: [NSArray array]];

wich gives a deny forbidden-sandbox-reinit error. 这给了一个拒绝禁止沙盒重新启动错误。 (presumably because the Helper.app is inside Main.app?) (大概是因为Helper.app Main.app 里面 ?)

newPath is found as follows: newPath的发现如下:

NSMutableArray *pathComponents = [NSMutableArray arrayWithArray:[[[NSBundle mainBundle] bundlePath] pathComponents]];
[pathComponents addObject:@"Contents"];
[pathComponents addObject:@"Library"];
[pathComponents addObject:@"LoginItems"];
[pathComponents addObject:@"Helper.app"];
[pathComponents addObject:@"Contents"];
[pathComponents addObject:@"MacOS"];
[pathComponents addObject:@"Helper"];
NSString *newPath = [NSString pathWithComponents:pathComponents];

How can i fix this? 我怎样才能解决这个问题? :) :)

Thanks, 谢谢,

You also can use NSTask and launchctl. 您还可以使用NSTask和launchctl。 Example Or you can use NSTask and bash script. 示例或者您可以使用NSTask和bash脚本。 Example: 例:

#Run Menu
if [ $(ps -u $USERNAME | grep -v grep | grep -m1 '/Library/Menu/Menu.app/Contents/MacOS/Menu' | awk '{print $5}') ]; then 
    echo "Menu already  running";
else 
    echo "Menu not  running"
    if [ $(users $USERNAME ) == $USERNAME ]; then
        echo "User logined"
        echo "running menu for user $USERNAME"
        sudo -u $USERNAME /Library/Menu/Menu.app/Contents/MacOS/Menu&
    else
        echo "User not logined"
    fi
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM