Editing a .plist file (Safari)

Hello All,

I am at a total loss when it comes to making do shell scripts in Applescript. I can write applescripts and i can write shell scripts but when it comes to combining them I’m a little confused. Is it possible to get this into an applescript with a do shell script? I know my variables would have to be set in applescript. Here is the Shell I built:


#!/bin/sh
SAFARIPLIST=~/Desktop/com.apple.Safari.plist
NEWSAFARIPLIST=~/Desktop/newSafariPlist
COUNTER=0

cat $SAFARIPLIST | while read FN
do
if [ "${FN}" == "<key>AppleNavServices:PutFile:0:Path</key>" ] ; then
echo "${FN}" >>$NEWSAFARIPLIST
echo "<string>file://newVARIABLE</string>" >>$NEWSAFARIPLIST
let COUNTER=$COUNTER+1
elif [ $COUNTER == 1 ] ; then
let COUNTER=$COUNTER+1
else
echo "${FN}" >>$NEWSAFARIPLIST
fi
done

If anyone can direct me to a site to better learn how to do this, please let me know. Also this shell script is replacing one line of code in the com.apple.safari.plist file. Thanks in advance!!!

Well, you could just save the shell script as a file and invoke it using a do shell script.

But if all you want to do is edit a plist, isn’t there a MacOS X CLI tool to do just that? Can’t recall the command offhand.

And be all that as it may, i suspect you can reduce your shell script down to a perl one-line command (which could easily be dropped into a simple, relatively easy to construct do shell script command). Are you just trying to add a line, or change a line, in the Safari plist file?

This is going to be on a clients machine. So i don’t want to have a shell script on there machine. I am editing a line in the Safari plist. Mac OS X CLI tool, is this part of unix or an application? Thanks!!

In Terminal, run: man defaults

Defaults should allow you to modify the plist in a single line of AppleScript code (via do shell script).

– Rob

Yes, defaults in the terminal will work, but do you know how to keep the preference file’s other default entries? When i use the terminal to change my the default that i want it ends up deleteing the rest of the settings in my plist good thing it makes a copy .plist.plist.

Any ideas, Thanks!

Yes, defaults in the terminal will work, but do you know how to keep the preference file’s other default entries? When i use the terminal to change my the default that i want it ends up deleteing the rest of the settings in my plist good thing it makes a copy .plist.plist.

Any ideas, Thanks!

This AppleScript script works for me. It changes only the targeted value while preserving the rest.

set new_value to "value to write to plist"
set command to "defaults write com.apple.safari AppleNavServices:PutFile:0:Path " & new_value
do shell script command

Note: It will modify the Safari plist that’s found in the Preferences folder. If the new value contains spaces, you should use the following to avoid problems in the shell.

set new_value to quoted form of "value to write to plist"
set command to "defaults write com.apple.safari AppleNavServices:PutFile:0:Path " & new_value
do shell script command

– Rob

Oh sweet, that works perfect!! Thanks for your help. I was doing it right in the terminal which made a copy for some reason.