Re: modify xml file
Re: modify xml file
- Subject: Re: modify xml file
- From: David Durkee <email@hidden>
- Date: Mon, 11 Aug 2003 16:31:15 -0500
On Monday, August 11, 2003, at 04:08 PM, Jeramey Valley wrote:
I need to modify one value in an xml file, and save the updated file.
So far, I don't see an easy way to do this, but perhaps I'm missing
something.
Specifically, I need to modify the user id (authname) in the system
preferences.xml file. I need non-Admin users to be able to do this so
they can enter their own dial up information while on the road. After
looking at the issue from several angles, modifying the file directly
appears to be my best choice.
So, I figure reading in the pref file, finding that value (see below)
and writing out the file would be my best choice. I'm having
difficulty in coming up with a clean way of performing this task.
<key>AuthName</key>
<string>MyUserID</string>
A similar question came in about a week and a half ago, and I answered
it privately instead of to the list (by accident). My answer discussed
two possible ways to modify an XML file through AppleScript. I hope
it's helpful.
Questions: Are you sure that modifying the file will be sufficient? You
may have to log out and back in again for the change to take effect.
You could try editing the file with a text editor (note: TextEdit is a
BAD choice for this. BBEdit is a good one.) to see whether it works.
Also, if the permissions of the file you're editing exclude editing by
non-admin users, I'm not sure how to make this work. (Change the
permission on the file, maybe?)
David
I'm not sure I understood everything, but it looks possible. You might
not even need BBEdit. AppleScript itself is pretty good at handling
files and text, there are scripting additions (open for access, read,
close access) that can read in the files. You'd need to experiment to
find the best way to parse the text, but Applescript could load the
entire contents of the file into a variable like this:
set fileref to open for access afile
set plist to read fileref as text
close access fileref
where afile is an alias to the file. The text of the file is now in
plist.
When you're done, you can write the file like this:
set fileref to open for access afile with write permission
write plist to afile
close access fileref
You can parse the text using expressions like:
word n of plist
paragraph n of plist -- this returns a line of text
word w of paragraph p of plist
Unfortunately the standard word parsing isn't well suited to XML.
Searching the text can be done like this:
offset of "/Library/filename.plist" in plist
Actually, a simple search and replace of one instance could be done
like this:
set searchstring to "/Library/filename.plist"
set replacestring to
"/Network/Servers/ServerName/Volumes/VolumeName/Students/ClassYear/
Username/Library/Preferences/FolderName/filename.plist"
set x to offset of searchstring in plist
set plist to (characters 1 through (x - 1) of plist as string) &
replacestring & characters (x + (count searchstring)) through -1 of
plist as string
Another alternative is a freeware scripting addition called XML Tool.
This allows you to read an XML file into an AppleScript record,
manipulate the data, and write it out again as XML. I haven't used this
so I can't guarantee it will produce a properly formatted plist file,
but it's certainly worth a try. You can get it at:
http://www.latenightsw.com/freeware/XMLTools2/
While you're at the Late Night Software site, you should check out
Script Debugger if you don't have it already. It's excellent if you
have the budget for it. There's a free 30-day demo version.
David
On Thursday, July 31, 2003, at 08:50 AM, Meg Faddick wrote:
Can AppleScript do this?
I have hundreds of nested folders with a few known text files (XML) in
each which need to have changes made in the text based on two folder
names which correspond to a username (text) and a class year (a
number). The folders (and files) are in each of these two path
structures:
/Students/ClassYear/Username/Library/Preferences/filename.plist
/Students/ClassYear/Username/Library/Prefences/ByHost/filename.plist
I want to:
Drag and drop the Students folder onto a script which:
-Looks for the name of the ClassYear folder and the name of the
Username folder
-Sets variable1 to the Username found by the folder in the above path
named with the Username
-Sets variable2 to the ClassYear found by the folder in the above path
named with the ClassYear
(loop)
-Searches the Preferences/ByHost folder for a known file - FileA
-Searches FileA for one known text string which is always in the same
place, set found string ("/Library/filename.plist") to new string
based on variable1 and variable2
("/Network/Servers/ServerName/Volumes/VolumeName/Students/ClassYear/
Username/Library/Preferences/FolderName/filename.plist")
(-Searches the Preferences/ByHost folder for known FileB, etc, etc
---- end loop)
(loop)
-Searches the Preferences folder for a known file - FileX
-Searches FileX for one known text string which is always in the same
place, set found string ("/Library/filename.plist") to new string
based on variable1 and variable2
("/Network/Servers/ServerName/Volumes/VolumeName/Students/ClassYear/
Username/Library/Preferences/FolderName/filename.plist")
-Searches FileX for one known text string, set found string
("/Library/filename.plist") to new string based on variable1 and
variable2
("/Network/Servers/ServerName/Volumes/VolumeName/Students/ClassYear/
Username/Library/Preferences/FolderName/filename.plist")
(on to known FileY, FileZ, etc, etc --- end loop)
Alternatively, if it made for an easier script to write, I would be
willing to manually set variable2 to each ClassYear and just drag each
ClassYear folder onto the script, letting the script find each
username folder and set it to variable1. I'd have to do this 12 times,
but that's survivable. Doing each file by hand is not!
I'm an OS X Server admin at a K-12 school. I'm new to AppleScript, but
believe it could be the answer to some problems we're experiencing
with property list (.plist) preference files. The files are just XML
text, and thus can be edited in most text editors. I know that BBEdit
is scriptable with a find/replace command in its dictionary and I was
hoping to use it for this purpose.
Any help with this would be tremendously appreciated!
Meg
--
David Durkee
email@hidden
<
http://ddurkee.homeip.net/>
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.