Report Hard Drive Availabe Space ?

Hello,
We’re hoping to develop a workflow where a script could systematically poll the various computers on our network to determine their available space. Ideally this info would get passed out and into a Filemaker database that would be the clearing house for the information.

So is there some way to get the “Used Space” and “Available Space” info for Hard Drives? Would this then be expandable to ALL the various HD’s on the network?

Thanks for any input ! KB

Here’s why I’m at so far …

tell application "Finder" to set theVolume to name of startup disk

tell application "Finder"
	set whatsLeft to free space ¬
		of disk theVolume
end tell

set Giga to {(whatsLeft) / 1024 / 1024 / 1024}

display dialog "The available space on the drive
 Macintosh HD is " & Giga & " Gigabytes"

I see you have already made some progress, I am not sure how much this post really adds.

Here is something that might get you started on the data gathering part:

set reportText to ""

tell application "Finder" to ¬
	set diskList to every disk whose local volume is true
repeat with diskRef in diskList
	if reportText is not equal to "" then ¬
		set reportText to reportText & return
	tell application "Finder" to ¬
		set {name:diskName, free space:freeSpace, capacity:totalSpace} to contents of diskRef
	set usedSpace to totalSpace - freeSpace
	set reportText to reportText & diskName & ": " & freeSpace & " / " & usedSpace
end repeat

display dialog reportText with title "Free/Used Space Report"

The core of it is getting Finder to make a list of every disk whose local volume is true. Then, in the loop, I extract various properties from each of the disks: name, free space, and capacity. Instead of making a report, it sounds like you would want to push the information into a database (I do not have Filemaker, so I will not speculate on that part). This list will include external disks, “thumb drives”, and mounted disk images (regardless of their source), so you may need to come up with a way to filter out disks in which you have no interest.

As far as gathering information from multiple machines, there is a way to run AppleScript commands on remote machines, but I only have one machine, so I have never done it. First you have to enable Remote Apple Events in the Sharing System Preference Pane. Then, you use something like tell application “Finder” of machine “Name of Other Machine” to send commands to Finder running on the other machine. See References to Remote Applications in the AppleScript Language Guide. Enabling Remote Apple Events opens a new service that may expose new security vulnerabilities.

Another way to go might be to arrange for an AppleScript program to run on each of the computers and then “report back” by writing to a file on network share, or maybe by sending the data directly to Filemaker through some sort of remote database access (I have read mentions of something like that, but I have never used Filemaker).

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Another solution might be to enable SSH tunneling on all those computers (which is relatively secure as long is no one is loose with their password and on the same LAN) and useing a short shell script to report out the disk space numbers. There is a handy little command called “df” which you can type into terminal and get out something like this:

% df
Filesystem 512-blocks Used Avail Capacity Mounted on
/dev/disk0s9 39065400 35600912 3464488 91% /
devfs 63 63 0 100% /dev
fdesc 2 2 0 100% /dev
1024 1024 0 100% /.vol
automount -fstab [243] 0 0 0 100% /Network/Servers
automount -static [243] 0 0 0 100% /automount

We could THEN create a script in either shell script or applescript which can save and send that data. For example, this little script takes down that “df” data (Used space and Available space and much more included) and writes it to a file called hd_test in your Documents folder.


set theData to do shell script "df"
set dp to ((path to "docs" as text) & "hd_test.txt")
set theTarget to open for access file dp with write permission
close access theTarget
write theData to file dp

You could very easily have your own program run a remote “df” check on those machines, have it save that data as text or something like above, and email/ftp/SSH/whatever it back to your computer. It may even be possible to merely “mount” the disks remotely and run the check from your computer. If this sounds like an exceptable plan, I would be happy to walk you through creating the necassary scripts and activations for such a task.

  • dp

So I’ve been working a little more on this script. What I have now will take a disk (for the attached example it’s the startup disk) and report back the Overall Capacity, the Used Space, the Available Space and finally the disk capacity as a percentage. I worked in some sub-routines to take care of rounding the numbers off some.

So the next step would be to implement this across the mounted volumes that any particular machine has. So it basically would require combining what I have so far + the script that chrys posted in his response. So that’s where I stand. I’ll keep working away on it and will post back. If anyone else wants to chime in along the way, I’m all ears. Thanks for the input so far.

I’m sure I’ll run into complications at some point when I try to consolidate what I have with chrys’s example.

Here it is:

tell application "Finder"
	set theVolume to name of startup disk
	set {diskSize, whatsLeft} to {capacity, free space} of disk theVolume
	set usedUp to (diskSize - whatsLeft)
	set ConvertedSpace to {(usedUp) / 1024 / 1024 / 1024} as string
	set theSize to capacity of disk "Macintosh HD"
	set ConvertedSize to {(theSize) / 1024 / 1024 / 1024} as string
	set ConvertedFree to {(whatsLeft) / 1024 / 1024 / 1024} as string
	set Percentage to ((diskSize - whatsLeft) / diskSize) * 100
