launchd 运行和隐藏应用程序

发布于 2024-10-14 20:20:17 字数 982 浏览 4 评论 0 原文

所以我有一个应用程序“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

但这要么无法运行,或者它更有可能在我的应用程序有机会初始化之前运行。

有没有一种我只是忽略的更简单的方法来做到这一点? 提前致谢。

So I have an application 'myApp', and I have a preference to load 'myApp' at login.
I have this all running fine via 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>

I would also like to give the user the option of also hiding 'myApp'

I tried creating a bash script, and adding to the ProgramArguments array in my lauchd plist:

#!/bin/sh

osascript=/usr/bin/osascript

$osascript -e 'tell application "System Events" to set visible of process "'myApp'" to false'

exit 0

but this either fails to run, or it more likely runs before my app has had a chance to initialise.

Is there an easier way to do this that I am simply overlooking?
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千年*琉璃梦 2024-10-21 20:20:17

您可以通过调用来在首选项 plist 中设置一个布尔值

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HideOnLaunch"];

当用户选择在启动时隐藏您的应用程序时,

。然后,当您的应用程序通过 launchd 启动时,您的应用程序本身可以检查 applicationDidFinishLaunching: 中的 HideOnLaunch 设置,并相应地隐藏自身:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"HideOnLaunch"]){
     [[NSApplication sharedApplication] hide:nil];
}

不要让 launchd< /code> 隐藏您的应用程序!

另一种方法如下:您可以轻松地将参数传递给 Cocoa 程序。如这个 NSUserDefaults 文档,如果你像这样启动 Cocoa 应用程序:

AnApp.app/Contents/MacOS/AnApp -FuBar YES

那么你可以通过 [[ 获取值 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

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HideOnLaunch"];

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 in applicationDidFinishLaunching:, and hide itself accordingly:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"HideOnLaunch"]){
     [[NSApplication sharedApplication] hide:nil];
}

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:

AnApp.app/Contents/MacOS/AnApp -FuBar YES

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 argument
HideOnLaunch is set.

毁虫ゝ 2024-10-21 20:20:17

谢谢佑二。

我最终得到了一个像这样的启动 plist:

<?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>/bin/sh</string>
        <string>-c</string>
        <string>/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

我将 bash 脚本作为字符串添加到 ProgramArguments 键中,就像 Apple 在以下 plist 中所做的那样:

~/Library/LaunchAgents/com.apple.FTMonitor.plist

hideOnLogin 键只能通过 launchd plist 访问,并且在 myApp 退出时重置。我有一个复选框绑定到另一个键“hideOnLoad”,当它更改时,我将启动的 plist 重写为:

/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES

/Applications/MyApp.app/Contents/MacOS/MyApp

根据情况而定。

启动时,我检查两个默认值是否都为 true,如果是,我隐藏 myApp,如下所示:
[NSApp隐藏:自我];

再次感谢您为我指明了正确的方向!

Thanks Yuji.

I ended up with a launched plist like this:

<?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>/bin/sh</string>
        <string>-c</string>
        <string>/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

I added the bash script as strings in the ProgramArguments key, as Apple does in the following plist:

~/Library/LaunchAgents/com.apple.FTMonitor.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:

/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES

or

/Applications/MyApp.app/Contents/MacOS/MyApp

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文