How to change the size of the system cursor in a m

2019-05-17 16:20发布

问题:

I've tried to set it's global size using this code:

-(void)setOption {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);
}

And I can see in the NSLog that it changed it but I don't know how to restart/reset the system cursor in order for the cursor to change to the specified size.

Does anybody know a better way to change it's size programmatically or how to restart the system cursor after the defaults change?

EDIT(about duplication): My question is unique because I can't use applescript in resolving this like the answer provided in the other topic. Also the topic has been created in 2013, and seems outdated. Maybe things have changed a little since then. Also maybe Swift would be a viable solution for resolving this. Who knows? All these arguments make it clear that this is not a duplicate question.

回答1:

CGError state = CGSShowCursor(CGSDefaultConnection) ;
    if (state != kCGErrorSuccess) NSLOG(@"error : %d",state);

maybe try it with CGSShowCursor(CGSMainConnectionID())

This might also help : https://github.com/alexzielenski/Mousecape/blob/1d534b1e076b07a01b80364be23c88c8439028bc/Mousecape/mousecloak/NSCursor_Private.h



回答2:

Warning. This code is not based on what is saved in preferences so combine it:

  float cursorScale = 2;
  cursorScale = MAX(1, MIN(cursorScale,4));
  int connectionID = CGSMainConnectionID();
  CGSSetCursorScale(connectionID, cursorScale);

to get the size

  CGSGetCursorScale(connectionID, &cursorScale);

,

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);

CREDITS: Alex Zielenski