这是通过实现NSServices来接收NSString并从另一个任何其他应用程序返回NSString的代码正常工作,如下所述:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/SysServices/Articles/properties.html
- (void)simpleEncrypt:(NSPasteboard *)pboard
userData:(NSString *)userData error:(NSString **)error {
// Test for strings on the pasteboard.
NSArray *classes = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
if (![pboard canReadObjectForClasses:classes options:options]) {
*error = NSLocalizedString(@"Error: couldn't encrypt text.",
@"pboard couldn't give string.");
return;
}
// Get and encrypt the string.
NSString *pboardString = [pboard stringForType:NSPasteboardTypeString];
NSString *newString = [self rotateLettersInString:pboardString];
if (!newString) {
*error = NSLocalizedString(@"Error: couldn't encrypt text.",
@"self couldn't rotate letters.");
return;
}
// Write the encrypted string onto the pasteboard.
[pboard clearContents];
[pboard writeObjects:[NSArray arrayWithObject:newString]];
}
有没有办法获取发件人应用程序所选文本的位置,位置和/或坐标,以便在该位置显示弹出窗口?
答案 0 :(得分:1)
当用户通过双击文本选择文本时,您可以捕获并存储鼠标位置。 如果您的应用程序在用户从上下文菜单中选择菜单后获得控制权,请使用存储的鼠标位置显示弹出窗口。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseUpMask handler:
^(NSEvent *event)
{
if([event clickCount] == 2)
{
NSPoint mousePointInScreen = [NSEvent mouseLocation];//store this to a member ivar.
}
}];
[NSApp setServicesProvider:self];
NSUpdateDynamicServices();
}
- (void) simpleEncrypt:(NSPasteboard *)pboard
userData:(NSString *)userData
error:(NSString **)error
{
//Get the stored mouse location
//Perform other tasks
}