Requires Growl (
http://www.growl.info)
[php]
-- ******************************************
-- Size warning FolderActionScript
-- ******************************************
(*
This script is a folder action script.
It needs to be attached to a folder - and will act on files and folders added to that folder.
It will check the size of the folder when items are added.
Then warn if the size is too large.
Bugs:
The script is triggered at the start of the copy - so the size is calculated before the copy operation completes.
This means that a copy could quite easily go over the limit without warning you.
The next copy would correctly warn you though...
*)
-- Config Properties
-- The folder limit size in megabytes
property limitInMegs : 15
-- How close to the limit do we want the warnings to start?
-- e.g. 0.5 would start warning at half full
property proximityWarningPercent : 0.75
-- Growl Config
property scriptGrowlAppName : "Folder Size Warning AppleScript"
property allNotificationsList : {"Script Error", "Limit Proximity Warning", "Limit Breached", "InternalDebugging"} as list
property defaultNotificationsList : {"Script Error", "Limit Proximity Warning", "Limit Breached"} as list
-- Warn when run normally.
on run
my registerForGrowl()
my quickError("This is a Folder Action Script - it should not be run as a normal script.")
-- But run it anyway - on a specific folder - for debugging purposes.
-- set testFolder to ":users:diggory:a:" as alias
-- my checkFolderSize(testFolder)
end run
--The handler for responding to when items are added to a folder:
on adding folder items to this_folder after receiving these_items
-- 'these_items' will contain a list of file references to the added items
-- But we do not care about those - just the parent folder.
my checkFolderSize(this_folder)
end adding folder items to
to checkFolderSize(the folderAlias)
try
my registerForGrowl()
my quickInternalDebug("LimitWarn starts...")
tell application "Finder" to set folderSize to physical size of the folderAlias
-- Call it twice - for some reason the finder needs this.
tell application "Finder" to set folderSizeInKBytes to (physical size of the folderAlias) / 1024 as number
set folderSizeInMBytes to folderSizeInKBytes / 1024
my quickInternalDebug(("Folder size... " & folderSizeInMBytes & " MB") as string)
-- are we over the limit?
if folderSizeInMBytes > limitInMegs then
-- Danger Will Robinson!
my quickInternalDebug("We are over the limit!! ")
set growlDescription to "You have Breached your limit!"
tell application "GrowlHelperApp" to notify with name ¬
"Limit Breached" title ¬
"Warning!" description ¬
growlDescription application name scriptGrowlAppName image from location "/System/Library/CoreServices/Software Update.app/Contents/Resources/exclamation.tif"
else
-- are we close enough to the limit to warn?
set percentFull to folderSizeInMBytes / limitInMegs
my quickInternalDebug(("How full? " & percentFull * 100 & "%") as string)
if percentFull > proximityWarningPercent then
-- We should warn.
set percentAboveWarningThreshold to ((percentFull - proximityWarningPercent) / (1 - proximityWarningPercent))
-- There are five different priorities in growl.
set the growlPriority to round ((percentAboveWarningThreshold * 5) - 2) rounding as taught in school
my quickInternalDebug(("This is a courtesy notice - you are getting close to your limit... percent above warnThresh: " & percentAboveWarningThreshold * 100 & " % -- GrowlPriority: " & growlPriority) as string)
set nicePercentage to round (percentFull * 100) rounding as taught in school
set growlDescription to "you are getting close to your limit... " & nicePercentage & "% full."
tell application "GrowlHelperApp" to notify with name ¬
"Limit Proximity Warning" title ¬
"Limit Proximity Warning" description ¬
growlDescription application name scriptGrowlAppName ¬
priority growlPriority
end if
end if
on error errorText number errnum
my quickError("It's all gone a bit Pete Tong! : " & errorText & "(" & errnum & ")")
end try
end checkFolderSize
--
-- SubRoutines
--
-- Growl Support Routines
to registerForGrowl()
tell application "GrowlHelperApp"
-- Make a list of all the notification types
-- that this script will ever send:
-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application ¬
scriptGrowlAppName all notifications allNotificationsList ¬
default notifications defaultNotificationsList ¬
icon of application "Finder"
end tell
end registerForGrowl
to quickError(errorText)
tell application "GrowlHelperApp"
-- Send a Notification...
notify with name ¬
"Script Error" title ¬
"LimitWarn AppleScript ran into an error..." description ¬
(errorText as string) application name scriptGrowlAppName image from location "/System/Library/CoreServices/Software Update.app/Contents/Resources/exclamation.tif"
end tell
end quickError
to quickInternalDebug(debugNote)
tell application "GrowlHelperApp"
notify with name ¬
"InternalDebugging" title ¬
"Debugging note..." description ¬
(debugNote as string) application name scriptGrowlAppName image from location "/System/Library/CoreServices/Software Update.app/Contents/Resources/Installcomplete.tif"
end tell
end quickInternalDebug
[/php]
[edit - code clean up and priority calculation bug fixed.]