gpt4 book ai didi

cocoa - 如何在 Lion 上使用 NSTask 在预览中打开 pdf/jpg?

转载 作者:行者123 更新时间:2023-12-03 16:43:53 24 4
gpt4 key购买 nike

我有以下代码在雪豹上运行良好。它使用首选应用程序打开存储在临时目录中的文件。

        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 为已发布,但这对我来说还不够好,因为我需要找出应用程序何时关闭以及打开立即结束。

有人能给我指出正确的方向吗?我已经在这个问题上花了两天了。非常感谢!

最佳答案

您可以简单地使用:

[[NSWorkspace sharedWorkspace] openURL: 'URL to PDF'];

效果很好。

关于cocoa - 如何在 Lion 上使用 NSTask 在预览中打开 pdf/jpg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7755035/

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