Search code examples
macoscocoa

Get my Mac's computer name


How can I get the computer's name on a Mac? I'm talking about the same name as the one you can find in System Profiler under "Software".


Solution

  • Objective C

    The name I was looking for is:

    [[NSHost currentHost] localizedName];
    

    It returns "Jonathan's MacBook" rather than "Jonathans-Macbook", or "jonathans-macbook.local" which just name returns.

    Swift 3

    For Swift >= 3 use.

    if let deviceName = Host.current().localizedName {
       print(deviceName)
    }