I can happily open a PDF file at path
with Preview.app from within my application using
[NSWorkspace.sharedWorkspace openFile: path];
However, I would like to launch Preview.app with that file at a certain page. Is this possible, e.g. by passing a specific NSAppleEventDescriptor
in NSWorkspace
's
- (BOOL)openURLs:(NSArray *)urls withAppBundleIdentifier:(NSString *)bundleIdentifier options:(NSWorkspaceLaunchOptions)options additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor launchIdentifiers:(NSArray **)identifiers
method? QuickLook can do this and I would like to imitate this behavior.
Thanks for any help!
You can do this via NSAppleScript.
Here's an NSWorkspace category method that opens the file and jumps to the specified page:
- (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];
}
Here's what this does:
You can then call the method like so:
[[NSWorkspace sharedWorkspace] openPreviewFile:@"/YOUR_PDF.pdf"
onPage:3];
Disclaimer: This breaks if the user defines a custom keyboard shortcut for the "Go to Page..." menu item!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With