Re: How am I doing? (Script requires 24U Appearance OSAX)
Re: How am I doing? (Script requires 24U Appearance OSAX)
- Subject: Re: How am I doing? (Script requires 24U Appearance OSAX)
- From: Brennan <email@hidden>
- Date: Wed, 25 Sep 2002 00:26:53 +0200
On 24/09/2002 at 12:33 PM, email@hidden wrote:
Mr Tea <email@hidden> wrote
>
works, and in the continued pursuit of my ambition to be the most competent
>
AppleScripter in Wales, I would welcome any feedback and suggestions for
>
streamlining the script.
>
>
Here's the script. Longish, I'm afraid, so, many thanks to anyone who has
>
the patience to trawl through it.
OK, I'll make a few comments, but there may be stuff that I miss. Here goes:
>
set startupItems to alias "Mezzanine!:Users:ndt:Startup Items:"
>
set disabledItems to alias "Mezzanine!:Users:ndt:Startup Items:Disabled:"
Bzzt. Thanks to Chris Nebel's 'insider tip' mac.com archive, (accessible only to the initiate) I can tell you that, rather than using a hardcoded alias, you can use
set startupItems to (path to "strt") -- Startup Items folder
set disabledItems to (path to "strD") -- Startup Items Disabled folder
Why? Dunno. Someone (like me) might be running your script on a machine without a HD called 'Mezzanine' with a user called something other than 'ndt'.
>
tell application "Finder"
>
set onList to (name of every file of startupItems)
>
set offList to (name of every file of disabledItems)
>
end tell
Don't use the Finder unless you absolutely need it, it's slow, especially if you don't activate it first. In this case, use instead
set onList to (list folder startupItems)
set offList to (list folder disabledItems)
>
(* ***
>
step 4 - set default value of true/false variables
>
used to set 'selected' property of 24U check boxes...
>
I'm sure there must be a better way to do this
>
*** *)
>
set {checkOne, checkTwo, checkThree, checkFour, checkFive, checkSix,
>
checkSeven, checkEight, checkNine, checkTen} to {false, false, false, false,
>
false, false, false, false, false, false}
Ugh! If you're going to use a list, you don't need variable names.
Here's a nicer way to build a list of 10 'false' values. More lines of code, but easier to modify if you suddenly want more or fewer items.
set checklist to {}
repeat 10 times
set checklist to checklist & false
end repeat
You'll see why I did this, and how it can replace all those variables in a moment:
>
(* ***
>
step 5 - check each startup item to see if it's enabled
>
*** *)
>
if item 1 of newList is in (onList as string) then set checkOne to true
>
if item 2 of newList is in (onList as string) then set checkTwo to true
>
if item 3 of newList is in (onList as string) then set checkThree to true
>
if item 4 of newList is in (onList as string) then set checkFour to true
>
if item 5 of newList is in (onList as string) then set checkFive to true
>
if item 6 of newList is in (onList as string) then set checkSix to true
>
if item 7 of newList is in (onList as string) then set checkSeven to true
>
if item 8 of newList is in (onList as string) then set checkEight to true
Now that we're working with a list, we don't need all that.
Surely we want something more like:
set oLstr to (onList as string) -- do the coercion once & save time in the loop
repeat with i from 1 to 8
set item i of checkList to (item i of newList is in oLstr)
end repeat
Again, having more or fewer than 8 items is trivial now.
>
(* ***
>
step 6 - set the properties/parameters for the
>
'display better dialog' command, and display the dialog
>
*** *)
>
set theDialog to "Startup Items Manager" & return
>
set fieldOne to {kind:check box, name:(item 1 of newList) as string,
>
selected:checkOne}
>
set fieldTwo to {kind:check box, name:(item 2 of newList) as string,
>
selected:checkTwo}
>
set fieldThree to {kind:check box, name:(item 3 of newList) as string,
>
selected:checkThree}
>
set fieldFour to {kind:check box, name:(item 4 of newList) as string,
>
selected:checkFour}
>
set fieldFive to {kind:check box, name:(item 5 of newList) as string,
>
selected:checkFive}
>
set fieldSix to {kind:check box, name:(item 6 of newList) as string,
>
selected:checkSix}
>
set fieldSeven to {kind:check box, name:(item 7 of newList) as string,
>
selected:checkSeven}
>
set fieldEight to {kind:check box, name:(item 8 of newList) as string,
>
selected:checkEight}
>
set theFields to {fieldOne, fieldTwo, fieldThree, fieldFour, fieldFive,
>
fieldSix, fieldSeven, fieldEight}
This can be boiled down to...
set theFields to {}
repeat with i from 1 to 8
set iname to ((item i of newList) as string)
set selState to (item i of checkList)
set theFields to theFields & {kind:check box, name:iname, selected:selState}
end repeat
...But how about doing the iterative parts of both step 5 and 6 together in one fell swoop;
set oLstr to (onList as string) -- do the coercion once & save time in the loop
set ofLstr to (offList as string) -- do the coercion once & save time in the loop
repeat with i from 1 to 8
set theNewItem to (item i of newList)
set iname to (theNewItem as string)
set selState to (theNewItem is in oLstr)
set theFields to theFields & {kind:check box, name:iname, selected:selState}
end repeat
... and BTW, that hardcoded 8 makes me wonder whether it shouldn't be a variable instead.
>
tell application "Finder"
>
activate
>
set StartupSelection to display better dialog theDialog fields theFields
>
buttons theButtons default button 1
>
end tell
Huh? Why are you telling the Finder to display the dialog?
>
set theFields to the fields returned of StartupSelection
>
if the button returned of StartupSelection is not "Er..." then
>
repeat with aField in theFields
>
if selected of aField is true and (((name of aField) as string) is
>
in (offList as string)) then
>
tell application "Finder"
>
move (every file of disabledItems whose name contains ((name
>
of aField) as string)) to startupItems
>
end tell
>
else if not selected of aField and (((name of aField) as string) is
>
in (onList as string)) then
>
tell application "Finder"
>
move (every file of startupItems whose name contains ((name
>
of aField) as string)) to disabledItems
>
end tell
>
end if
>
end repeat
>
if the button returned of StartupSelection is "Log Out" then
>
tell application "Logout Script" to activate
>
end if
>
if the button returned of StartupSelection is "Restart" then
>
tell application "Restart Script X" to activate
>
end if
>
end if
'button returned of StartupSelection' can be got and stored in a temporary variable instead of making repeated access to the same property. (Some work always takes more time than no work at all). 'else' is your friend. It can also speeds up your code.
(* step 7 - respond to the results returned by the dialog *)
set theFields to the fields returned of StartupSelection
set theButton to button returned of StartupSelection
if theButton is not "Er..." then
tell application "Finder" -- use one tell block
activate -- and activate for better performance
repeat with aField in theFields
set fieldName to ((name of aField) as string)
set fieldSelected to (selected of aField)
if fieldSelected and (fieldName is in ofLstr) then
move (every file of disabledItems whose name contains fieldName) to startupItems
else if not fieldSelected and (fieldName is in oLstr) then
move (every file of startupItems whose name contains fieldName) to disabledItems
end if
end repeat
end tell
if theButton is "Log Out" then
-- tell application "Logout Script" to activate
-- why not run these scripts as script objects? It will be faster.
else if theButton is "Restart" then
-- tell application "Restart Script X" to activate
-- why not run these scripts as script objects? It will be faster.
end if
end if
I can't help feeling this last part could be optimized further, so that you gather two lists of all the files that should be moved to each destination and then move them all at once. Dunno if that would be faster, but I suspect so.
How is that for you?
Brennan
_______________________________________________
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.