如何在 Lion 上使用 NSTask 在预览中打开 pdf/jpg?
我有以下代码在雪豹上运行良好。它使用首选应用程序打开存储在临时目录中的文件。
CFURLRef prefAppUrl = nil;
LSGetApplicationForURL((CFURLRef) tempUrl, kLSRolesAll, NULL, &prefAppUrl);
NSBundle *bundle = [NSBundle bundleWithURL:(NSURL*)prefAppUrl];
NSString *path = [bundle executablePath];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:path];
[task setArguments:[NSArray arrayWithObject:[tempUrl path]]];
[task launch];
但它在 Lion 上根本不起作用。它会导致以下错误并预览(以及文本编辑)显示错误对话框。其他编辑器效果很好(例如 TextMate)。
011-10-11 16:29:36.867 Preview[11340:60b] PVPDFPageContainer initWithURL:file://localhost/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf failed, error = Error Domain=NSCocoaErrorDomain Code=257 "The file “1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo=0x100363fe0 {NSFilePath=/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf, NSUnderlyingError=0x100363f80 "The operation couldn’t be completed. Operation not permitted"}
11.10.2011 16:30
2011-10-11 16:28:37.517 Preview[11304:60b] PVPDFPageContainer initWithURL:file://localhost/private/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf failed, error = Error Domain=NSCocoaErrorDomain Code=257 "The file “c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo=0x7fec32a680e0 {NSFilePath=/private/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf, NSUnderlyingError=0x7fec32a68080 "The operation couldn’t be completed. Operation not permitted"}
我发现了许多关于如何解决 Preview 和 TextEdit 的 Lion 问题的提示,描述了如何更改 ~/Library/Containers 的权限,但这并没有解决从 Cocoa 打开文档的问题。
使用 finder 时,Preview 和 TextEdit 可以毫无问题地打开同一个文件,并且在发出命令 open -a "/Applications/Preview.app/Contents/MacOS/Preview" pathToFile 时也可以使用 NSTask 打开同一个文件,但是这对我来说还不够好,因为我需要找出应用程序何时关闭以及打开立即结束。
有人可以指出我正确的方向吗?我已经在这个问题上花了两天了。非常感谢!
I have following code working well on Snow Leopard. It opens a file stored in temporary directory with preferred application.
CFURLRef prefAppUrl = nil;
LSGetApplicationForURL((CFURLRef) tempUrl, kLSRolesAll, NULL, &prefAppUrl);
NSBundle *bundle = [NSBundle bundleWithURL:(NSURL*)prefAppUrl];
NSString *path = [bundle executablePath];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:path];
[task setArguments:[NSArray arrayWithObject:[tempUrl path]]];
[task launch];
But it does not work at all on Lion. It leads to following error and Preview (and also TextEdit) show error dialog. Other editors work well (for example TextMate).
011-10-11 16:29:36.867 Preview[11340:60b] PVPDFPageContainer initWithURL:file://localhost/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf failed, error = Error Domain=NSCocoaErrorDomain Code=257 "The file “1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo=0x100363fe0 {NSFilePath=/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/1dc07081-837d-4ecf-b4c3-9aefe92c3a16.pdf, NSUnderlyingError=0x100363f80 "The operation couldn’t be completed. Operation not permitted"}
11.10.2011 16:30
2011-10-11 16:28:37.517 Preview[11304:60b] PVPDFPageContainer initWithURL:file://localhost/private/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf failed, error = Error Domain=NSCocoaErrorDomain Code=257 "The file “c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo=0x7fec32a680e0 {NSFilePath=/private/var/folders/vt/pjtj7vg54x950234v__4363m0000gn/T/c0df6d94-49c9-4ce2-a362-8ec0e04d5104.pdf, NSUnderlyingError=0x7fec32a68080 "The operation couldn’t be completed. Operation not permitted"}
I have found many hints how to fix Lion problems with Preview and TextEdit describing how to change permissions for ~/Library/Containers, but that did not solve the problem of opening documents from Cocoa.
Preview and TextEdit can open the same file without any problem when using finder and also by using NSTask when command open -a "/Applications/Preview.app/Contents/MacOS/Preview" pathToFile is issued, but that is not good enough for me, because I need to find out when the application is closed and open ends immediately.
Can anybody point me to correct direction, please? I have spent on this issues 2 days already. Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地使用:
效果很好。
You could simply use:
That works fine.