10

Is there a way to get OSX codename (I.e. Yosemite, El Capitan etc) from command line? I'm not looking for Version information (e.g.

sw_vers -productVersion

) which gives 10.10 etc Thanks!

lviggiani
  • 3,579
  • 2
    I think there’s no reliable way to do this from within bash (Terminal). Even system_profiler (which spits out a pretty exhaustive set of info) doesn’t include those names (which I think Apple calls the “marketing version”). Maybe you’re trying to solve a problem by looking for that codename? If so, be careful you’re not falling into the XY Problem • http://xyproblem.info/ – sideshowbarker Oct 06 '15 at 01:35
  • 1
    Aren't there only ten of them? Why not just get the version information and translate it using a hardcoded lookup table? – Wildcard Oct 06 '15 at 06:16
  • 1
    @wildcard: this is what I've been doing so far but every time a new version come out I have to update and redistribute my library... Or do you already know the future names? – lviggiani Oct 07 '15 at 21:55

6 Answers6

7

I'm sure there's got to be an easier and more-reliable way, but at least you can eliminate the pipe to sed altogether by using grep with -o (prints only matches) and -E (extended regular expressions):

grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf'

This does, however, also return the SOFTWARE LICENSE AGREEMENT FOR OS X portion of the output. If you just want the codename, you could pipe it to sed, but it would not require any back-references using the dreaded -E flag that BSD sed is so infamous for:

grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | sed "s/SOFT.*OS X //"

Personally, I prefer the awk method instead:

grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'OS X ' '{print $NF}'

Pure awk solution:

awk '/SOFTWARE LICENSE AGREEMENT FOR OS X/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'OS X ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}'

(I'm sure there's a way to do it without piping to additional awk processes, but I'm not a pro.)

rubynorails
  • 2,293
  • 2
    In at least macOS Sierra, you need to remove the " OS X" portion from these, as the line now reads SOFTWARE LICENSE AGREEMENT FOR macOS Sierra\. The filename thankfully remains the same, though. – ecmanaut Mar 03 '17 at 01:02
3

How about this :

$ sed -nE '/SOFTWARE LICENSE AGREEMENT FOR/s/([A-Za-z]+ ){5}|\\$//gp' /System/Library/CoreServices/Setup\ Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf
SebMa
  • 2,149
1

FWIW, here’s a fugly hacky way; it’s probably neither very forward- nor backward-compatible:

grep "SOFTWARE LICENSE AGREEMENT FOR OS X" \
  "/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf" \
  | sed -E 's/^SOFTWARE LICENSE AGREEMENT FOR OS X (.+)\\$/\1/'

It works at least on the El Capitan and Mavericks systems I checked (and so I would assume on Yosemite). It returns the marketing name in all caps; e.g., EL CAPITAN and MAVERICKS.

1

⚠️ This method no longer works.

From this blog post, I learned that Apple has a web service you can query for the marketing name of macOS for versions 10.7-10.12.6. You can supply it with the macOS version number you need the marketing name for and it will return XML with the marketing name.

curl -Ls "https://support-sp.apple.com/sp/product?edid=10.12.6"

Returns (formatted for display purposes)

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <name>CPU Name</name>
    <configCode>macOS Sierra</configCode>
    <locale>en_US</locale>
</root>

If you wish to get the marketing name for the version of macOS that you are currently running, you can run the one liner

curl -Ls "https://support-sp.apple.com/sp/product?edid=$(sw_vers -productVersion)" |
xmllint --xpath '/root/configCode/text()' -
n8felton
  • 111
0

Struggled for a consistent way to retrieve this information across OS X versions. Apple moved the location of: OSXSoftwareLicense.rtf in High Sierra. Rather than use regular expressions to hit a target that might move, I devised the following:

#!/bin/bash
# Retrieve Apple's marketing name for installed operating system 

# Take the number extracted from here; use it as a reference

osx_num=$(sw_vers -productVersion| awk -F '[.]' '{print $2}')

# Lookup table for OS X marketing names 

OSX_MARKETING=(
["10"]="Yosemite"
["11"]="El Capitan"
["12"]="Sierra"
["13"]="High Sierra"
)

# Check if the number extracted is in array; 
# if it is, print marketing name

osx_name () {
  if [[ -n "${OSX_MARKETING[$osx_num]}" ]]; then 
    printf "%s\\n" "${OSX_MARKETING[$osx_num]}"
fi
}

This works reliably on the four most-recent versions of OS X, and probably others.

marshki
  • 607
  • 1
  • 4
  • 14
0

Perl version:

perl -n -e 's/.*SOFTWARE LICENSE AGREEMENT FOR (?i:macOS|(?:mac\s?)?OS\s*X)\s+(\w+(\s\w+)*).*/$1/ and print' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf'
  • The Perl command you provided relies on the presence of the string macOS in the software license agreement file, which may not be present or formatted consistently across all versions of OSX – karel Dec 14 '23 at 20:16
  • 1
    Yes, true. The command was also failing to work if the OS name was more than one word. I've edited it. It now works at least back to High Sierra. It might work for older versions of macOS or OS X. It seems crazy that we have to parse a software license to get this information. – Marnix A. van Ammers Dec 15 '23 at 21:24