end tell

set ConvertedSize to round_truncate(ConvertedSize, 2)
set ConvertedSpace to round_truncate(ConvertedSpace, 2)
set ConvertedFree to round_truncate(ConvertedFree, 2)
set Percentage to round_truncate(Percentage, 1)
display dialog "This Hard Drive's overall capacity is " & ConvertedSize & " GB." & return & return & "The used space is " & ConvertedSpace & " GB." & return & return & "Tha available space is " & ConvertedFree & " GB." & return & return & "This drive is " & Percentage & "% full." buttons "OK" default button "OK"


-->Subroutine #1 - Number to Text
number_to_text(ConvertedSpace)
on number_to_text(this_number)
	set this_number to this_number as text
	if this_number contains "E+" then
		set x to the offset of "." in this_number
		set y to the offset of "+" in this_number
		set z to the offset of "E" in this_number
		set the decimal_adjust to characters (y - (length of this_number)) thru ¬
			-1 of this_number as string as number
		if x is not 0 then
			set the first_part to characters 1 thru (x - 1) of this_number as string
		else
			set the first_part to ""
		end if
		set the second_part to characters (x + 1) thru (z - 1) of this_number as string
		set the converted_number to the first_part
		repeat with i from 1 to the decimal_adjust
			try
				set the converted_number to ¬
					the converted_number & character i of the second_part
			on error
				set the converted_number to the converted_number & "0"
			end try
		end repeat
		return the converted_number
	else
		return this_number
	end if
end number_to_text

-->Subroutine #2 - Round and Truncate
round_truncate(ConvertedSpace, 2)
on round_truncate(this_number, decimal_places)
	if decimal_places is 0 then
		set this_number to this_number + 0.5
		return number_to_text(this_number div 1)
	end if
	
	set the rounding_value to "5"
	repeat decimal_places times
		set the rounding_value to "0" & the rounding_value
	end repeat
	set the rounding_value to ("." & the rounding_value) as number
	
	set this_number to this_number + rounding_value
	
	set the mod_value to "1"
	repeat decimal_places - 1 times
		set the mod_value to "0" & the mod_value
	end repeat
	set the mod_value to ("." & the mod_value) as number
	
	set second_part to (this_number mod 1) div the mod_value
	if the length of (the second_part as text) is less than the ¬
		decimal_places then
		repeat decimal_places - ¬
			(the length of (the second_part as text)) times
			set second_part to ("0" & second_part) as string
		end repeat
	end if
	
	set first_part to this_number div 1
	set first_part to number_to_text(first_part)
	set this_number to (first_part & "." & second_part)
	
	return this_number
end round_truncate

Hi,

I know, the formatting is not perfect, but try this

property GB : 1.073741824E+9 -- is the equivalent to 1024 * 1024 * 1024

tell application "Finder" to set allDisks to name of disks whose local volume is true
set theData to "Disk" & tab & tab & "Capacity" & tab & "Used" & tab & "Free" & tab & tab & "Percentage" & return
repeat with oneDisk in allDisks
	tell application "Finder" to set {diskSize, whatsLeft} to {capacity, free space} of disk oneDisk
	set ConvertedSpace to round_dec(((diskSize - whatsLeft) / GB), 2)
	set ConvertedSize to round_dec(diskSize / GB, 2)
	set ConvertedFree to round_dec(whatsLeft / GB, 2)
	set Percentage to round_dec(((diskSize - whatsLeft) / diskSize * 100), 1)
	set theData to theData & oneDisk & tab & ConvertedSize & tab & ConvertedSpace & tab & ConvertedFree & tab & Percentage & return
end repeat
display dialog theData buttons {"                          OK                            "} default button 1

on round_dec(value, decplace)
	return (round (value * (10 ^ decplace))) / (10 ^ decplace)
end round_dec

PS: these Apple subroutines are not needed, because the values are converted in gigabytes anyway
and the rounding can be done with an one-liner

I like this version (adapted from chrys’) which lines up a bit better:

set reportText to ""
set Gig to 1024 * 1024 * 1024
tell application "Finder" to ¬
	set diskList to every disk whose local volume is true
repeat with diskRef in diskList
	if reportText is not equal to "" then ¬
		set reportText to reportText & return
	tell application "Finder" to ¬
		set {name:diskName, free space:freeSpace, capacity:totalSpace} to contents of diskRef
	set usedSpace to totalSpace - freeSpace
	set perc to usedSpace / totalSpace
	set reportText to reportText & diskName & ": " & characters 1 thru 4 of ((freeSpace / Gig) as text) & " / " & characters 1 thru 4 of ((usedSpace / Gig) as text) & " - " & characters 1 thru 4 of ((100 * perc) as text) & "% used."
end repeat

display dialog reportText with title "Free/Used Space Report"