Colour of Pixel Under Mouse Pointer

Function: Returns the RGBA colour of the pixel under the mouse cursor, copying the values to the clipboard and momentarily displaying them on screen.

Trigger: Hold down control (⌃) and tap the left command (⌘) key.

Colour of Pixel Under Mouse.kmmacros (4.7 KB)


This macro looks really interesting even though it doesn't seem to work for me. Moreover, I would like it to return the Hex color code instead of the RGBA. Any help?

I would need more information. Saying it doesn't work doesn't provide a starting point from which I can attempt to fix whatever it is that' stopping it from performing as one would expect on your system.

Can you provide me with some debugging information ? My first recommendation would be to try using a simple Hotkey trigger to make sure the macro is being triggered, just in case the device key trigger isn't recognised on your system from it having a different keyboard device to the one I used at the time of registering it to trigger this macro.

Once you can be confident the macro is being triggered, and that the problem is occurring somewhere in the course of its execution, can you then tell me what value gets copied to the clipboard or displayed in large text, if any ? If this doesn't happen, can you trigger the JavaScript action by itself and check what value gets stored in the variable RGBa (or whatever you might have named it to) ? I'd recommend pressing the cog to bring up the menu options for that action, and making sure the following three options are checked: Include Errors, Failure Aborts Macro, and Notify on Failure. This should mean that, even if the script throws an error, the details of that error will get stored in the RGBa variable, which I will need in order to understand what's going wrong for you.

I am assuming you're running Mojave, and so I will probably need you to be willing to test three of the JavaScript commands that I suspect are the most likely candidates for any scripting errors to arise from. But I'll explain which ones and how to do that once it's clear what the actual problem is.

Once fixed, getting it to return hex values won't be a problem at all.

Hi @CJK,

This is the result I receive:

2019-09-20 14:55:56.521 osascript[10899:2119215] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}''
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff3ef032fd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff695d0a17 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff3ee58c05 __NSGetSizeAndAlignment + 1098
	3   CoreFoundation                      0x00007fff3ee58a6f __NSGetSizeAndAlignment + 692
	4   CoreFoundation                      0x00007fff3ee65ccd NSGetSizeAndAlignment + 33
	5   JavaScriptAppleEvents               0x00007fff5c784d56 JSOCObjCTypeIterateTypes + 136
	6   JavaScriptAppleEvents               0x00007fff5c784e4a JSOCObjCTypeIterateStructElements + 131
	7   JavaScriptAppleEvents               0x00007fff5c7a442d +[JSOCType typeWithObjCType:] + 456
	8   JavaScriptAppleEvents               0x00007fff5c782ade -[JSOCBridgeSupport typeForArgumentInfo:fallbackObjCType:] + 358
	9   JavaScriptAppleEvents               0x00007fff5c782eab -[JSOCBridgeSupport typeForSelectorInfo:typeEncoding:] + 97
	10  JavaScriptAppleEvents               0x00007fff5c783583 -[JSOCBridgeSupport typeForClass:selector:] + 409
	11  JavaScriptAppleEvents               0x00007fff5c783605 -[JSOCBridgeSupport functionPointerForTarget:selector:superclassDepth:] + 74
	12  JavaScriptAppleEvents               0x00007fff5c7b1bdc JSOCInvokeMethod + 652
	13  JavaScriptAppleEvents               0x00007fff5c77dd9a __JSOCObjectGetPropertyBlock_block_invoke + 746
	14  CoreFoundation                      0x00007fff3ee5c0cc __invoking___ + 140
	15  CoreFoundation                      0x00007fff3ee5bf97 -[NSInvocation invoke] + 311
	16  JavaScriptCore                      0x00007fff424d6028 _ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6_ + 440
	17  JavaScriptCore                      0x00007fff424d5a47 _ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6_ + 247
	18  JavaScriptCore                      0x00007fff424d52ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
	19  JavaScriptCore                      0x00007fff424b3c08 vmEntryToNative + 206
	20  JavaScriptCore                      0x00007fff4214288a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
	21  JavaScriptCore                      0x00007fff42c012f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
	22  JavaScriptCore                      0x00007fff42142508 JSObjectCallAsFunction + 488
	23  JavaScriptAppleEvents               0x00007fff5c786337 JSAEObjectGetProperty + 282
	24  JavaScriptCore                      0x00007fff424eb97b _ZN3JSC16JSCallbackObjectINS_20JSDestructibleObjectEE18getOwnPropertySlotEPNS_8JSObjectEPNS_9ExecStateENS_12PropertyNameERNS_12PropertySlotE + 395
	25  JavaScriptCore                      0x00007fff422a9dff llint_slow_path_get_by_id + 5519
	26  JavaScriptCore                      0x00007fff424b9f25 llint_entry + 25234
	27  JavaScriptCore                      0x00007fff424b3ad9 vmEntryToJavaScript + 200
	28  JavaScriptCore                      0x00007fff42a1370c _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 10972
	29  JavaScriptCore                      0x00007fff42c2811a _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 298
	30  JavaScriptCore                      0x00007fff42128bc4 JSEvaluateScript + 676
	31  JavaScriptCore                      0x00007fff424cc582 -[JSContext evaluateScript:withSourceURL:] + 194
	32  JavaScriptOSA                       0x00007fff5c7c8146 -[JSStorage(JSProcedures) evaluateScript:inContextID:] + 737
	33  JavaScriptOSA                       0x00007fff5c7cbe27 -[JSStorage(JSProcedures) handleEvent:inContext:modeFlags:resultingResultID:] + 588
	34  CoreFoundation                      0x00007fff3ee5c0cc __invoking___ + 140
	35  CoreFoundation                      0x00007fff3ee5bf97 -[NSInvocation invoke] + 311
	36  JavaScript                          0x00000001063e17bf JavaScriptComponent + 1135
	37  OpenScripting                       0x00007fff3e448922 OSAExecuteEvent + 52
	38  osascript                           0x0000000104d4d5a0 osascript + 9632
	39  libdyld.dylib                       0x00007fff6ad9e3d5 start + 1
	40  ???                                 0x0000000000000004 0x0 + 4
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Thank you, that's really helpful. I've actually come across this error before from another script that someone had the same issue with, but I think couldn't be bothered to go through the steps at his end to help debug it, so I was never able to provide him with a fix.

