Re: Settings backup script
Re: Settings backup script
- Subject: Re: Settings backup script
- From: Nigel Garvey <email@hidden>
- Date: Fri, 3 Aug 2001 03:05:57 +0100
"Joseph A. Weaks" wrote on Thu, 2 Aug 2001 03:57:15 -0500:
>
Well, here it is... my first real script. I've been lurking on the
>
list for over a month. If it pleases you, feel free to peruse the
>
script and let me know what I did needlessly, incorrect, foolishly,
>
etc.
Hi, Joe. It's a good first script! I can't find anything outrageously
wrong with it, but there are a couple of vulnerabilities:
>
tell application "Finder"
>
>
-- Defining the path to the Accordance folder and the
>
settings file to be backed up
>
set AccordanceFolder to folder "Accordance folder" of folder
>
"Applications" of startup disk as string
>
set SettingsFile to item "Highlight settings" of folder
>
"Accordance Settings " of folder AccordanceFolder as string
The first of these 'set' lines will error if "Accordance folder" doesn't
exist in the expected location. The second will error if "Highlight
settings" isn't found. So the 'try' block which follows - to test for
this - comes too late:
>
-- Reads the Comments info field of the settings file
>
try
>
set MyComments to the comment of item SettingsFile
>
on error
>
set MyReply to choose folder with prompt "Please
>
locate the Accordance folder. To avoid this step in the future,
>
please edit the AccordanceFolder variable at the beginning of this
>
script. It's very easy to do, so give it a try."
>
set AccordanceFolder to MyReply as string
>
end try
It's not actually necessary to get the user to edit the script. The
script itself can learn the location the first time it's run if you make
AccordanceFolder a 'property'. You'd start the script something like this:
property AccordanceFolder : ""
if AccordanceFolder is "" then
set AccordanceFolder to (choose folder with prompt "Please locate the
Accordance folder.") as string
end if
The first time the script runs now, it'll ask the user to locate the
folder. Thereafter, the path string will be stored in the script itself
and the user won't be bothered again. (You can recompile and resave the
script to make it forget the selection, or you can write some other
contingency into the script for this.)
>
-- Ask for a Comment entry if none is found
>
if MyComments is "" then
MyComments is set in your 'try' block. If there's an error, the variable
won't be set - so trying to read it here will cause another error. I
imagine you'd probably want to quit the script before this anyway if
there was a problem with "Highlight settings":
tell application "Finder"
try
set SettingsFile to (file "Highlight settings" of folder
"Accordance Settings " of folder AccordanceFolder) as string
set MyComments to the comment of file SettingsFile
on error errMsg -- if there's an error
display dialog errMsg -- display the cause
error number -128 -- and quit the script
end try
And now for a couple of niceties:
>
set MyReply to display dialog "Please add a
>
descriptive comment to the current settings file. A folder with this
>
name will be created in a Backup folder inside the Accordance folder
>
to house this version of the settings file." default answer "Replies
>
over 30 characters will be truncated."
>
if (button returned of MyReply is "OK") then
>
set MyComments to text returned of MyReply
>
end if
This works fine, but when no buttons are specified in a 'display dialog'
command, the defaults (on English-language systems) are "Cancel" and
"OK". If the user clicks on "Cancel", the script immediately quits
(unless another 'try' block prevents this). So, if the execution gets to
your 'if' line, it *must* have been the "OK" button that was clicked, so
there's no need for the 'if' line! (I love explaining these things. ;-) )
>
set comment of item SettingsFile to MyComments
>
end if
>
>
if the number of characters of the MyComments is greater than 30 then
It's better (though not vital) to avoid mentioning the 'characters' of a
string unless you actually need a list of those individual entities. It's
the characters of a string that are counted by default, so 'number of
MyComments', or 'length of MyComments', or 'count MyComments' will tell
you how many characters there are in the string. The expression 'number
of characters of MyComments' is the same as 'number of (characters of
MyComments)'. It first makes a list of the individual characters - eg.
{"H", "e", "l", "l", "o"} - and then counts the items in the list. The
answer's exactly the same, but it uses more memory and takes a little
longer.
>
set the MyComments to (characters 1 thru 30 of the
>
MyComments) as string
Similarly, '(characters 1 thru 30 of MyComments) as string' creates a
list of 30 1-character strings and then coerces it back to a single,
30-character string. To copy the 30-character string directly from the
original, you use 'text 1 thru 30 of MyComments'.
I hope this is helpful. I haven't actually run the script myself, but the
rest of it looks fine. :-)
>
I thought the use of the comments field was pretty clever.
It's a nice touch... ;-)
NG