Adding Items to Login Items From the CLI
来源:互联网 发布:腾讯云域名备案幕布 编辑:程序博客网 时间:2024/06/05 03:04
http://www.macgeekery.com/tips/cli/adding_items_to_login_items_from_the_cli
Adding Items to Login Items From the CLI
- CLI
- 10.3
- 10.4
- Church of the Bearded Unix God
It’s a question we get every now and again that simply doesn’t have an easy answer: “How do I add items to the Login Items from the Terminal?”
Well, that’s hard, and mainly because the defaults
command is limited in that it cannot nest collections and the folks that made the plist for loginwindow
decided on an array of dictionaries, thus tossing that command out the window as a solution.
$ defaults write foo test -array-add -dict Hide 1 Path /Applications/TextEdit.app
2007-04-09 20:44:42.659 defaults[17951] Cannot nest composite types (arrays and dictionaries); exiting
So I poked around at how applications do it when adding daemons and such and it turns out they do it by one of two methods. The first is the obvious one: they edit the loginwindow
plist and add in the item. It’s just a dictionary with two keys and it’s only written when changes are made in the Accounts preference pane, so it’s no big deal to edit outside of that (unlike the Dock’s preferences).
Now the second one is kind of fun, and quite open to CLI scripting. You can talk to the System Events program via AppleScript and have it mangle the property list for you. You can even get a list of items and replace them if you so choose. Well, AppleScript can be accomplished in Terminal rather easily, so let’s take a look at a script that does what we want so we can make a reduction:
tellapplication “System Events” tomake new login item with properties {path: “/Applications/TextEdit.app”, hidden:false} atend
Voila! We have a new Login Item. (Go ahead and run this and then check the Login Items pane of the Accounts preferences.) So, now it needs to run in Terminal. Well, osascript
will do this:
/usr/bin/osascript -e 'tell application "System Events" to make new login item with properties { path: "/Applications/TextEdit.app", hidden:false } at end'
That works great. The only caveat being that it’s a PITA to type. So, let’s make a simple shell command out of it:
#!/bin/bash/usr/bin/osascript -e "tell application /"System Events/" to make new login item with properties { path: /"$1/", hidden:false } at end"
Because it’s expecting a POSIX path anyway, you don’t need to bother with a conversion to a file reference or alias or whatever. Just use a plain old Unixy string as the argument and it works. Also, be aware that the quotes had to change so that the shell would expand the $1 variable.
Now, presuming you saved it as addloginitem
somewhere in your PATH
, you can just run:
$ addloginitem /Applications/TextEdit.applogin item TextEdit
Ta da.
This does require a user to be logged in and will only change the preferences for that user. If you want something different then you’re hacking up plists.
Going From Here
You can play around with this in Script Editor a little, too. This should give you some other ideas of the fun to be had.
tell application "System Events"make new login item with properties {path:"/Applications/TextEdit.app", hidden:false} at login item 2end tell
tell application "System Events"set theItem to every login item whose path contains "TextEdit"delete theItem
- Adding Items to Login Items From the CLI
- Adding new items to the Checklist
- Adding Login Items (OS X)
- dynamically load more items to the ListView
- MacOsx Running items at login
- Rest Api how to retrieve list items from SharePoint Online
- Adding items in magento stoped working
- Adding List items into a List Folder
- Adding Menu Items Based on an Intent
- BHO开发浏览器插件-Adding Menu Items
- Altium Designer 出现错误提示(警告)adding items to hidden net GND/VCC
- We need other items to prevent the fur packs
- How to change the text color on UINavigationBar button items
- SharePoint 2013 REST Service how to Retrive The List Items
- Mac OS X: Running items at login
- How to: Retrieve List Items
- RadComboBox: How to Sort Items
- how to get internal items from a dialog created from wizard of VC2005
- 从JavaScript思考网页运行原理
- exe注册成windows服务
- jquery win7弹出窗口
- 把一个进程内组件以进程外组件的形式调用引发的问题
- UNIX/LINUX 平台可执行文件格式分析(转)
- Adding Items to Login Items From the CLI
- 制作自己的ip库需要用到工具IPLook.exe
- 如何做好项目沟通管理的工作
- 如何把一个空的 Console VC 工程改造成 Win MFC 工程
- Installshield安装软件
- 依赖注入获得一个对象却想返回不同的值(Error)
- 用户crontab -e设定的脚本文件位置
- 制作自己的ip库
- 制作自己的ip库