It is a Mojave JXA bug, where it appears to be incorrectly converting the data types from the sort that JavaScript uses to the sort that C or Objective-C uses. In this case, it appears to be one of the arguments passed to one of the functions that's the problem, and it occurs when that function is invoking another function behind the scenes called NSGetSizeAndAlignment(), at which point the JavaScript engine is crashing.

I'm don't know for certain which function is the guilty culprit. However, since NSGetSizeAndAlignment() is an Objective-C Foundation method (starting with "NS..."), my first guess is that it might be the NSScreen class that's calling it in order to obtain the dimensions of your display.

My second suspect would be CGRectMake(), which also deals with arguments that specify the size and position of something on the screen, however "CG..."functions belong to Core Graphics, and these use Core Foundation data types, which existed before Foundation data types came along, so it would be surprising—but not impossible—if was making use of a Foundation class method, as it's typically the other way round (Foundation class methods calling Core Foundation functions in the background).

Anyway, that's all probably sounds very dry to you. If you're happy to, let's find out if NSScreen is being naughty... Can you delete the entire text in the JavaScript action, and replace it with this:

ObjC.import('Cocoa');

$.NSScreen.mainScreen.frame.size.height;

Run that action with just those two lines, and let me know what gets stored in the RGBa variable. It's best to go into Preferences... and delete the current contents of the RGBa variable first, because I'm hoping that the variable will end up being given the same error message you're getting now, and if there's anything that simply stops the script from running before the action writes out to the variable, it'll contain the same data as before and be misleading false positive test.

If the variable contains the height of your screen, then could you re-run the test with this script instead:

ObjC.import('Cocoa');

$.CGRectMake(10, 10, 1, 1);

If both of those work successfully, then my next step would be to do this test with every single line individually until I get one that throws the error (you can see why the previous user a while back probably just scoffed at this idea and thought "ah, fuck it, I'll find another way", so I don't blame them. But if I had Mojave, I can actually run the script myself in a step-wise fashion using my own debugging routine that would take about a minute to locate the problem, but it's these scripting bugs that Apple are too ambivalent to bother fixing that made me not want to upgrade to Mojave in the first place). Don't do this yourself, but let me know if you're willing and I'll tell you the order in which to test each function as some can't be tested by themselves on a single line and need to be tested in conjunction with another function that first needs vetting before testing it with another, etc. etc.

Hi @CJK,

I put a Set Variable “RGBa” to Text %Delete% at the beginning of the macro and replace the text in the JavaScript action with:

