Re: Calculate sizes script
Re: Calculate sizes script
- Subject: Re: Calculate sizes script
- From: Richard Morton <email@hidden>
- Date: Mon, 11 Feb 2002 14:58:23 +1100
Mr Tea's message of 09/02/02 10:10 AM contained:
...It will write each item's size into its 'get info' comments field at
the
top level of the dropped folder...
[...]
on open {theFolder}
tell application "Finder"
activate
set theItems to every item of theFolder as alias list
end tell
There is a bug in the old Finder's 'alias list' coercion, which causes it
to error if there is only 1 item. If you want to avoid that error you
may wish to do something like this:
tell application "Finder"
activate
try
set theItems to items of theFolder as alias list
on error
set theItems to {items of theFolder as alias}
end try
end tell
repeat with anItem in theItems
if size of (info for anItem) > 0 then
copy (size of (info for anItem)) / 1.073741824E+9 to theGBs
This calls 'info for' twice, which slows things unneccessarily. I would
perhaps use something like:
repeat with anItem in theItems
set byteSize to size of (info for anItem)
if byteSize > 0 then
copy byteSize / 1.073741824E+9 to theGBs
Alternatively, you could put all this string generating code (including
the last 2 lines above) into a handler so it could be used elsewhere.
All that might make the open handler look something like this:
on open {theFolder}
tell application "Finder"
activate
try
set theItems to items of theFolder as alias list
on error
set theItems to {items of theFolder as alias}
end try
end tell
repeat with anItem in theItems
set sizeString to makeSizeString from (size of (info for anItem))
tell application "Finder" to set comment of anItem to sizeString
end repeat
end open
I had my own handler (below) for generating readable size strings, but
it didn't do gigabytes so I updated it. I also noticed during
testing that your code errors when it encounters items that are
exact multiples of any of the size breakpoints - 1K, 10K, 100K, 1000K,
1MB, 10MB, etc. You could be reasonably confident that this would only
ever happen when you were showing the script to someone important. ;-}
The code below is about 2.5 times faster than what you posted, but I
wouldn't expect it to make a huge difference to overall execution times
because 'info for' will take quite a while (relatively speaking) to get
the size of any large folders.
-- -- ' makeSizeString' -- -- Richard Morton 2002
-- return a human readable size (string) from a byte value (int)
to makeSizeString from byteSize
set {oneK, oneMB, oneGB} to {1024, 1024 ^ 2, 1024 ^ 3}
tell byteSize to [option-L]
if it is not less than oneGB then
return "" & (it div oneGB) & "." & text -2 thru -1 of ("0" & (it mod
oneGB) div (oneK * 1.0E+4)) & " GB"
else if it is not less than oneMB then
return "" & (it div oneMB) & "." & text -2 thru -1 of ("0" & (it mod
oneMB) div (oneK * 10)) & " MB"
else if it is not less than oneK then
return "" & it div oneK & " K"
else
return "" & it & " bytes"
end if
end makeSizeString
(Any line that abuts the left margin and doesn't begin 'on' or 'end' has
been wrapped & should be appended to the previous line.)
Likewise. There is also a line break (noted as [option-L]) in the sub
routine above.
Cheers,
Richard Morton
-- Great Lies of the Music Business: "You'll have plenty of time for a
soundcheck"
_______________________________________________
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.