|
|
#1 |
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Use tab and newline with sed
Using a bash script I've to add (and then delete) a loginitem as first item to ~/Library/Preferences/loginwindow.plist.
I can't use defaults write array-add as it add item at the end of the array. Till now I've solved with: Code:
#!/bin/sh filepath="$HOME/Library/Preferences/loginwindow.plist" plutil -convert xml1 "$filepath" sed '/<array>/a\ <dict><key>Hide<\/key><true\/><key>Path<\/key><string>~\/PathToMyApp.app<\/string><\/dict>' "$filepath" > "$filepath"_tmp mv -f "$filepath"_tmp "$filepath" Code:
<dict> <key>AutoLaunchedApplicationDictionary</key> <array> <dict><key>Hide</key><true/><key>Path</key><string>~/PathToMyApp.app</string></dict> <dict> .... As it has to be like these: Code:
<dict> <key>AutoLaunchedApplicationDictionary</key> <array> <dict> <key>Hide</key> <true/> <key>Path</key> <string>~/P_Script.app</string> </dict> <dict> .... Code:
sed 's/<dict><key>Hide<\/key><true\/><key>Path<\/key><string>~\/PathToMyApp.app<\/string><\/dict>//' "$loginplist" > "$loginplist"_tmp mv -f "$loginplist"_tmp "$loginplist" Sorry no find clue googling or man sed. Thanks in advance, C.
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
|
|
|
|
|
#2 |
|
Hall of Famer
Join Date: Sep 2003
Location: Tokyo
Posts: 4,419
|
You don't say which version you are running, but the version of sed that comes with 10.3 doesn't support \n newline or \t tab. Install fink then install the latest sed. Type `sed --version` and if it returns "illegal option" then it's too old.
But is it necessary? XML files really don't care about neatness. All the newlines and indenting is just to make it easier for us biologicals to understand. The next time Apple's tools edit the plist they will pretty it up. Last edited by acme.mail.order; 09-22-2006 at 08:01 PM. |
|
|
|
|
|
#3 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Sorry, I forget to tell that I'm on 10.4.x
Thank, I was not sure about this, although I've noticed that it worked. But I was also interested to leran how to manage tab and newline. I've noticed also that if I use a variable for the string to append, it is not expanded. I mean if I use: Code:
mystring="<dict><key>Hide<\/key><true\/><key>Path<\/key><string>..." sed '/<array>/a\ "$mystring"' "$filepath" > "$filepath"_tmp Code:
<array> mystring .... Thanks, C.
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#4 | |||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
P.S.
Yes, I get "illegal option", but my script will run on default OS 10.4.x.
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
|||||||||||||||||||||||
|
|
|
|
|
#5 |
|
Hall of Famer
Join Date: Sep 2003
Location: Tokyo
Posts: 4,419
|
Your variable doesn't expand because it is inside single quotes, which hides it from shell expansion (but permits command expansion if the command does that kind of thing).
And you don't normally need to quote shell variables. If you are trying to edit a file in place, you can do it with perl. sed (Stream Editor) doesn't do this easily. perl -pi -e 's/search/replace/' filename Of course, if your function is wrong it can really mess things up
|
|
|
|
|
|
#6 |
|
Hall of Famer
Join Date: Sep 2003
Location: Tokyo
Posts: 4,419
|
So if you do the following:
echo 'one-two-three-four' | sed -e 's/-/\n/g' you get four separate lines, or one munged-up line? |
|
|
|
|
|
#7 | |||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Opps, that's the problem, if user change meanwhile his loginitems, the one that I've added is not removed.
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
|||||||||||||||||||||||
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Moderator
Join Date: Jan 2002
Location: Montreal
Posts: 29,448
|
For manipulation of the Login Items list from an application, Apple recommends you do this by sending AppleEvents to the System Events process as illustrated in their sample C code: http://developer.apple.com/samplecod...sAE/index.html (A 3rd-party developer has supplied a Cocoa wrapper class for this: http://www.zathras.de/angelweb/sourc...inItemRegistry) Since the System Events program apparently has an AppleScript dictionary for handling Login Items, I'm guessing that you could also do this via AppleScript.
__________________
hayne.net/macosx.html |
|||||||||||||||||||||||
|
|
|
|
|
#9 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Thanks, but sorry, I know only a little about bash script. This is my limit.
Good clue, quoting this: Code:
The core reusable library works by sending Apple events to the "System Events"
process. These Apple events exactly mirror the events that are sent when you
execute AppleScripts like:
tell application "System Events"
make new login item at end
with properties {path:"/Applications/TextEdit.app", hidden:false}
end tell
tell application "System Events"
properties of every login item
end tell
tell application "System Events"
delete login item 1
end tell
Than I'll delete it with delete login item 1
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#10 | |||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Are login items started in the order of AutoLaunchedApplicationDictionary array?
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
|||||||||||||||||||||||
|
|
|
|
|
#11 |
|
Hall of Famer
Join Date: Sep 2003
Location: Tokyo
Posts: 4,419
|
Brief experimentation should answer that question. Share the knowledge!!
|
|
|
|
|
|
#12 |
|
All Star
Join Date: May 2004
Location: london on ca
Posts: 895
|
Another option might be 'PlistBuddy', which has been included in a number of recent installer packages for Apple updates, with copies left behind in "/Library/Receipts", such as inside the recent iTunes 7 receipt. However, there is no guarantee that 'PlistBuddy' will be present on every install.
It's better than 'defaults' for manipulating nested values, but generally, I find it harder to use. For example, adding a new login item seems to require a number of steps where each property has to be created before its value can be set (there might be a way to add the whole thing at once, but I haven't been able to figure it out): Code:
/path/to/PlistBuddy -c 'add :AutoLaunchedApplicationDictionary:0 dict' ~/Library/Preferences/loginwindow.plist /path/to/PlistBuddy -c 'add :AutoLaunchedApplicationDictionary:0:Hide bool' ~/Library/Preferences/loginwindow.plist /path/to/PlistBuddy -c 'set :AutoLaunchedApplicationDictionary:0:Hide true' ~/Library/Preferences/loginwindow.plist /path/to/PlistBuddy -c 'add :AutoLaunchedApplicationDictionary:0:Path string' ~/Library/Preferences/loginwindow.plist /path/to/PlistBuddy -c 'set :AutoLaunchedApplicationDictionary:0:Path "/Applications/New.app"' ~/Library/Preferences/loginwindow.plist To delete the first item in an array, eg.: Code:
/path/to/PlistBuddy -c "delete :AutoLaunchedApplicationDictionary:0" ~/Library/Preferences/loginwindow.plist |
|
|
|
|
|
#13 | |||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
I've added only two apps (not in the dock) to loginitems and they appear in the dock in the same order as array. Reverting the order in the array also on the dock the apps are listed reversed. I think that this mean that apps are launched in array order. The only strange thing is with my custom loginitem; I've added Safari as last one, but when I re-login Safari was one position before!!!
__________________
FreeSMUG-Free/opensource Sw Mac User Group Last edited by leonida; 09-23-2006 at 06:58 PM. |
|||||||||||||||||||||||
|
|
|
|
|
#14 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Triple-A Player
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
|
Thanks, but I need a standard config. I see that in the dock submenu there is a "Open at Login" for each item; just wonder how it work.
By the moment to add and delete loginitem in my shell script I'll use: defaults write somedomain preferenceKey -array-add '{"Hide" = "True"; "Path" = "~/PathToMyApp.app";}' if loginiwindow.plist doesn't exist, or: sed '/<array>/a\ <dict><key>Hide<\/key>....' if exist, and applescript: delete login item 1 to delet it. I'd prefer a regex to delete exactly my array instead of delete 1 if something change meanwhile, but I've to find how to do it. If you wish I can be more detailed on what I've to do in this thread or new one. Thanks again.
__________________
FreeSMUG-Free/opensource Sw Mac User Group |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|