ObjC.import('Cocoa');

$.NSScreen.mainScreen.frame.size.height;

And the RGBa variable returns:

2019-09-23 09:34:46.065 osascript[1590:31413] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}''
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff4ca202fd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff770eda17 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff4c975c05 __NSGetSizeAndAlignment + 1098
	3   CoreFoundation                      0x00007fff4c975a6f __NSGetSizeAndAlignment + 692
	4   CoreFoundation                      0x00007fff4c982ccd NSGetSizeAndAlignment + 33
	5   JavaScriptAppleEvents               0x00007fff6a2a1d56 JSOCObjCTypeIterateTypes + 136
	6   JavaScriptAppleEvents               0x00007fff6a2a1e4a JSOCObjCTypeIterateStructElements + 131
	7   JavaScriptAppleEvents               0x00007fff6a2c142d +[JSOCType typeWithObjCType:] + 456
	8   JavaScriptAppleEvents               0x00007fff6a29fade -[JSOCBridgeSupport typeForArgumentInfo:fallbackObjCType:] + 358
	9   JavaScriptAppleEvents               0x00007fff6a29feab -[JSOCBridgeSupport typeForSelectorInfo:typeEncoding:] + 97
	10  JavaScriptAppleEvents               0x00007fff6a2a0583 -[JSOCBridgeSupport typeForClass:selector:] + 409
	11  JavaScriptAppleEvents               0x00007fff6a2a0605 -[JSOCBridgeSupport functionPointerForTarget:selector:superclassDepth:] + 74
	12  JavaScriptAppleEvents               0x00007fff6a2cebdc JSOCInvokeMethod + 652
	13  JavaScriptAppleEvents               0x00007fff6a29ad9a __JSOCObjectGetPropertyBlock_block_invoke + 746
	14  CoreFoundation                      0x00007fff4c9790cc __invoking___ + 140
	15  CoreFoundation                      0x00007fff4c978f97 -[NSInvocation invoke] + 311
	16  JavaScriptCore                      0x00007fff4fff3028 _ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6_ + 440
	17  JavaScriptCore                      0x00007fff4fff2a47 _ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6_ + 247
	18  JavaScriptCore                      0x00007fff4fff22ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
	19  JavaScriptCore                      0x00007fff4ffd0c08 vmEntryToNative + 206
	20  JavaScriptCore                      0x00007fff4fc5f88a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
	21  JavaScriptCore                      0x00007fff5071e2f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
	22  JavaScriptCore                      0x00007fff4fc5f508 JSObjectCallAsFunction + 488
	23  JavaScriptAppleEvents               0x00007fff6a2a3337 JSAEObjectGetProperty + 282
	24  JavaScriptCore                      0x00007fff5000897b _ZN3JSC16JSCallbackObjectINS_20JSDestructibleObjectEE18getOwnPropertySlotEPNS_8JSObjectEPNS_9ExecStateENS_12PropertyNameERNS_12PropertySlotE + 395
	25  JavaScriptCore                      0x00007fff4fdc6dff llint_slow_path_get_by_id + 5519
	26  JavaScriptCore                      0x00007fff4ffd6f25 llint_entry + 25234
	27  JavaScriptCore                      0x00007fff4ffd0ad9 vmEntryToJavaScript + 200
	28  JavaScriptCore                      0x00007fff5053070c _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 10972
	29  JavaScriptCore                      0x00007fff5074511a _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 298
	30  JavaScriptCore                      0x00007fff4fc45bc4 JSEvaluateScript + 676
	31  JavaScriptCore                      0x00007fff4ffe9582 -[JSContext evaluateScript:withSourceURL:] + 194
	32  JavaScriptOSA                       0x00007fff6a2e5146 -[JSStorage(JSProcedures) evaluateScript:inContextID:] + 737
	33  JavaScriptOSA                       0x00007fff6a2e8e27 -[JSStorage(JSProcedures) handleEvent:inContext:modeFlags:resultingResultID:] + 588
	34  CoreFoundation                      0x00007fff4c9790cc __invoking___ + 140
	35  CoreFoundation                      0x00007fff4c978f97 -[NSInvocation invoke] + 311
	36  JavaScript                          0x000000010e0417bf JavaScriptComponent + 1135
	37  OpenScripting                       0x00007fff4bf65922 OSAExecuteEvent + 52
	38  osascript                           0x000000010dc655a0 osascript + 9632
	39  libdyld.dylib                       0x00007fff788bb3d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

