launchd 运行和隐藏应用程序
所以我有一个应用程序“myApp”,并且我偏好在登录时加载“myApp”。 我通过 launchd 使这一切运行良好:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.myAppDomain.myApp</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/myApp.app/Contents/MacOS/myApp</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
我还想为用户提供隐藏“myApp”的选项
我尝试创建一个 bash 脚本,并添加到我的 lauchd plist 中的 ProgramArguments 数组:
#!/bin/sh
osascript=/usr/bin/osascript
$osascript -e 'tell application "System Events" to set visible of process "'myApp'" to false'
exit 0
但这要么无法运行,或者它更有可能在我的应用程序有机会初始化之前运行。
有没有一种我只是忽略的更简单的方法来做到这一点? 提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用来在首选项 plist 中设置一个布尔值
当用户选择在启动时隐藏您的应用程序时,
。然后,当您的应用程序通过 launchd 启动时,您的应用程序本身可以检查
applicationDidFinishLaunching:
中的HideOnLaunch
设置,并相应地隐藏自身:不要让
launchd< /code> 隐藏您的应用程序!
另一种方法如下:您可以轻松地将参数传递给 Cocoa 程序。如这个
NSUserDefaults
文档,如果你像这样启动 Cocoa 应用程序:那么你可以通过
[[ 获取值
。YES
NSUserDefaults standardUserDefaults] boolForKey:@"FuBar"]因此,根据用户的偏好,您可以编写一个
launchd
plist,设置参数-HideOnLaunch YES
或-HideOnLaunch NO
。因此,在您的应用程序委托中,大概在
applicationDidFinishLaunching:
中,根据程序参数是否隐藏您的应用程序
HideOnLaunch
已设置。You can just set a bool in your preference plist by calling
when the user chooses to hide your app on launch.
Then, when your app is launched via launchd, your app itself can check the
HideOnLaunch
setting inapplicationDidFinishLaunching:
, and hide itself accordingly:Don't let
launchd
to hide your app!Another approach would be the following: You can easily pass an argument to a Cocoa program. As described in this
NSUserDefaults
document, if you launch a Cocoa app like this:Then you can get the value
YES
via[[NSUserDefaults standardUserDefaults] boolForKey:@"FuBar"]
.So, depending on the user's preference, you can write a
launchd
plist setting an argument-HideOnLaunch YES
or-HideOnLaunch NO
.So, in your app delegate, presumably in
applicationDidFinishLaunching:
, hide your app depending on whether the program argumentHideOnLaunch
is set.谢谢佑二。
我最终得到了一个像这样的启动 plist:
我将 bash 脚本作为字符串添加到 ProgramArguments 键中,就像 Apple 在以下 plist 中所做的那样:
hideOnLogin 键只能通过 launchd plist 访问,并且在 myApp 退出时重置。我有一个复选框绑定到另一个键“hideOnLoad”,当它更改时,我将启动的 plist 重写为:
或
根据情况而定。
启动时,我检查两个默认值是否都为 true,如果是,我隐藏 myApp,如下所示:
[NSApp隐藏:自我];
再次感谢您为我指明了正确的方向!
Thanks Yuji.
I ended up with a launched plist like this:
I added the bash script as strings in the ProgramArguments key, as Apple does in the following plist:
the hideOnLogin key is only accessible through the launchd plist, and is reset when myApp is quit. I have a checkbox bound to another key "hideOnLoad", and when this is changed, I rewrite the launched plist to either:
or
depending on the circumstances.
On startup I then check to see if both defaults are true, if they are, I hide myApp, like so:
[NSApp hide:self];
thanks again for pointing me in the right direction!