First of all, I’m new to AppleScript, so please excuse my simple questions.
I’ve been trying to write a script to use as a folder action that tells me when a
certain folder reaches 650MB. So far I’ve had no luck.
In plain english here’s what I’d like to do.
Tell application Finder
Get size of folder UntitledFolder
If size is greater than 650 MB then disply dialog
“Time to brun a disc.”
Now how do I write that in AplpeScript?
Get Size gives me a number like 1.96534260+E
How do I Get the size of a folder in MB?
Then how do I write “if the size is greather than 650 MB”?
This might be close to what you want. I haven’t tested much but it appears to work. Tested on OS X 10.2.8.
on adding folder items to this_folder after receiving added_items
set folSize to convertByteSize from (size of (info for this_folder))
if word 2 of folSize is "TB" or word 2 of folSize is "GB" then
display dialog "Burn Baby Burn! Folder size: " & folSize
return
end if
if word 1 of folSize is greater than or equal to 650 and word 2 of folSize is "MB" then
display dialog "Burn Baby Burn! Folder size: " & folSize
end if
end adding folder items to
to convertByteSize from byteSize -- by Nigel Garvey
if byteSize ? 1.099511627776E+12 then -- Terabytes (2 ^ 40)
((byteSize / 1.099511627776E+12 div 0.01 / 100.0) as string) & " TB"
else if byteSize ? 1.073741824E+9 then -- Gigabytes (2 ^ 30)
((byteSize / 1.073741824E+9 div 0.01 / 100.0) as string) & " GB"
else if byteSize ? 1048576 then -- Megabytes (2 ^ 20)
((byteSize / 1048576 div 0.01 / 100.0) as string) & " MB"
else if byteSize ? 1024 then -- Kilobytes (2 ^ 10)
((byteSize div 1024) as string) & " K"
else
(byteSize as string) & " bytes"
end if
end convertByteSize
Nope, I guess not, and I didn’t notice! Thanks for the heads up, Greg. Let’s try again.
on adding folder items to this_folder after receiving added_items
set folSize to convertByteSize from (size of (info for this_folder))
if word 2 of folSize is "TB" or word 2 of folSize is "GB" then
display dialog "Burn Baby Burn! Folder size: " & folSize
return
end if
if word 1 of folSize is greater than or equal to 650 and word 2 of folSize is "MB" then
display dialog "Burn Baby Burn! Folder size: " & folSize
end if
end adding folder items to
to convertByteSize from byteSize -- by Nigel Garvey
if byteSize is greater than or equal to 1.099511627776E+12 then -- Terabytes (2 ^ 40)
((byteSize / 1.099511627776E+12 div 0.01 / 100.0) as string) & " TB"
else if byteSize is greater than or equal to 1.073741824E+9 then -- Gigabytes (2 ^ 30)
((byteSize / 1.073741824E+9 div 0.01 / 100.0) as string) & " GB"
else if byteSize is greater than or equal to 1048576 then -- Megabytes (2 ^ 20)
((byteSize / 1048576 div 0.01 / 100.0) as string) & " MB"
else if byteSize is greater than or equal to 1024 then -- Kilobytes (2 ^ 10)
((byteSize div 1024) as string) & " K"
else
(byteSize as string) & " bytes"
end if
end convertByteSize
Presumably what’s wanted is a folder action something like this:
on adding folder items to thisFolder after receiving addedItems
tell application "Finder"
thisFolder's physical size >= 6.815744E+8 -- 650 * 2 ^ 20
end tell
if the result is true then
tell application (path to frontmost application as string)
display dialog "Time to burn a disc." buttons {"OK"} default button 1 with icon note
end tell
end if
end adding folder items to