And with:

ObjC.import('Cocoa');

$.CGRectMake(10, 10, 1, 1);

It returns:

2019-09-23 09:41:45.347 osascript[1732:38771] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}''
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff4ca202fd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff770eda17 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff4c975c05 __NSGetSizeAndAlignment + 1098
	3   CoreFoundation                      0x00007fff4c975a6f __NSGetSizeAndAlignment + 692
	4   CoreFoundation                      0x00007fff4c982ccd NSGetSizeAndAlignment + 33
	5   JavaScriptAppleEvents               0x00007fff6a2a1d56 JSOCObjCTypeIterateTypes + 136
	6   JavaScriptAppleEvents               0x00007fff6a2a1e4a JSOCObjCTypeIterateStructElements + 131
	7   JavaScriptAppleEvents               0x00007fff6a2c142d +[JSOCType typeWithObjCType:] + 456
	8   JavaScriptAppleEvents               0x00007fff6a29fb52 -[JSOCBridgeSupport typeForArgumentInfo:fallbackObjCType:] + 474
	9   JavaScriptAppleEvents               0x00007fff6a29fbf4 -[JSOCBridgeSupport typeForFunctionInfo:isBlock:] + 99
	10  JavaScriptAppleEvents               0x00007fff6a2a0750 -[JSOCBridgeSupport functionPointerForFunctionWithName:] + 89
	11  JavaScriptAppleEvents               0x00007fff6a29b43c __JSOCFunctionConstructorBlock_block_invoke + 304
	12  CoreFoundation                      0x00007fff4c9790cc __invoking___ + 140
	13  CoreFoundation                      0x00007fff4c978f97 -[NSInvocation invoke] + 311
	14  JavaScriptCore                      0x00007fff4fff3028 _ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6_ + 440
	15  JavaScriptCore                      0x00007fff4fff2a47 _ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6_ + 247
	16  JavaScriptCore                      0x00007fff4fff22ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
	17  JavaScriptCore                      0x00007fff4ffd0c08 vmEntryToNative + 206
	18  JavaScriptCore                      0x00007fff4fc5f88a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
	19  JavaScriptCore                      0x00007fff5071e2f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
	20  JavaScriptCore                      0x00007fff4fc5f508 JSObjectCallAsFunction + 488
	21  JavaScriptCore                      0x00007fff4fda9af6 -[JSValue callWithArguments:] + 278
	22  JavaScriptAppleEvents               0x00007fff6a2a8690 __JSOCDollarObject_block_invoke_2 + 357
	23  CoreFoundation                      0x00007fff4c9790cc __invoking___ + 140
	24  CoreFoundation                      0x00007fff4c978f97 -[NSInvocation invoke] + 311
	25  JavaScriptCore                      0x00007fff4fff3028 _ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6_ + 440
	26  JavaScriptCore                      0x00007fff4fff2a47 _ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6_ + 247
	27  JavaScriptCore                      0x00007fff4fff22ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
	28  JavaScriptCore                      0x00007fff4ffd0c08 vmEntryToNative + 206
	29  JavaScriptCore                      0x00007fff4fc5f88a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
	30  JavaScriptCore                      0x00007fff5071e2f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
	31  JavaScriptCore                      0x00007fff4fc5f508 JSObjectCallAsFunction + 488
	32  JavaScriptAppleEvents               0x00007fff6a2a8189 GetProperty + 309
	33  JavaScriptCore                      0x00007fff5000d6f1 _ZN3JSC16JSCallbackObjectINS_20JSDestructibleObjectEE14callbackGetterEPNS_9ExecStateExNS_12PropertyNameE + 225
	34  JavaScriptCore                      0x00007fff4fdc6ffe llint_slow_path_get_by_id + 6030
	35  JavaScriptCore                      0x00007fff4ffd6f25 llint_entry + 25234
	36  JavaScriptCore                      0x00007fff4ffd0ad9 vmEntryToJavaScript + 200
	37  JavaScriptCore                      0x00007fff5053070c _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 10972
	38  JavaScriptCore                      0x00007fff5074511a _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 298
	39  JavaScriptCore                      0x00007fff4fc45bc4 JSEvaluateScript + 676
	40  JavaScriptCore                      0x00007fff4ffe9582 -[JSContext evaluateScript:withSourceURL:] + 194
	41  JavaScriptOSA                       0x00007fff6a2e5146 -[JSStorage(JSProcedures) evaluateScript:inContextID:] + 737
	42  JavaScriptOSA                       0x00007fff6a2e8e27 -[JSStorage(JSProcedures) handleEvent:inContext:modeFlags:resultingResultID:] + 588
	43  CoreFoundation                      0x00007fff4c9790cc __invoking___ + 140
	44  CoreFoundation                      0x00007fff4c978f97 -[NSInvocation invoke] + 311
	45  JavaScript                          0x0000000103bca7bf JavaScriptComponent + 1135
	46  OpenScripting                       0x00007fff4bf65922 OSAExecuteEvent + 52
	47  osascript                           0x00000001039ee5a0 osascript + 9632
	48  libdyld.dylib                       0x00007fff788bb3d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Sorry for the delay. I've been away.

