Page 1 of 1

How to Get the Volume ID of a Hard Drive - Mac OSX

Posted: Fri Mar 17, 2017 10:52 pm
by PB2004
A few months ago I received some great help from Keya and TS-soft on getting the volume ID of a hard drive under Windows (see http://www.purebasic.fr/english/viewtop ... 13&t=66647).

I am now trying to port my PB app to Mac but the windows-specific code of course bombs. I am brand spanking-new to the Mac environment, but my Purebasic app works great in there except for this bit of code.

Does anyone have equivalent PB-insertable code to do the same thing? Thank you.

Re: How to Get the Volume ID of a Hard Drive - Mac OSX

Posted: Sat Mar 18, 2017 11:42 am
by Shardik
What drive information do you need? The Windows API examples from ts-soft demonstrate how to read the volume name (first example) and the volume serial number (second example).

I had already posted this example for MacOS that demonstrates how to get 16 attributes of all connected volumes including volume name and UUID. For your conveniance I have stripped down that example to only retrieve the volume name of all volumes. I have successfully tested the following example on MacOS 10.9.5 (Mavericks) and MacOS 10.12.3 (Sierra) with PB 5.44 x86 and x64 in ASCII and Unicode mode.

Code: Select all

EnableExplicit

If OSVersion() <= #PB_OS_MacOSX_10_5
  MessageRequester("Info",
    "Sorry, but this program needs at least OS X 10.6 (Snow Leopard) !")
  End
EndIf

Procedure DisplayVolumeNames()
  Protected FileManager.I
  Protected i.I
  Protected KeyArray.I
  Protected NumVolumes.I
  Protected URL.I
  Protected URLArray.I
  Protected VolumeName.S

  FileManager = CocoaMessage(0, 0, "NSFileManager defaultManager")

  If FileManager
    KeyArray = CocoaMessage(0, 0,
      "NSArray arrayWithObject:$", @"NSURLVolumeLocalizedNameKey")

    If KeyArray
      URLArray = CocoaMessage(0, FileManager,
        "mountedVolumeURLsIncludingResourceValuesForKeys:", KeyArray,
        "options:", 0)

      If URLArray
        NumVolumes = CocoaMessage(0, URLArray, "count")

        If NumVolumes > 0
          For i = 0 To NumVolumes - 1
            URL = CocoaMessage(0, URLArray, "objectAtIndex:", i)

            If URL
              VolumeName = PeekS(CocoaMessage(0, CocoaMessage(0,
                URL, "path"), "UTF8String"), -1, #PB_UTF8)
              VolumeName = StringField(VolumeName,
                CountString(VolumeName, "/") + 1, "/")

              If VolumeName = ""
                VolumeName = "/"
              EndIf

              Debug VolumeName
            EndIf
          Next i
        EndIf
      EndIf
    EndIf
  EndIf
EndProcedure

DisplayVolumeNames()

Re: How to Get the Volume ID of a Hard Drive - Mac OSX

Posted: Sat Mar 18, 2017 2:39 pm
by PB2004
FANTASTIC! Thank you so much.