better cleanup
MODIFY
TextCopy/OsxClipboard.cs
TextCopy/OsxClipboard.cs
@@ -3,46 +3,35 @@ using System.Runtime.InteropServices;
static class OsxClipboard
{
- public static string GetText()
- {
- var nsString = objc_getClass("NSString");
- IntPtr dataType = default;
- try
+ static IntPtr nsString = objc_getClass("NSString");
+ static IntPtr nsPasteboard = objc_getClass("NSPasteboard");
+ static IntPtr nsStringPboardType;
+ static IntPtr utfTextType;
+ static IntPtr generalPasteboard;
+
+ static OsxClipboard()
{
- dataType = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), "NSStringPboardType");
+ utfTextType = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), "public.utf8-plain-text");
+ nsStringPboardType = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), "NSStringPboardType");
- var nsPasteboard = objc_getClass("NSPasteboard");
- var generalPasteboard = objc_msgSend(nsPasteboard, sel_registerName("generalPasteboard"));
+ generalPasteboard = objc_msgSend(nsPasteboard, sel_registerName("generalPasteboard"));
+ }
- var ptr = objc_msgSend(generalPasteboard, sel_registerName("stringForType:"), dataType);
+ public static string GetText()
+ {
+ var ptr = objc_msgSend(generalPasteboard, sel_registerName("stringForType:"), nsStringPboardType);
var charArray = objc_msgSend(ptr, sel_registerName("UTF8String"));
-
return Marshal.PtrToStringAnsi(charArray);
}
- finally
- {
- if (dataType != default)
- {
- objc_msgSend(dataType, sel_registerName("release"));
- }
- }
- }
public static void SetText(string text)
{
- var nsString = objc_getClass("NSString");
IntPtr str = default;
- IntPtr dataType = default;
try
{
str = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), text);
- dataType = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), NSPasteboardTypeString);
-
- var nsPasteboard = objc_getClass("NSPasteboard");
- var generalPasteboard = objc_msgSend(nsPasteboard, sel_registerName("generalPasteboard"));
-
objc_msgSend(generalPasteboard, sel_registerName("clearContents"));
- objc_msgSend(generalPasteboard, sel_registerName("setString:forType:"), str, dataType);
+ objc_msgSend(generalPasteboard, sel_registerName("setString:forType:"), str, utfTextType);
}
finally
{
@@ -50,11 +39,6 @@ static class OsxClipboard
{
objc_msgSend(str, sel_registerName("release"));
}
-
- if (dataType != default)
- {
- objc_msgSend(dataType, sel_registerName("release"));
- }
}
}
@@ -69,11 +53,10 @@ static class OsxClipboard
[DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector, IntPtr arg1);
+
[DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);
[DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
static extern IntPtr sel_registerName(string selectorName);
-
- const string NSPasteboardTypeString = "public.utf8-plain-text";
}
\ No newline at end of file

better cleanup