That's great news regarding the error thrown by NSScreen. I'm a bit disappointed by the apparent error thrown by CGRectMake(), but also a little confused how remarkably identical those two error messages appear. Did you clear the variable between the two macro executions, or might the error message from CGRectMake() possibly be the one left behind by NSScreen ?

I'm mindful I've not attended to this in a few days, and I can appreciate that line-by-line debugging is not something you wish to wait even more days for feedback.

I've given some thought to the more likely contenders of possible/probable misbehaving lines of code, and adjusted the script accordingly. This isn't how debugging is typically advised being done, because if the script still produces an exception, that leads us back to this spot. But, I also feel that, if CGRectMake() was responsible for that second error message, then it could be that Mojave has fundamentally broken core elements of the JavaScript-Objective-C bridge that might make debugging fruitless (I am, of course, hypothesising).

I've uploaded the macro to replace the link in the original post #1, and updated the screenshot as well.
Nope. I don't seem to be allowed to edit the original post. Will upload anew.

Here's the updated JavaScript code:

ObjC.import('CoreGraphics');
ObjC.import('AppKit');

nil = $();

(() => {
	const mainDisplayID = $.CGMainDisplayID(),
	      displayHeight = $.CGDisplayPixelsHigh(mainDisplayID);

	var mouse = $.NSEvent.mouseLocation;
	mouse.y   = displayHeight - mouse.y;

	const rect    = $.NSMakeRect(mouse.x, mouse.y, 1.0, 1.0);
	// const rect    = $.NSRectFromString([mouse.x, mouse.y, 1.0, 1.0].join(','));
	// const rect    = { "origin":{"x": mouse.x, "y": mouse.y},
	//                    "size":{"width": 1.0, "height": 1.0} };

	const image   = $.CGDisplayCreateImageForRect(mainDisplayID,rect),	      bitmap  = $.NSBitmapImageRep.alloc.initWithCGImage(image),
	      color   = bitmap.colorAtXY(0, 0);

	$.CGImageRelease(image);
	bitmap.release;

	var r = Ref(),
	    g = Ref(),
	    b = Ref(),
	    a = Ref();

	color.getRedGreenBlueAlpha(r, g, b, a);
	return [r[0], g[0], b[0], a[0]];
})();

Key changes I made:

  • Import Core Graphics rather than Cocoa; also import AppKit (for mouse location);
  • Display height now retrieved using a Core Graphics function instead of a Foundation class property;
  • The bounding rectangle (previously constructed with CGRectMake()) is now constructed using a Foundation function, which I hope avoids the errors in casting from CG to NS data types. Instead, the NS get cast to CG. Additionally, have included two alternative methods to create the bounding rectangle, currently commented out (have only one of these methods uncommented for use, noting that the last method spans over two lines in which I declare the bounds of the rectangle explicitly).
  • The macro now also produces hex RGBA output.

Version 2b

Function: Returns the RGBA colour of the pixel under the mouse cursor, copying the values to the clipboard and momentarily displaying them on screen.

Trigger: Hold down control (⌃) and tap the left command (⌘) key.

Colour of Pixel Under Mouse.kmmacros (5.7 KB) [updated 2019-09-28]



Summary of changes:

  1. Imports Core Graphics rather than Cocoa; also imports AppKit;

  2. Display height now retrieved using a Core Graphics function instead of a Foundation class property;

  3. CGRectMake() now replaced by NSMakeRect(). Additionally, two alternative methods to create a bounding rectangle provide backup options in light of Mojave's crashing of the JavaScriptOSA engine.

  4. The macro now outputs a hexadecimal RGBA representation, e.g. B8EBF4FF → clipboard, printed in large type on screen as #B8EBF4FF.

