gpt4 book ai didi

cocoa - 从发送者应用程序获取 NSServices 信息时获取文本的位置(坐标)

转载 作者:行者123 更新时间:2023-12-03 17:12:18 28 4
gpt4 key购买 nike

这段代码工作正常,可以通过实现 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]];
}

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

最佳答案

您可以做的是,当用户通过双击文本选择文本时捕获并存储鼠标位置。当您的应用程序在用户从上下文菜单中选择菜单后获得控制权时,使用存储的鼠标位置来显示弹出窗口。

- (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
}

关于cocoa - 从发送者应用程序获取 NSServices 信息时获取文本的位置(坐标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865268/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com