我正在嘗試構建一個獨立的系統服務(應用程序擴展名爲.service,保存到〜/ Library/Services /)以替換用戶在Mac OS X中選擇的文本。使用Xcode替換文本服務 - 不替換選定的文本
我想用Xcode和而不是 Automator,因爲我比Applescript更習慣於Objective-C。
我在網上找到了幾個例子,例如: this和Apple's documentation。我得到了適當配置的Xcode項目,並且沒有任何問題。但是,當我安裝我的服務並嘗試使用它時,沒有任何反應。
服務方法本身被執行:我放置代碼在其方法體內顯示一個NSAlert並顯示。但是,所選文字不會被替換。
任何想法可能會丟失什麼?這是實現服務的方法:
- (void) fixPath:(NSPasteboard*) pboard
userData:(NSString*) userData
error:(NSString**) error
{
// Make sure the pasteboard contains a string.
if (![pboard canReadObjectForClasses:@[[NSString class]] options:@{}])
{
*error = NSLocalizedString(@"Error: the pasteboard doesn't contain a string.", nil);
return;
}
NSString* pasteboardString = [pboard stringForType:NSPasteboardTypeString];
//NSAlert* alert = [[NSAlert alloc] init];
//[alert setMessageText:@"WORKING!"];
//[alert runModal];
//^This alert is displayed when selecting the service in the context menu
pasteboardString = @"NEW TEXT";
NSArray* types = [NSArray arrayWithObject:NSStringPboardType];
[pboard clearContents];
[pboard declareTypes:types owner:nil];
// Set new text:
[pboard writeObjects:[NSArray arrayWithObject:pasteboardString]];
// Alternatively:
[pboard setString:pasteboardString forType:NSStringPboardType];
// (neither works)
return;
}
任何人......? Bueller? – 2014-12-04 07:38:16