gpt4 book ai didi

macos - 是否可以使用 Preview.app 在特定页面打开 PDF?

转载 作者:行者123 更新时间:2023-12-03 16:11:27 25 4
gpt4 key购买 nike

我可以愉快地从我的应用程序中使用 Preview.app 打开位于 path 的 PDF 文件

[NSWorkspace.sharedWorkspace openFile: path];

但是,我想在某个页面上使用该文件启动 Preview.app。这可能吗,例如通过在 NSWorkspace

中传递特定的 NSAppleEventDescriptor
- (BOOL)openURLs:(NSArray *)urls withAppBundleIdentifier:(NSString *)bundleIdentifier options:(NSWorkspaceLaunchOptions)options additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor launchIdentifiers:(NSArray **)identifiers

方法? QuickLook 可以做到这一点,我想模仿这种行为。

感谢您的帮助!

最佳答案

您可以通过 NSAppleScript 执行此操作。

下面是一个NSWorkspace类别方法,打开文件并跳转到指定页面:

- (void)openPreviewFile:(NSString*)filePath onPage:(int)pageNumber {
[self openFile:filePath];

NSString *sysEvents = @"System Events";

NSString *source = [NSString stringWithFormat:@"tell application \"%@\" to activate\ntell application \"%@\" to keystroke \"g\" using {command down, option down}\ndelay %f\ntell application \"%@\" to keystroke \"%i\"\ntell application \"%@\" to keystroke return",
@"Preview", sysEvents, 0.5, sysEvents, pageNumber, sysEvents];

NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:source] autorelease];
[script executeAndReturnError:nil];
}

这是它的作用:

  • 在 NSWorkspace 实例上调用 openFile:
  • 打开预览的“转到页面”对话框
  • 等待对话框弹出
  • 模拟应激活页面的按键,然后按回车键

然后您可以像这样调用该方法:

[[NSWorkspace sharedWorkspace] openPreviewFile:@"/YOUR_PDF.pdf"
onPage:3];

免责声明:如果用户为“转到页面...”菜单项定义自定义键盘快捷键,则会中断!

关于macos - 是否可以使用 Preview.app 在特定页面打开 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941104/

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