1

这是代码工作正常,以便通过实现 NSServices 从另一个任何其他应用程序接收和返回一个 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]];
}

有没有办法获取发件人应用程序具有所选文本的位置、位置和/或坐标,以便在该位置准确显示弹出窗口?

4

1 回答 1

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
 }
于 2013-11-20T10:41:03.163 回答