Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-22-2006, 07:51 PM   #1
leonida
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"
And loginwindow.plist file became like this:
Code:
<dict>
	<key>AutoLaunchedApplicationDictionary</key>
	<array>
<dict><key>Hide</key><true/><key>Path</key><string>~/PathToMyApp.app</string></dict>		<dict>
....
How can I have dict strings appended with correct new line and tabs?
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>
		....
Same problem then get the same pattern with regex to delete it. Now I'm using this to remove dict I've appended:
Code:
sed 's/<dict><key>Hide<\/key><true\/><key>Path<\/key><string>~\/PathToMyApp.app<\/string><\/dict>//' "$loginplist" > "$loginplist"_tmp
mv -f "$loginplist"_tmp "$loginplist"
I've tryed all kind of \\n, /\n, \\t,/\t, \\r, \015...
Sorry no find clue googling or man sed.

Thanks in advance, C.
__________________
FreeSMUG-Free/opensource Sw Mac User Group
leonida is offline   Reply With Quote
Old 09-22-2006, 07:56 PM   #2
acme.mail.order
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.
acme.mail.order is online now   Reply With Quote
Old 09-22-2006, 08:14 PM   #3
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by acme.mail.order
You don't say which version you are running,

Sorry, I forget to tell that I'm on 10.4.x

Quote:
Originally Posted by acme.mail.order
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.

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
I get:
Code:
<array>
mystring
....
I've seen a lot of examples using variables but I don't understand what is wrong.
Thanks, C.
__________________
FreeSMUG-Free/opensource Sw Mac User Group
leonida is offline   Reply With Quote
Old 09-22-2006, 08:17 PM   #4
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
P.S.
Quote:
Originally Posted by acme.mail.order
Type `sed --version` and if it returns "illegal option" then it's too old.

Yes, I get "illegal option", but my script will run on default OS 10.4.x.
__________________
FreeSMUG-Free/opensource Sw Mac User Group
leonida is offline   Reply With Quote
Old 09-22-2006, 08:20 PM   #5
acme.mail.order
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
acme.mail.order is online now   Reply With Quote
Old 09-22-2006, 08:22 PM   #6
acme.mail.order
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?
acme.mail.order is online now   Reply With Quote
Old 09-22-2006, 08:37 PM   #7
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by acme.mail.order
The next time Apple's tools edit the plist they will pretty it up.

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
leonida is offline   Reply With Quote
Old 09-22-2006, 08:37 PM   #8
hayne
Moderator
 
Join Date: Jan 2002
Location: Montreal
Posts: 29,448
Quote:
Originally Posted by leonida
Using a bash script I've to add (and then delete) a loginitem as first item to ~/Library/Preferences/loginwindow.plist.

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
hayne is offline   Reply With Quote
Old 09-23-2006, 04:10 AM   #9
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by hayne
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)

Thanks, but sorry, I know only a little about bash script. This is my limit.

Quote:
Originally Posted by hayne
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.

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
Now how to make new login item at begin and not to the end? Is begin the contrary of end?

Than I'll delete it with delete login item 1
__________________
FreeSMUG-Free/opensource Sw Mac User Group
leonida is offline   Reply With Quote
Old 09-23-2006, 05:31 AM   #10
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by leonida
Now how to make new login item at begin and not to the end? Is begin the contrary of end?

Are login items started in the order of AutoLaunchedApplicationDictionary array?
__________________
FreeSMUG-Free/opensource Sw Mac User Group
leonida is offline   Reply With Quote
Old 09-23-2006, 07:45 AM   #11
acme.mail.order
Hall of Famer
 
Join Date: Sep 2003
Location: Tokyo
Posts: 4,419
Brief experimentation should answer that question. Share the knowledge!!
acme.mail.order is online now   Reply With Quote
Old 09-23-2006, 09:59 AM   #12
biovizier
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
Items can be added by position with numbering starting at "0", hence the ":0" in the examples adds the new item at the beginning of the list.

To delete the first item in an array, eg.:
Code:
/path/to/PlistBuddy -c "delete :AutoLaunchedApplicationDictionary:0" ~/Library/Preferences/loginwindow.plist
Try '/path/to/PlistBuddy -h' for details.
biovizier is offline   Reply With Quote
Old 09-23-2006, 06:56 PM   #13
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by acme.mail.order
Brief experimentation should answer that question. Share the knowledge!!

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.
leonida is offline   Reply With Quote
Old 09-25-2006, 03:10 AM   #14
leonida
Triple-A Player
 
Join Date: Apr 2004
Location: Milan, Italy
Posts: 72
Quote:
Originally Posted by biovizier
However, there is no guarantee that 'PlistBuddy' will be present on every install.

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.

Quote:
Originally Posted by biovizier
It's better than 'defaults' for manipulating nested values, but generally, I find it harder to use.

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
leonida is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:05 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Site design © Mac Publishing LLC; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of Mac Publishing LLC.