核心提示:MacOS开发教程之NSPasteboard解析。复制内容到剪贴板复制文字- (void)pasteString{NSPasteboard *paste = [NSPasteboard general...
MacOS开发教程之NSPasteboard解析。
复制内容到剪贴板
复制文字
- (void)pasteString{ NSPasteboard *paste = [NSPasteboard generalPasteboard]; [paste clearContents]; [paste writeObjects:@[@"123"]]; }复制前,最好清空剪贴板,否则可能复制失败。 注意,并非
[[NSPasteboard generalPasteboard] writeFileContents:@"123"]; 可以同时复制多个字符串,获取时,会换行分格输出所有。
获得剪贴板的内容
获得文字
- 如果你拷贝的是文件里面的文字,剪切板里保存是刚复制的字符串,;
- 如果你拷贝的是一个或多个的文件,文件夹, 这里就是文件或文件夹的名称
- (void)getPasteString{ NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; if ([[pasteboard types] containsObject:NSPasteboardTypeString]) { NSString *str = [pasteboard stringForType:NSPasteboardTypeString]; NSLog(@"粘贴的文字:%@",str); } }
获取不同类型的剪贴板内容
粘贴板类型
NSPasteboardTypeString; NSPasteboardTypePDF; NSPasteboardTypeTIFF; NSPasteboardTypePNG; NSPasteboardTypeRTF; NSPasteboardTypeRTFD; NSPasteboardTypeHTML; NSPasteboardTypeTabularText; NSPasteboardTypeFont; NSPasteboardTypeRuler; NSPasteboardTypeColor; NSPasteboardTypeSound; NSPasteboardTypeMultipleTextSelection; NSPasteboardTypeFindPanelSearchOptions;
获取方法
- (void)getPasteboard{ NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; NSArray *types = [pasteboard types]; NSLog(@"types : %@",types); if ([[pasteboard types] containsObject:NSPasteboardTypeString]) { //如果你拷贝的是文件里面的文字,剪切板里保存是刚复制的字符串,; //如果你拷贝的是一个或多个的文件,文件夹, 这里就是文件或文件夹的名称 NSString *str = [pasteboard stringForType:NSPasteboardTypeString]; NSLog(@"NSPasteboardTypeString : %@",str); } if ([[pasteboard types] containsObject:NSPasteboardTypePDF]) { NSData *pdfData = [pasteboard dataForType:NSPasteboardTypePDF]; if (pdfData) { NSLog(@"NSPasteboardTypePDF:获取成功"); } } if ([[pasteboard types] containsObject:NSPasteboardTypePNG]) { NSData *pdfData = [pasteboard dataForType:NSPasteboardTypePNG]; NSImage *img = [[NSImage alloc]initWithData:pdfData]; if (img) { NSLog(@"NSPasteboardTypePNG:获取成功"); self.imgView.image = img; } } if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { NSArray *filenames = [pasteboard stringForType:NSFilenamesPboardType]; NSLog(@"NSFilenamesPboardType : %@",filenames);//xml 格式 } // 获取到文件路径URL, 亲测有效 NSArray *tempArray = [pasteboard pasteboardItems]; NSLog(@"pasteboardItems:%@",tempArray); for(NSPasteboardItem *tmpItem in tempArray){ NSString *pathString = [tmpItem stringForType:@"public.file-url"]; NSURL *tmpURL = [NSURL URLWithString:pathString]; NSError *error = nil; if (tmpURL) { //跳过隐藏文件,获取到的直接子文件数量(不包含子文件中的子文件) NSArray *children = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:tmpURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:&error]; NSLog(@"children:%d",[children count]); } } // 同样的,不过好像遍历的更加细致, 不知道有什么不同 for(NSPasteboardItem *tmpItem in tempArray) { NSArray *types = [tmpItem types]; for(NSString *type in types){ NSString *pathString = [tmpItem stringForType:type]; // 如果type是一个public.file-url, 那么url就是一个代表你所复制的文件的路径, 虽然格式有点与该文件的实际路径不一样. NSURL *url = [NSURL URLWithString:pathString]; // 假设你拷贝的文件是一个Image, 你可以直接使用这个url来读取这个image NSImage *image = [[NSImage alloc] initWithContentsOfURL:url]; if (image) { self.imgView.image = image; } // 假设你拷贝的是一个text或rtf等NSString支持的文件类型, 你可以直接使用这个url来初始化一个NSString NSString *string = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSLog(@"------- "); // 反正不管如何, 这种方式,得到的url它是能代表剪贴板里的文件的, 所以是可以直接使用的 NSLog(@"type:%@, pathString:%@\n",type,pathString); } } }