# Trying to find display (screen) names

**URL:** <https://forum.xojo.com/t/trying-to-find-display-screen-names/59741>\
**Category:** macOS\
**Created:** [December 12, 2020, 7:00pm UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741 "2020-12-12T19:00:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Koperwhats](https://forum.xojo.com/user_avatar/forum.xojo.com/william_koperwhats/32/230_2.png) [@William\_Koperwhats](https://forum.xojo.com/u/William_Koperwhats)\
**Post date:** [December 12, 2020, 7:00pm UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/1 "2020-12-12T19:00:58Z")

</div>

Does anyone know how to detect the _names_ of connected screens (e.g. “Built-in Retina Display”, or “Thunderbolt Display”)? I am currently referring to them in my app as “Display 1”, “Display 2”, etc., but I’d like to present the user with something more useful. I haven’t found anything in the MBS suite with this ability. Cheers.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 12, 2020, 8:24pm UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/2 "2020-12-12T20:24:03Z")

</div>

we have several ways.

One is to use [NSScreenMBS](https://www.monkeybreadsoftware.net/class-nsscreenmbs.shtml) class and then query [localizedName](https://monkeybreadsoftware.net/cocoa-nsscreenmbs-property.shtml#7) property.

---

<div class="post-metadata">

**Author:** ![Jürg\_Otter](https://forum.xojo.com/user_avatar/forum.xojo.com/j%C3%BCrg_otter/32/584_2.png) [@Jürg\_Otter](https://forum.xojo.com/u/J%C3%BCrg_Otter)\
**Post date:** [December 12, 2020, 8:45pm UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/3 "2020-12-12T20:45:26Z")

</div>

And just for reference and for those who don’t want or can’t use a Plugin:

```
Dim sScreenLocalizedNames() As String

#If TargetMacOS Then
  Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
  Declare Function getScreens Lib "AppKit" selector "screens" (ptrToNSScreenClass As Ptr) As Ptr
  Declare Function getLocalizedName Lib "AppKit" selector "localizedName" (ptrToNSScreenInstance As Ptr) As CFStringRef
  
  Declare Function NSArrayCount Lib "Foundation" selector "count" (ptrToNSArray As Ptr) As UInteger
  Declare Function NSArrayObjectAtIndex Lib "Foundation" selector "objectAtIndex:" (ptrToNSArray As Ptr, index As UInteger) As Ptr
  
  Dim ptrNSScreenClass As Ptr = NSClassFromString("NSScreen")
  Dim ptrScreensArray As Ptr = getScreens(ptrNSScreenClass)
  Dim iScreensArrayCount As UInteger = NSArrayCount(ptrScreensArray)
  
  For i As Integer = 0 To iScreensArrayCount - 1
    Dim ptrToNSScreen As Ptr = NSArrayObjectAtIndex(ptrScreensArray, i)
    sScreenLocalizedNames.Append(getLocalizedName(ptrToNSScreen))
  Next
#EndIf

Me.Text = Join(sScreenLocalizedNames, ", ")

```

_Note: This code is just to get an idea which Declares are involved. Could need more error checking._

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 13, 2020, 2:51am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/4 "2020-12-13T02:51:19Z")

</div>

Also the function `localizedName` is macOS 10.15 or newer. I was looking for such a thing a couple of years ago and the amount of code required to this was a lot!

---

<div class="post-metadata">

**Author:** ![William\_Koperwhats](https://forum.xojo.com/user_avatar/forum.xojo.com/william_koperwhats/32/230_2.png) [@William\_Koperwhats](https://forum.xojo.com/u/William_Koperwhats)\
**Post date:** [December 13, 2020, 3:37am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/5 "2020-12-13T03:37:19Z")

</div>

Thanks for the replies, that was exactly what I was looking for. I guess that I can live with it being Catalina and up, I’ll just default back to the old nomenclature for older systems. Cheers.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 13, 2020, 3:50am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/6 "2020-12-13T03:50:33Z")

</div>

You should use `[NSObject respondsToSelector:]` to figure out if it is supported, at least that is what Apple recommends you do as opposed to checking for the OS version.

```
#if targetMacOS and targetDesktop then
  Dim s as integer = NSWindowScreen( self.handle )
  if NSObjectRespondsToSelector( s, NSSelectorFromString( "localizedName" ) ) then
    // --- Do thing with screen name
  else
    // --- Get screen name it the old long winded way.
  end if

#elseif targetWindows then
  // --- Do Windows thang...
#endIf
```

---

<div class="post-metadata">

**Author:** ![William\_Koperwhats](https://forum.xojo.com/user_avatar/forum.xojo.com/william_koperwhats/32/230_2.png) [@William\_Koperwhats](https://forum.xojo.com/u/William_Koperwhats)\
**Post date:** [December 13, 2020, 4:02am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/7 "2020-12-13T04:02:47Z")

</div>

Good point. Luckily this is a macOS only utility, so less to worry about. Thanks Sam.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 13, 2020, 4:44am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/8 "2020-12-13T04:44:33Z")

</div>

Not just to determine if this is running on a Mac or not, but also to determine that this function is available on a Mac.

In the recent years with Apple’s rabid pace of change, I have seen API that was only valid for one version of the OS, with it being deprecated the year after it was introduced.

---

<div class="post-metadata">

**Author:** ![DetlefK](https://forum.xojo.com/user_avatar/forum.xojo.com/detlefk/32/23653_2.png) [@DetlefK](https://forum.xojo.com/u/DetlefK)\
**Post date:** [December 13, 2020, 9:15am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/9 "2020-12-13T09:15:10Z")

</div>

I second the check for ability, as Sam’s thought is right in removing things.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 13, 2020, 10:03am UTC](https://forum.xojo.com/t/trying-to-find-display-screen-names/59741/10 "2020-12-13T10:03:48Z")

</div>

This one uses [CGDisplayMBS](https://www.monkeybreadsoftware.net/class-cgdisplaymbs.shtml) class and may work on older macOS versions:

```
Dim DisplayList() As CGDisplayMBS = CGDisplayMBS.GetActiveDisplayList()

For Each display As CGDisplayMBS In DisplayList
  
  Dim dict As Dictionary = display.Info
  Dim names As Dictionary = dict.Lookup( "DisplayProductName", Nil )
  If names <> Nil Then
    Dim name As String = names.Lookup("en_US", "?")
    MsgBox name+" "+display.PixelsWide.ToString+"x"+display.PixelsHigh.ToString
  end if
Next
```
