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!