【问题标题】:Is it possible to open PDFs with Preview.app at a certain page?是否可以在特定页面使用 Preview.app 打开 PDF?
【发布时间】:2013-10-04 01:12:36
【问题描述】:

我可以在我的应用程序中使用 Preview.app 在path 打开一个 PDF 文件

[NSWorkspace.sharedWorkspace openFile: path];

但是,我想在某个页面上使用该文件启动 Preview.app。这可能吗,例如通过在NSWorkspace's 中传递特定的NSAppleEventDescriptor

- (BOOL)openURLs:(NSArray *)urls withAppBundleIdentifier:(NSString *)bundleIdentifier options:(NSWorkspaceLaunchOptions)options additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor launchIdentifiers:(NSArray **)identifiers

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

感谢您的帮助!

【问题讨论】:

  • Preview 没有 AppleScript 字典,因此可能不容易弄清楚 QuickLook 是如何做到的。

标签: macos cocoa


【解决方案1】:

您可以通过 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];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多