I'm sure most people won't care about this alternative method, but it's shorter and uses only KM actions:

image

1 Like

Ours both have 4 actions (excluding my info box, which is a non-functional element).

So does mine.

I think alternative methods are always welcome. For me, I had no idea that application existed, and think I just examined the colour of every pixel on my desktop. It also fits in with being your knack for keeping things simple, which is a good thing.

As a small, but potentially significant, critique, it is currently specific to one's system display size, and it's worth noting (before anyone replies with "it doesn't work") that the OCR area will likely need adjusted by the user to target the appropriate area on their screen.

However, if you wanted to eliminate this variable, then you can do this:

AppleScript
activate application id "com.apple.DigitalColorMeter"

tell application id "com.apple.systemevents" to ¬
	repeat until the bundle identifier of every process ¬
		contains "com.apple.DigitalColorMeter"
		delay 0.5
	end repeat

It's surprisingly swift, and probably most people won't mind a brief flash of the Digital Colour Meter application appearing and disappearing, although it's what would prevent me personally going this route.

However, you've also given me another idea I might toy around with.

1 Like

Hehe, true. And wow, you improved on my idea using menu items. Dang, I've been upstaged again!

Actually, my screenshot shows 5 actions (the group action) so you actually win.

I didn't test it on other screen sizes. I could have used some math to make it work on multiples sizes. And OCR is quite robust when it comes to font sizes.

Glad you’re importing Core Graphics rather than Cocoa as I thought the latter went away in Catalina (but I might’ve been listening with my elbows). :slight_smile:

Cocoa is name given to the trio of frameworks: Foundation, AppKit, and Core Data. I would be very surprised if it was suddenly scrapped, as there is no other API I am aware of teed up to supersede it, and a lot of the Core Foundation functions have been deprecated for a while in favour of Cocoa implementations.

That said, I don't know anything about Catalina and have remained on High Sierra so far.

1 Like

Thanks to everyone. Still no luck with the JavaScript. Here is the error:

