“打开最近的菜单”仅适用于XCode

时间:2016-12-23 17:01:47

标签: xcode nsurl

我正在非NSDocument应用中实现Open Recent Menu。

在通过NSOpenPanel获取文件的url后,文件的完整路径,app程序打开文件,并成功地将应用程序与应用程序相关联:

[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:self.URLFile];

这会将文件添加到“打开最近的菜单”,此时一切正常。

当我尝试通过“打开最近的菜单”打开文件时,应用程序cals:

  • (BOOL)应用程序:(NSApplication *)theApplication openFile:(NSString *)filename

如果文件是成功打开则返回YES,此时一切正常工作正常....但只有在应用程序运行到XCode时才有效,如果存档应用程序导出Finder或Terminal.app中的启动以查看日志,不行。

在终端应用程序中使用XCode内部的应用程序后,问题是当我尝试从NSString转换为NSURL时。这是代码:

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename  
{  
    // paths in NSDocumentController recentDocumentURLs array  
    NSLog(@"Paths in recentDocumentURLs");  
    for (NSURL *url in [NSDocumentController sharedDocumentController].recentDocumentURLs) {  
        NSLog(@"Path: %@",[url absoluteString]);  
    }  

    NSLog(@"Path filename: %@", filename);  

    NSString *temp = [filename stringByExpandingTildeInPath];  
    NSLog(@"Path temp: %@", temp);  

    self.archivoURL = [NSURL fileURLWithPath:filename isDirectory:NO];  
    NSLog(@"Path self.archivoURL: %@", self.archivoURL);  

    if (self.archivoURL == nil) return NO;  

    // open file...  

    return YES;  
} // Fin de application:OpenFile  

XCode日志中的这段代码:

Paths in recentDocumentURLs
2016-12-21 00:08:23.118577 AppMake[9424:253018] Path: file:///Users/Manuel/Documents/Modelos%203D/X_single_Ring_round_40.stl
2016-12-21 00:08:23.118612 AppMake[9424:253018] Path: file:///Users/Manuel/Documents/Modelos%203D/pirinola.stl
2016-12-21 00:08:23.118625 AppMake[9424:253018] Path: file:///Users/Manuel/Documents/Modelos%203D/cubo%203.stl
2016-12-21 00:08:23.118636 AppMake[9424:253018] Path filename: /Users/Manuel/Documents/Modelos 3D/cubo 3.stl
2016-12-21 00:08:23.118665 AppMake[9424:253018] Path temp: /Users/Manuel/Documents/Modelos%203D/cubo%203.stl
2016-12-21 00:08:23.118706 AppMake[9424:253018] Path self.archivoURL: file:///Users/Manuel/Documents/Modelos%203D/cubo%203.stl

此代码在XCode之外并通过Terminal.app启动应用程序以查看日志,日志:

2016-12-21 00:08:05.564 AppMake[9410:252746] Paths in recentDocumentURLs
2016-12-21 00:08:05.566 AppMake[9410:252746] Path: file:///Users/Manuel/Documents/Modelos%203D/X_single_Ring_round_40.stl
2016-12-21 00:08:05.566 AppMake[9410:252746] Path: file:///Users/Manuel/Documents/Modelos%203D/pirinola.stl
2016-12-21 00:08:05.566 AppMake[9410:252746] Path: file:///Users/Manuel/Documents/Modelos%203D/cubo%203.stl
2016-12-21 00:08:05.566 AppMake[9410:252746] Path filename: /Users/Manuel/Documents/Modelos 3D/cubo 3.stl
2016-12-21 00:08:05.566 AppMake[9410:252746] Path temp: /Users/Manuel/Documents/Modelos%203D/cubo%203.stl
2016-12-21 00:08:05.566 AppMake[9410:252746] Path self.archivoURL: (null)

任何想法?

感谢Manuel

0 个答案:

没有答案