Re: Renaming files in Finder: why so slow?
Re: Renaming files in Finder: why so slow?
- Subject: Re: Renaming files in Finder: why so slow?
- From: Nigel Garvey <email@hidden>
- Date: Wed, 3 Sep 2003 17:02:52 +0100
Emmanuel wrote on Sun, 31 Aug 2003 13:19:41 +0200:
>
At 11:07 AM +0100 8/31/03, Nigel Garvey wrote:
>
> on doUnderScore(theList)
>
[...]
>
> repeat with thisItem in theList
>
[...]
>
> my doUnderScore(thisItem)
>
[...]
>
> end repeat
>
[...]
>
> end doUnderScore
>
>
This is a circumstance where recursivity is only a comfort issue, the
>
algorithm is not intrinsically recursive.
[...]
>
Here I think that it is enough to:
>
1. maintain a global "theList"
>
2. replacing the call to "my doUnderScore" with something by "set end of
>
theList to thisItem"
>
3. instead of calling once doUnderScore, call it with "repeat while
>
theList's length is 0"
Being very doubtful about this, I contacted Emmanuel off-list to seek
clarification. He rephrased it for me thus:
>
repeat until the to-do list is empty
>
set thepath to item 1 of the to-do list
>
set to-do list to items 2 thru -1 of the to-do list
>
process thepath
>
if thepath is a folder then set end of the to-do list to all items in thepath
>
end
Trying my own take, I've come up with a script that's nearly twice as
fast with my test hierarchy as the one I posted on Sunday. I've no idea
where the speed comes from, but I'm looking into it! ;-) Again, this is
only intended for pre-X systems. I haven't tried it in X.
on open aliasList
tell application "Finder" to activate
set oldTIDs to AppleScript's text item delimiters
considering case -- for faster 'contains' and 'ends with'
repeat until aliasList is {} -- until the last item is removed
-- Get the first alias from the list
set thisItem to the beginning of aliasList
-- Test whether it's a file or a folder
set pathStr to thisItem as string
set AppleScript's text item delimiters to {":"}
if pathStr ends with ":" then -- it's a folder
set itemName to text item -2 of pathStr
-- Remove the folder from the alias list and append aliases to
any of
-- its contents that qualify for treatment. They must be
aliases, as
-- the folder's name may change before the contents are
processed.
tell application "Finder" to set aliasList to the rest of
aliasList & my getAliasList(a reference to thisItem's folders) & my
getAliasList(a reference to (thisItem's files whose name contains " "))
else -- it's a file
set itemName to text item -1 of pathStr
-- Remove the file from the alias list
set aliasList to the rest of aliasList
end if
-- Only change the name if necessary
if itemName contains " " then
set AppleScript's text item delimiters to {" "}
set itemName to itemName's text items
set AppleScript's text item delimiters to {"_"}
tell application "Finder" to set name of thisItem to (itemName
as string)
end if
end repeat
end considering
set AppleScript's text item delimiters to oldTIDs
end open
on getAliasList(FinderRef)
tell application "Finder"
try
FinderRef as alias list
on error
FinderRef as alias as list
end try
end tell
end getAliasList
NG
_______________________________________________
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.