2019-09-30 16:11:23.437 osascript[3311:162228] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}''
*** First throw call stack:
(
0 CoreFoundation 0x00007fff50c0ca7d __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff7b2dea17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff50b62365 __NSGetSizeAndAlignment + 1098
3 CoreFoundation 0x00007fff50b621cf __NSGetSizeAndAlignment + 692
4 CoreFoundation 0x00007fff50b6f42d NSGetSizeAndAlignment + 33
5 JavaScriptAppleEvents 0x00007fff6e492d56 JSOCObjCTypeIterateTypes + 136
6 JavaScriptAppleEvents 0x00007fff6e492e4a JSOCObjCTypeIterateStructElements + 131
7 JavaScriptAppleEvents 0x00007fff6e4b242d +[JSOCType typeWithObjCType:] + 456
8 JavaScriptAppleEvents 0x00007fff6e490b52 -[JSOCBridgeSupport typeForArgumentInfo:fallbackObjCType:] + 474
9 JavaScriptAppleEvents 0x00007fff6e490bf4 -[JSOCBridgeSupport typeForFunctionInfo:isBlock:] + 99
10 JavaScriptAppleEvents 0x00007fff6e491750 -[JSOCBridgeSupport functionPointerForFunctionWithName:] + 89
11 JavaScriptAppleEvents 0x00007fff6e48c43c _JSOCFunctionConstructorBlock_block_invoke + 304
12 CoreFoundation 0x00007fff50b6582c invoking
+ 140
13 CoreFoundation 0x00007fff50b656f7 -[NSInvocation invoke] + 311
14 JavaScriptCore 0x00007fff541e0028 ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6 + 440
15 JavaScriptCore 0x00007fff541dfa47 ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6 + 247
16 JavaScriptCore 0x00007fff541df2ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
17 JavaScriptCore 0x00007fff541bdc08 vmEntryToNative + 206
18 JavaScriptCore 0x00007fff53e4c88a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
19 JavaScriptCore 0x00007fff5490b2f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
20 JavaScriptCore 0x00007fff53e4c508 JSObjectCallAsFunction + 488
21 JavaScriptCore 0x00007fff53f96af6 -[JSValue callWithArguments:] + 278
22 JavaScriptAppleEvents 0x00007fff6e499690 _JSOCDollarObject_block_invoke_2 + 357
23 CoreFoundation 0x00007fff50b6582c invoking
+ 140
24 CoreFoundation 0x00007fff50b656f7 -[NSInvocation invoke] + 311
25 JavaScriptCore 0x00007fff541e0028 ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6 + 440
26 JavaScriptCore 0x00007fff541dfa47 ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6 + 247
27 JavaScriptCore 0x00007fff541df2ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
28 JavaScriptCore 0x00007fff541bdc08 vmEntryToNative + 206
29 JavaScriptCore 0x00007fff53e4c88a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
30 JavaScriptCore 0x00007fff5490b2f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
31 JavaScriptCore 0x00007fff53e4c508 JSObjectCallAsFunction + 488
32 JavaScriptAppleEvents 0x00007fff6e499189 GetProperty + 309
33 JavaScriptCore 0x00007fff541fa6f1 _ZN3JSC16JSCallbackObjectINS_20JSDestructibleObjectEE14callbackGetterEPNS_9ExecStateExNS_12PropertyNameE + 225
34 JavaScriptCore 0x00007fff53fb3ffe llint_slow_path_get_by_id + 6030
35 JavaScriptCore 0x00007fff541c3f25 llint_entry + 25234
36 JavaScriptCore 0x00007fff541cce9f llint_entry + 61964
37 JavaScriptCore 0x00007fff541bdad9 vmEntryToJavaScript + 200
38 JavaScriptCore 0x00007fff5471d70c _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 10972
39 JavaScriptCore 0x00007fff5493211a ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 298
40 JavaScriptCore 0x00007fff53e32bc4 JSEvaluateScript + 676
41 JavaScriptCore 0x00007fff541d6582 -[JSContext evaluateScript:withSourceURL:] + 194
42 JavaScriptOSA 0x00007fff6e4d6146 -[JSStorage(JSProcedures) evaluateScript:inContextID:] + 737
43 JavaScriptOSA 0x00007fff6e4d9e27 -[JSStorage(JSProcedures) handleEvent:inContext:modeFlags:resultingResultID:] + 588
44 CoreFoundation 0x00007fff50b6582c invoking
+ 140
45 CoreFoundation 0x00007fff50b656f7 -[NSInvocation invoke] + 311
46 JavaScript 0x00000001034067bf JavaScriptComponent + 1135
47 OpenScripting 0x00007fff50151922 OSAExecuteEvent + 52
48 osascript 0x0000000101d8e5a0 osascript + 9632
49 libdyld.dylib 0x00007fff7caac3d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

On the other hand, the Digital Color Meter approach is interesting and… working :smile: So I can already use this method.

Thanks for using it! It's not really a great solution because it has some issues with speed, flexibility and robustness. It's almost more of an example solution. But if it works, it works.

Thanks for feeding back. Can I just get confirmation that this occurred with each of the three different forms of rect ?

If so, either the bounding rectangle needed can't be created; or the crash occurs before this; or, of course, both. Either way, it's an issue with the JavaScript/osascript engine in Mojave, and I don't see the likelihood of a positive outcome being too low to make it worth asking you to debug this script to death (and one shouldn't have to, as it's a perfectly fine script, and Apple ought to be doing the work rather than us).

I'm glad there's an alternative you're using. Thanks for taking time to help problem-solve with me.

FYI, I commented/uncommented the three different forms of rect.

The first one ($.NSMakeRect) returned the following error:

2019-10-01 09:57:34.795 osascript[2820:93716] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}''
*** First throw call stack:
(
0 CoreFoundation 0x00007fff376e3a7d __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff61db5a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff37639365 __NSGetSizeAndAlignment + 1098
3 CoreFoundation 0x00007fff376391cf __NSGetSizeAndAlignment + 692
4 CoreFoundation 0x00007fff3764642d NSGetSizeAndAlignment + 33
5 JavaScriptAppleEvents 0x00007fff54f69d56 JSOCObjCTypeIterateTypes + 136
6 JavaScriptAppleEvents 0x00007fff54f69e4a JSOCObjCTypeIterateStructElements + 131
7 JavaScriptAppleEvents 0x00007fff54f8942d +[JSOCType typeWithObjCType:] + 456
8 JavaScriptAppleEvents 0x00007fff54f67b52 -[JSOCBridgeSupport typeForArgumentInfo:fallbackObjCType:] + 474
9 JavaScriptAppleEvents 0x00007fff54f67bf4 -[JSOCBridgeSupport typeForFunctionInfo:isBlock:] + 99
10 JavaScriptAppleEvents 0x00007fff54f68750 -[JSOCBridgeSupport functionPointerForFunctionWithName:] + 89
11 JavaScriptAppleEvents 0x00007fff54f6343c _JSOCFunctionConstructorBlock_block_invoke + 304
12 CoreFoundation 0x00007fff3763c82c invoking
+ 140
13 CoreFoundation 0x00007fff3763c6f7 -[NSInvocation invoke] + 311
14 JavaScriptCore 0x00007fff3acb7028 ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6 + 440
15 JavaScriptCore 0x00007fff3acb6a47 ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6 + 247
16 JavaScriptCore 0x00007fff3acb62ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
17 JavaScriptCore 0x00007fff3ac94c08 vmEntryToNative + 206
18 JavaScriptCore 0x00007fff3a92388a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
19 JavaScriptCore 0x00007fff3b3e22f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
20 JavaScriptCore 0x00007fff3a923508 JSObjectCallAsFunction + 488
21 JavaScriptCore 0x00007fff3aa6daf6 -[JSValue callWithArguments:] + 278
22 JavaScriptAppleEvents 0x00007fff54f70690 _JSOCDollarObject_block_invoke_2 + 357
23 CoreFoundation 0x00007fff3763c82c invoking
+ 140
24 CoreFoundation 0x00007fff3763c6f7 -[NSInvocation invoke] + 311
25 JavaScriptCore 0x00007fff3acb7028 ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6 + 440
26 JavaScriptCore 0x00007fff3acb6a47 ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6 + 247
27 JavaScriptCore 0x00007fff3acb62ec _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExecStateE + 492
28 JavaScriptCore 0x00007fff3ac94c08 vmEntryToNative + 206
29 JavaScriptCore 0x00007fff3a92388a _ZN3JSC11Interpreter11executeCallEPNS_9ExecStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 538
30 JavaScriptCore 0x00007fff3b3e22f4 _ZN3JSC12profiledCallEPNS_9ExecStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 196
31 JavaScriptCore 0x00007fff3a923508 JSObjectCallAsFunction + 488
32 JavaScriptAppleEvents 0x00007fff54f70189 GetProperty + 309
33 JavaScriptCore 0x00007fff3acd16f1 _ZN3JSC16JSCallbackObjectINS_20JSDestructibleObjectEE14callbackGetterEPNS_9ExecStateExNS_12PropertyNameE + 225
34 JavaScriptCore 0x00007fff3aa8affe llint_slow_path_get_by_id + 6030
35 JavaScriptCore 0x00007fff3ac9af25 llint_entry + 25234
36 JavaScriptCore 0x00007fff3aca3e9f llint_entry + 61964
37 JavaScriptCore 0x00007fff3ac94ad9 vmEntryToJavaScript + 200
38 JavaScriptCore 0x00007fff3b1f470c _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 10972
39 JavaScriptCore 0x00007fff3b40911a ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 298
40 JavaScriptCore 0x00007fff3a909bc4 JSEvaluateScript + 676
41 JavaScriptCore 0x00007fff3acad582 -[JSContext evaluateScript:withSourceURL:] + 194
42 JavaScriptOSA 0x00007fff54fad146 -[JSStorage(JSProcedures) evaluateScript:inContextID:] + 737
43 JavaScriptOSA 0x00007fff54fb0e27 -[JSStorage(JSProcedures) handleEvent:inContext:modeFlags:resultingResultID:] + 588
44 CoreFoundation 0x00007fff3763c82c invoking
+ 140
45 CoreFoundation 0x00007fff3763c6f7 -[NSInvocation invoke] + 311
46 JavaScript 0x0000000102d257bf JavaScriptComponent + 1135
47 OpenScripting 0x00007fff36c28922 OSAExecuteEvent + 52
48 osascript 0x00000001019295a0 osascript + 9632
49 libdyld.dylib 0x00007fff635833d5 start + 1
)
libc++abi.dylib:

And the other two didn't return anything. No error but no value either.

Is there some reason the PIXEL function is not sufficient for this task?

image

%Hex2%255*PIXEL(Red,MOUSEX(),MOUSEY())%%Hex2%255*PIXEL(Green,MOUSEX(),MOUSEY())%%Hex2%255*PIXEL(Blue,MOUSEX(),MOUSEY())%

I don't know what you would want with the alpha since there is no alpha on a pixel on the screen.

Thanks Peter, I didn't know the PIXEL function but this is just perfect! And so damn simple!

1 Like