display free disk space shell script

Wow, I thought grep was powerful.

Thanks,
Nik

G’day Bend3

The disk space is also available via an Applescript command

Regards

Santa


			set free_bytes to free space of disk YourDiskName -- the number of free bytes left on the disk
			set free_Gbytes to (free_bytes / (1024 * 1024 * 0.1024) div 100) / 100
			

Needs the Finder, Santa:

tell application "Finder" to set free_bytes to free space of disk "ACB-G5_Leopard" -- the number of free bytes left on the disk
set free_Gbytes to (free_bytes / (1024 * 1024 * 0.1024) div 100) / 100

. or System Events :wink:

I am using
set dSize to (do shell script “df -H /Volumes/| grep /dev/disk | cut -d ‘G’ -f 3 | cut -d ’ ’ -f 4-5”) as text
now how I can compare with some number (say 10 or 10 GB) so that I can display dialog

What’s wrong with the System Events version?

tell application "System Events" to set freeBytes to free space of disk "Macintosh HD"
set freeGigaBytes to freeBytes / 1.0E+9 div 1 -- never system versions use 1000 Bytes = 1 kB

or the Cocoa way (edit: including a smart formatting)

Edit: The coercion to record handles the type of the file size (unsigned long long) automatically.

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
set freeBytes to (attributes as record)'s NSFileSystemFreeSize
set formattedFreeBytes to current application's NSByteCountFormatter's stringFromByteCount:freeBytes countStyle:(current application's NSByteCountFormatterCountStyleFile)
display dialog (formattedFreeBytes as text)

With this
tell application “System Events” to set freeBytes to free space of disk “Macintosh HD”
set freeGigaBytes to freeBytes / 1.0E+9 div 1 – never system versions use 1000 Bytes = 1 kB

HDD name should be “Macintosh HD” and in a large number this can be different (many user like to change the name).
Can we set it to get free space info from Boot volume

posix file "/" as string
tell application "System Events" to set freeBytes to free space of startup disk

Thanks DJ Bazzie Wazzie

Thanks Stefan

I was looking for the same
working for me
Thanks All

Ah, didn’t saw you using system events. In Mavericks (haven’t tested it on new machines), somehow system events return wrong values. Two of my Maverick machines return wrong values, one returns 1 and the other returns a negative value. In a tell application “Finder” block the right values are returned.

On systems supporting AppleScriptObjC I’d recommend those version anyway.

Then I recommend not to use integerValue() for free disk space. It’s better to use longLongValue() instead to be certain that you always use a 64-bit integer. When running in 32-bit mode integerValue() will only be accurate till 2GB free space (4GB for unsignedIntegerValue()).

Absolutely right, thanks. Strictly spoken the value is unsigned, so it’s even unsignedLongLongValue()

Edit: A coercion to record handles the proper type implicitly. I updated the post above.

Here is StefanK’s proposed version returning the number of free bytes.

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
set freeBytes to (attributes's objectForKey:(current application's NSFileSystemFreeSize))'s longLongValue()
set freeBytes to my formatnumber:freeBytes
--> "277 390 725 120"

# Borrowed from Shane STANLEY
on formatnumber:theNumber
	set theResult to current application's NSNumberFormatter's localizedStringFromNumber:theNumber numberStyle:(current application's NSNumberFormatterDecimalStyle)
	return theResult as text
end formatnumber:

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) lundi 30 mai 2016 16:11:57

Yvan, for file sizes there’s even a more convenient formatter

on formatnumber:theNumber
	set theResult to current application's NSByteCountFormatter's stringFromByteCount:theNumber countStyle:(current application's NSByteCountFormatterCountStyleFile)
	return theResult as text
end formatnumber:

Hey chaps.

If the attribute’s dictionary’s coerced to record, would the scripting bridge automatically take care of the integer type(s)?

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
-- Coerce the dictionary to record and read the free byte figure from that.
set freeBytes to (attributes as record)'s NSFileSystemFreeSize
set freeGigaBytes to (freeBytes / 1.0E+7 as integer) / 100

Or is it better to be explicit in ASObjC?

Good catch, Nigel.

Yes, that’s right indeed. The different types of NSNumber are bridged correctly to the appropriate AppleScript types.
I updated the code above again.

Thanks but I deliberately choose the Shane’s handler to show to interested users how to display a large number with all digits (not the scientific format).

In fact, using this handler there is no need to explicitly define a format.

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
set freeBytes to (attributes's objectForKey:(current application's NSFileSystemFreeSize))
set freeBytesYK to my formatnumber:freeBytes
--> "277 390 725 120"

on formatnumber:theNumber
	set theResult to current application's NSNumberFormatter's localizedStringFromNumber:theNumber numberStyle:(current application's NSNumberFormatterDecimalStyle)
	return theResult as text
end formatnumber:

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) lundi 30 mai 2016 18:57:11