Sharing the current clipboard content

Sharing the current clipboard content

Screenshot:

Here is a script to share the current clipboard content (Files, Images, Strings). You can use it as AppleScript Action in your presets (Touchbar-Button, Shortcuts etc).

Have Fun!
Dirk

Note: It dosn't work as background script! You must use "Run Apple Script (enter directly as text)"!

script theScript
	use framework "Foundation"
	on shareClipboard()
		set theClipboard to current application's NSPasteboard's generalPasteboard()
		set theURLs to theClipboard's readObjectsForClasses:{current application's class "NSURL"} options:(missing value)
		if theURLs's |count|() > 0 then
			my performSelectorOnMainThread:"showSharingPicker:" withObject:theURLs waitUntilDone:false
		else
			set theImages to theClipboard's readObjectsForClasses:{current application's class "NSImage"} options:(missing value)
			if theImages's |count|() > 0 then
				my performSelectorOnMainThread:"showSharingPicker:" withObject:theImages waitUntilDone:false
			else
				set theAttributedStrings to theClipboard's readObjectsForClasses:{current application's class "NSAttributedString"} options:(missing value)
				if theAttributedStrings's |count|() > 0 then
					my performSelectorOnMainThread:"showSharingPicker:" withObject:theAttributedStrings waitUntilDone:false
				else
					set theStrings to theClipboard's readObjectsForClasses:{current application's class "NSString"} options:(missing value)
					if theStrings's |count|() > 0 then
						my performSelectorOnMainThread:"showSharingPicker:" withObject:theStrings waitUntilDone:false
					end if
				end if
			end if
		end if
	end shareClipboard
	
	on showSharingPicker:shareItems
		set currWindow to current application's NSWindow's alloc()'s initWithContentRect:(current application's NSMakeRect(0, 0, 200, 200)) ¬
			styleMask:0 backing:(current application's NSBackingStoreBuffered) defer:false
		set currView to current application's NSView's alloc's initWithFrame:{{0, 0}, {200, 200}}
		set currRect to currView's |bounds|
		currWindow's contentView's addSubview:currView
		currWindow's |center|()
		
		set sharingPicker to current application's NSSharingServicePicker's alloc()'s initWithItems:shareItems
		sharingPicker's showRelativeToRect:currRect ofView:currView preferredEdge:(current application's NSMinYEdge)
	end showSharingPicker:
end script

theScript's shareClipboard()

(Tested on Big Sur)