Monitor clipboard?
Monitor clipboard?
Is there a way on macOS to monitor the clipboard to check for appropriate types? I store my clipboard data in JSON format with an identifier at the start. If the identifier is valid, my application tries to parse the JSON. I'd like to enable the paste button only when an appropriate type is on the clipboard. Is there a way to do this without continuously calling GetClipboardText()?
Re: Monitor clipboard?
You can check for a specific UTI but "public.json" doesn't seem to be supported by default.
What you could do is something like this
There are no notifications available but if you want to check the pasteboard with a timer like for example two times a second, you can first check changeCount.Every time something changes to the pasteboard, this value is increased.
That way you only have to check in more detail if this value has changed in between.
What you could do is something like this
Code: Select all
; Get general pasteboard
Global Pasteboard = CocoaMessage(0, 0, "NSPasteboard generalPasteboard")
; Create a NSArray with a "public.json" string object
Global TypeJSON = CocoaMessage(0, CocoaMessage(0, 0, "NSArray alloc"), "initWithObject:$", @"public.json")
; Procedure to check if pasteboard contains JSON data
Procedure.i Pasteboard_ContainsJSON()
ProcedureReturn CocoaMessage(0, Pasteboard, "canReadItemWithDataConformingToTypes:", TypeJSON)
EndProcedure
; Procedure to get JSON data from clipboard
Procedure.s Pasteboard_GetJSON()
Protected JSONString.i = CocoaMessage(0, Pasteboard, "stringForType:$", @"public.json")
If JSONString
ProcedureReturn PeekS(CocoaMessage(0, JSONString, "UTF8String"), -1, #PB_UTF8)
Else
ProcedureReturn ""
EndIf
EndProcedure
; Procedure to set JSON data on the clipboard
Procedure Pasteboard_SetJSON(JSON.s)
CocoaMessage(0, Pasteboard, "declareTypes:", TypeJSON, "owner:", #nil)
CocoaMessage(0, Pasteboard, "setString:$", @JSON, "forType:$", @"public.json")
EndProcedure
; Test
Pasteboard_SetJSON("[1, 3, 5, 7, null, 23, 25, 27]")
If Pasteboard_ContainsJSON()
Debug Pasteboard_GetJSON()
EndIf
Code: Select all
CocoaMessage(0, Pasteboard, "changeCount")
That way you only have to check in more detail if this value has changed in between.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Monitor clipboard?
Perfect! Thank you!
Now to see if it's possible on Linux/Qt...haha.
Now to see if it's possible on Linux/Qt...haha.