The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Help with the Defaults command (http://hintsforums.macworld.com/showthread.php?t=35939)

bramley 03-04-2005 06:32 PM

Quote:

Originally Posted by hayne
I strongly recommend against implementing the above "hint". It is extremely unsafe for the reasons explained in the comments to the hint - e.g. that of 'bbum'

Uhh! Fair point. When I first read one of today's hints which linked to the one above, I read about sudoers in my 'bumper book of interesting UNIX facts' and 'man sudoers' which mentioned the use of the NOPASSWD tag to permit the use of selected commands, which Biovizier seems to have pointed out. I rashly assumed the above hint discussed NOPASSWD tags, but clearly it just disables all security everything.

Quote:

Originally Posted by MBHockey
Hopefully this is something that Automator can accomplish come Tiger.

[mock rant]
Why does everybody keep saying this? I don't know but I'm prepared to bet Automator is just a more GUIfied version for the much vaunted 'recordable' scripting mechanism that came out with 10.1. It will rely on 3rd party developers (and Apple) putting in as much effort in getting their products to 'automate' as they already have with recordable scripting support - in other words, none at all.
[/mock rant]

You could avoid using sudoers or opening Terminal at all with an Applescript and 'do shell script.' Yes, you still need your password, but you can script KeyChain Access to give this out. I'm trying to write an example to show what I mean but I'll have to post it sometime over the weekend.

[EDIT Ummm - I have managed to get this to work but have noticed a problem with KeyChain Access so probably better if I keep quiet for now]

saint.duo 03-05-2005 11:51 PM

found it
 
That friend of mine replied.

Here is the applescript I made for him to put in his scripts menu and change his display sleep time. Replace $MYADMINPASS with your admin password.

Code:

do shell script "sudo pmset dim 1" password "$MYADMINPASS" with administrator privileges
Works great for him. He has a 15 minute and a 1 minute one.

Hal Itosis 06-19-2005 07:29 AM

Quote:

Originally Posted by bramley
Coming into this from a different direction, you can avoid playing around with the plists (which I'm not certain will work anyway) by using the 'pmset' utility. Type 'man pmset' for more info.

(3 months later... guess we're running Tiger now).

Reading up on pmset, I found an interesting [optional] argument
called force. In tests I ran, it seems that we can totally avoid
the whole sudo mess... because --when "force" is deployed -- pmset
doesn't write to the plist, but rather directly to some location (RAM,
VM, register, whatever) where those variables are actively used.

From the man page:
Quote:

force - tells PM to immediately activate these settings. Does not write
them to disk, and the settings may easily be overwritten. Useful in cir-
cumstances where PM's configd plugin happens not to be running.

My display sleep is normally set to 30 minutes. But with a simple script...
Code:

#!/bin/bash
/usr/bin/pmset -a force displaysleep 1
exit 0

...I can go to black in 1 minute. No sudo, nada.

FWIW,

-HI-

elmimmo 08-03-2005 05:51 PM

Quote:

Originally Posted by biovizier
Using '-dict-add' will add a nested value, or modify an existing one as described above.

I see that this is true for something at tree level 2 (being root, Level0). That is, it keeps all other dicts in level 1 and other dicts at level 2 intact, and updates/overwrites this level 2 dict with specified keys and their values.

However, what if there where more nested dicts in the level 2 dict that we are editing. I have seen that even using '-dict-add' will wipe out any dict at level 3 branching from the level 2 dict we are editing.

In other words, how do you edit the value of the key 'Date' of dictionary 'Level3A' from '2005-07-18T21:53:04Z' to '2005-08-3T23:50:04Z' in:
Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Level1</key>
        <dict>
                <key>Level2</key>
                <dict>
                        <key>Level3a</key>
                        <dict>
                                <key>Date</key>
                                <string>2005-07-18T21:53:04Z</string>
                                <key>Value</key>
                                <string>booyah</string>
                        </dict>
                        <key>Level3b</key>
                        <dict>
                                <key>Date</key>
                                <string>2005-07-19T22:00:00Z</string>
                                <key>Value</key>
                                <string>hello</string>
                        </dict>
                </dict>
        </dict>
</dict>
</plist>


biovizier 08-03-2005 10:58 PM

I don't think deeply nested values can be edited using 'defaults' alone (I would be interested if there is a way), so workarounds will vary from situation to situation...

The example in your post is fairly straightforward, since there is only one 'Level2' entry, and all of the entries are of the string "type". There are probably easier ways, but you could for example, take the ascii formatted output of the 'defaults read' command (in which key-value pairs are conveniently placed together into lines), and process it with 'sed' or something. eg.
Code:

newdate="2005-08-3T23:50:04Z"
newL1="$(defaults read the.property.list Level1 | sed "s/\(Level3a[^\"]*\"\)[^\"]*/\1${newdate}/")"
defaults write the.property.list Level1 "$newL1"

I mentioned all of the entries being of type=string, because this process will convert everything into strings - eg. <true/> becomes <string>1</string>, or <integer>17</integer> gets changed to <string>17</string>, etc. Many programmes don't seem to care, but I've run into a few that crash so watch out.

Anyway, at this point, it becomes more of a question about "how to parse text" rather than use of the 'defaults' command...

elmimmo 08-04-2005 02:56 AM

The man page for defaults seems to explain how to set the value types of new values, choosing from string, data, int, float, bool, date…

I can see that there are sideways like editing ASCII plist, but my intention was to edit a plist from a shell script, and Tiger seems to be full of binary plists. The command plutil seems to be able to convert from binary to xml, but not to ASCII.

Maybe I can use sed, too, or other command to go through an ASCII file with line breaks, but I will have to look into that, and my shell skills are limted…


All times are GMT -5. The time now is 06:22 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.