Displaying hard drive capacity

Discussion in 'Mac Programming' started by alexcanton, Sep 25, 2013.

  1. alexcanton macrumors member

    alexcanton

    Joined:
    Jan 14, 2012
    Location:
    Gold Coast, AU
    #1
    Is there any code available to display a Mac's hard drive capacity?

    Thanks
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #4
    Applescript.

    Code:
    tell application "System Events"
    	set freeSpace to free space of disk "Macintosh HD" as real
    	return freeSpace
    end tell
    
    Like chown33 said, which language?

    Regards Mark
     
  4. alexcanton thread starter macrumors member

    alexcanton

    Joined:
    Jan 14, 2012
    Location:
    Gold Coast, AU
    #5
    Sorry I thought this was an Xcode forum. Objective C.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    The name of this forum is "Mac Programming". The Mac supports many different programming languages. So does Xcode.


    There is a C framework named "DiskArbitration". See here:
    https://developer.apple.com/library...s.html#//apple_ref/doc/uid/TP40009310-CH3-SW1

    You can get a DADiskRef, then get a CFDictionaryRef from the DADiskCopyDescription function. From that CFDictionaryRef (which is toll-free bridged to NSDictionary) you can then get any of the keys describing the disk. One such key is kDADiskDescriptionMediaSizeKey.

    For more info, read the header:
    You can find a complete list of properties in the DADisk.h header in the Disk Arbitration Framework, along with a description of the expected data types for the values of each key.
    Quoted from the subsection "Obtaining a Description Dictionary" of the Manipulating Disks reference linked above.

    Also look at the links to the framework function references. Each function has a link to a relevant Sample Code project. Look there, too.
     
  6. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #7
    Another way is to use the NSFileManager class.

    Something like:

    Code:
    unsigned long long fs_size;
    NSDictionary *fs_attrib = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[pathURL path] error:nil];
    fs_size = [[fs_attrib valueForKey:NSFileSystemSize] longLongValue];
    
    Fill in for the appropriate path. And you should probably actually check for an error. ;)
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    The attributesOfFileSystemForPath:error: method returns the attributes of a file-system, not a hard drive. For example, it won't account for other partitions, such as the recovery or EFI partitions. Also, if the disk has multiple user-defined partitions, it won't account for any of those.

    To illustrate the difference, copy & paste this command into a Terminal window:
    Code:
    diskutil list
    
    The item labeled "0:" will be the disk as a whole, i.e. the "hard drive". Each partition will also appear, i.e. the "file-systems" on the disk.

    The OP asked for "hard drive capacity", which I took to refer to the physical hard drive, not to partitions (file-systems) on the drive. I could be wrong, depending on what the OP actually intends to use the data for.
     

Share This Page