The NSScreen screen property is nullable.  From the docs for NSWindow screen:

"The value of this property is the screen where most of the window is on; it is 
nil when the window is offscreen."

I would go with Steve’s suggestion as much as possible, The NSScreen class has 
the info you need.

Jack


> On Nov 17, 2022, at 10:44 AM, Gabriel Zachmann via Cocoa-dev 
> <cocoa-dev@lists.apple.com> wrote:
> 
> In my screensaver (macOS), 
> I am trying to retrieve the 'screen number' (NSScreenNumber) for the screen 
> on which I am running.
> 
> So, in   
>     - (void) viewWillMoveToWindow: (NSWindow *) newWindow
> I have this line of code:
>     displayID_ = [ [[newWindow screen] deviceDescription] objectForKey: 
> @"NSScreenNumber"];
> 
> This sometimes works (I get the correct screen number),
> and sometimes it does not, i.e., I get displayID_ = nil!
> (Usually after invoking some other screen savers in System Preferences.)
> 
> I don't see a pattern.
> 
> Curiously, these lines in viewWillMoveToWindow always work:
> 
>    for ( NSScreen * s in [NSScreen screens] )
>    {
>        NSDictionary<NSDeviceDescriptionKey, id> * device_description = [s 
> deviceDescription];
>        NSNumber * n = [device_description objectForKey: @"NSScreenNumber"];
>       [...]
>    }
> 
> I always get non-null and correct 'screen numbers' n.
> 
> 
> So, I am wondering, why does the first line sometimes NOT work?
> And what is the proper way to retrieve the screen number on which my screen 
> saver is running?
> Should I use [NSScreen mainScreen] ?
> 
> Please note, that there might be several monitors attached to the Mac!
> (Some users have up to 6)
> 
> Thanks a lot in advance.
> 
> Best regards, Gabriel
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/jackbrindle%40me.com
> 
> This email sent to jackbrin...@me.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to