bash 在 Package Maker 安装程序 postflight 脚本期间将应用程序添加到 OSX“登录项”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6947925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add app to OSX "Login Items" during a Package Maker installer postflight script
提问by blak3r
I need a way to add an application to the Login Items from a postflight script which is run as part of my installer. It needs to work on 10.5+. Preferably, it would work in a bash script. My application already requires administrative rights.
我需要一种方法来将应用程序从作为我的安装程序的一部分运行的 postflight 脚本添加到登录项。它需要在 10.5+ 上运行。最好,它可以在 bash 脚本中工作。我的应用程序已经需要管理权限。
The approach I found here: Mac OS Login Items with Arguments?seemed to be on the right track (included below)... but didn't work when I tried it on command line and I'm not sure how to make it install for All Users or if I need to add logic to check if it's already added to startup items before calling this code.
我在这里找到的方法:Mac OS Login Items with Arguments? 似乎在正确的轨道上(包括在下面)......但是当我在命令行上尝试它时没有用,我不知道如何为所有用户安装它,或者我是否需要添加逻辑来检查是否在调用此代码之前,它已经添加到启动项中。
#!/bin/bash
/usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"\", hidden:false } at end"
I suspect I could also do something with a launchd. But, I'm not sure which approach is the best practice for compatibility across versions.
我怀疑我也可以用launchd做一些事情。但是,我不确定哪种方法是跨版本兼容性的最佳实践。
NOTE: I do NOT want to add it using some objective-c code inside my app. I need the installer to add it.Currently, what I do is start the application after install which then Adds it to Login Items in code using the LSSharedFileListRef... Example of that approach can be found here: How do you make your App open at login?. The reason this is not ok is I need to make my application install with Apple Remote Desktop via command line, when on the login screen. So, the application needs to not start automatically after install.
注意:我不想在我的应用程序中使用一些objective-c 代码添加它。我需要安装程序来添加它。目前,我所做的是在安装后启动应用程序,然后使用 LSSharedFileListRef 将其添加到代码中的登录项...可以在此处找到该方法的示例:如何在登录时打开您的应用程序?. 这不行的原因是我需要在登录屏幕上通过命令行使用 Apple Remote Desktop 安装我的应用程序。因此,应用程序在安装后不需要自动启动。
回答by blak3r
Here's the options I investigated and experimented with:
以下是我调查和试验的选项:
Option 1: Use Login Items
选项 1:使用登录项
This is the method I used. It's very easy to do from a bash file by adding the following line to your postflight.
这是我使用的方法。通过将以下行添加到 postflight,从 bash 文件很容易做到。
defaults write /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add '{Path="/Applications/Your Application.app";}'
Note: You don't even have to worry about adding duplicates if you reinstall the application. The loginwindow process removes duplicates when it reads them.
注意:如果您重新安装应用程序,您甚至不必担心添加重复项。loginwindow 进程在读取重复项时会删除它们。
I tested this on 10.5, 10.6, and 10.7
@noa says this doesn't work on mountain lion (10.8), Haven't personally confirmed.
我在10.5、10.6和 10.7 上对此进行了测试,
@noa 说这对山狮 (10.8) 不起作用,尚未亲自确认。
Option 2: LaunchAgent
选项 2:LaunchAgent
The unique ramifications of using a Launch Agent are:
使用 Launch Agent 的独特后果是:
- Your application doesn't appear in the Login Items list, so the user really has to know what they're doing to get rid of it
- The user cannot end your applications process without running: launchctl unload /Library/LaunchAgents/com.your.package.plist
- 您的应用程序没有出现在登录项列表中,因此用户真的必须知道他们在做什么才能摆脱它
- 用户不能在不运行的情况下结束您的应用程序进程:launchctl unload /Library/LaunchAgents/com.your.package.plist
Here's some code you could use to create the launch agent in your bash file:
以下是一些可用于在 bash 文件中创建启动代理的代码:
cat > /Library/LaunchAgents/com.your.application.agent.plist << EOT
<?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.your.application.agent</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Your Application.app/Contents/MacOS/Your Application</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOT
Option 3: Compile Obj-c code to a binary
选项 3:将 Obj-c 代码编译为二进制文件
I never actually finished this approach. Apparently, this is the approach that Novell takes. Essentially you'd make a foundation application that calls the libraries referenced from this solution: How do you make your App open at login?
我从未真正完成过这种方法。显然,这是 Novell 采取的方法。本质上,您将创建一个基础应用程序,该应用程序调用从此解决方案引用的库: 如何在登录时打开您的应用程序?
Other
其他
Didn't try this but according to this post if you want it to work on tiger you need to use AppleScript..? I can't confirm or deny that but thought this link might be relevant. Editing Mac OS X login items in Objective-C through AppleScript
没有尝试过这个,但是根据这篇文章,如果你想让它在老虎上工作,你需要使用 AppleScript ......?我无法确认或否认这一点,但认为此链接可能是相关的。 通过 AppleScript 在 Objective-C 中编辑 Mac OS X 登录项
回答by w00t
There are two ways you can start your program at login time:
您可以通过两种方式在登录时启动程序:
- Use login items
- Set up a LaunchAgent
- 使用登录项
- 设置 LaunchAgent
The LaunchAgent is simplest, all you need is a .plist file that tells launchd to load your program, and then place that file in /Library/LaunchAgents (as part of the install package).
LaunchAgent 是最简单的,您只需要一个 .plist 文件,它告诉 launchd 加载您的程序,然后将该文件放在 /Library/LaunchAgents(作为安装包的一部分)。
Login items are a bit of a pain and it's per-user.
登录项有点麻烦,而且是针对每个用户的。

