0

I want to change the mouse cursor to a laser pointer when someone clicks on a button using python script. How to do it on MAC?

In windows I have done like this to do the same

hnew = win32gui.LoadImage(0,  resource_path('laser.cur'), win32con.IMAGE_CURSOR, 0, 0, win32con.LR_LOADFROMFILE)
ctypes.windll.user32.SetSystemCursor(hnew, 32512)

But it is not supported on mac as it is window specific library.

1 Answers1

0

You need access to the Cocoa frameworks which seems possible with this library (not tested). Within Cocoa the object to call is NSCursor. In Objective-c you set the cursor like this :

[NSCursor.arrowCursor set]

You can also use a pair of command to change the cursor and reset it after the action :

[NSCursor.pointingHandCursor push]
// Do an action
[NSCursor pop]

To create a custom cursor, you use a NSImage you create from a path (here I do it with a pdf image to support high resolution screen) :

NSString * pathToAnImage = "A/B/C.pdf"
NSImage * image = [NSImage multiRepresentationImageFromPDF: pathToAnImage withMaxScaleFactor:4];
NSPoint hotSpot = NSMakePoint(8, 8); //the relative position within the image interpreted as the cursor location

NSCursor * customCursor = [[NSCursor alloc] initWithImage:image hotSpot:hotSpot]]

Then you can use it as any other system cursor.

dspr
  • 2,383
  • 2
  • 15
  • 19