Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Applications > Applescript to notify of Folder size limit?

Applescript to notify of Folder size limit?
Thread Tools
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Mar 18, 2005, 10:38 AM
 
Has anyone run across a Folder Script that will notify the user when a folder has reached a certain size?
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Mar 18, 2005, 01:31 PM
 
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.]
(Last edited by Diggory Laycock; Mar 19, 2005 at 06:29 AM. )
You know it makes sense. ☼ ☼ ☼ Growl.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Mar 20, 2005, 05:32 AM
 
Well - Did it work?

Does it suit?
You know it makes sense. ☼ ☼ ☼ Growl.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 05:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2