在mac服务包中接收NSMutableAttributedString

时间:2016-11-23 06:08:11

标签: objective-c macos nspasteboard

我正在创建一个mac .service包,它从应用程序接收富文本。 TextEdit和notes似乎没有问题将富文本写入常规粘贴板,但是当在服务粘贴板上传递相同的数据时,字符串上没有属性。这是这些程序中服务实现的问题还是有办法解决这个问题?以下是我用来访问粘贴板的代码:

- (void)strikeService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error{

// Test for strings on the pasteboard.
NSMutableAttributedString *pboardString;
NSMutableAttributedString *output;
NSDictionary *options = [NSDictionary dictionary];
NSArray *classes;

classes = [[NSArray alloc] initWithObjects:[NSAttributedString class], nil];
NSArray *attributedStrings = [pboard readObjectsForClasses:classes options:options];
if (attributedStrings != nil) {
    pboardString = [[NSMutableAttributedString alloc] initWithAttributedString:[attributedStrings objectAtIndex:0]];
}


classes = [[NSArray alloc] initWithObjects:[NSString class], nil];
NSArray *strings = [pboard readObjectsForClasses:classes options:options];
if (strings != nil && attributedStrings == nil) {
    pboardString = [[NSMutableAttributedString alloc] initWithString:[strings objectAtIndex:0]];
}
output = ...// do something with input

// Write the formatted string onto the pasteboard.
[pboard clearContents];
[pboard writeObjects:[NSArray arrayWithObject:output]];

}

基本上我的问题是pboardString没有属性,除非我从textedit和notes的常规粘贴板中获取。为什么这样,我该如何解决?

更新:从调用[pboard types]看来,当从服务调用时,返回到粘贴板的唯一UTI是Notes和TextEdit中的NSStringPboardType和public.utf8-plain-text但是当调用常规剪贴板时,然后这些程序发布一个public.rtf数据类型。如此有效,服务无法访问属性,但可以将属性字符串发布回应用程序。有没有办法找到突出显示文本的字符串属性?

0 个答案:

没有答案