[Cocoa] NSWorkspace 使用示例

 

http://blog.csdn.net/kesalin/article/details/6749107

 

NSWorkspace 为应用程序提供如下服务:
1)打开,操作文件/设备,获取文件/设备信息
2)跟踪文件,设备以及数据库的变动
3)设置或获取文件的 Finder 信息
4)启动应用程序。

NSWorkspace 是个 Singleton 类,我们通过 sharedWorkspace 来访问它。比如下面的语句用 TextEdit 打开指定的文件:
[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/README" withApplication:@"TextEdit"];

下面的代码演示了大部分 workspace 的应用,运行效果图如下:

[Cocoa] NSWorkspace 使用示例

 

下面来看代码,代码都很简单的:

 

    1. - (IBAction) launchApplication:(id) sender  
    2. {  
    3.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    4.     //BOOL wasLaunched = [workspace launchApplication:@"Safari"];  
    5.       
    6.     // launch without activation  
    7.     //  
    8.     BOOL wasLaunched = [workspace launchAppWithBundleIdentifier: @"com.apple.Safari"  
    9.                                                         options: NSWorkspaceLaunchWithoutActivation  
    10.                                  additionalEventParamDescriptor: NULL  
    11.                                                launchIdentifier: nil];  
    12.     if ( wasLaunched )  
    13.         NSLog (@"Safari was launched");  
    14.     else  
    15.         NSLog (@"Safari was not launched");  
    16.       
    17.     NSArray * apps = [workspace valueForKeyPath:@"launchedApplications.NSApplicationName"];  
    18.     self.launchedApplications = [NSString stringWithFormat:@"Launched Applications:\n%@", apps];  
    19.     NSLog(@"Launched Applications:\n%@", apps);  
    20. }  
    21.   
    22. - (IBAction) openPdfByDefault:(id) sender  
    23. {  
    24.     NSString * path    = @"/Developer/About Xcode and iOS SDK.pdf";  
    25.     NSURL    * fileURL = [NSURL fileURLWithPath: path];  
    26.       
    27.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    28.     [workspace openURL: fileURL];  
    29. }  
    30.   
    31. - (IBAction) openPdfBySafari:(id) sender  
    32. {  
    33.     NSString * path    = @"/Developer/About Xcode and iOS SDK.pdf";  
    34.     NSURL    * fileURL = [NSURL fileURLWithPath: path];  
    35.       
    36.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    37.     [workspace openFile:[fileURL path] withApplication:@"Safari"];  
    38. }  
    39.   
    40. - (IBAction) selectFileInFinder:(id) sender  
    41. {  
    42.     NSString * path    = @"/Developer/About Xcode and iOS SDK.pdf";  
    43.     NSURL    * fileURL = [NSURL fileURLWithPath: path];  
    44.       
    45.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    46.     [workspace selectFile:[fileURL path] inFileViewerRootedAtPath:nil];  
    47. }  
    48.   
    49. - (IBAction) gatherFileInfo:(id) sender  
    50. {  
    51.     NSString * path    = @"/Developer/About Xcode and iOS SDK.pdf";  
    52.     NSURL    * fileURL = [NSURL fileURLWithPath: path];  
    53.       
    54.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    55.       
    56.     NSString    * appName;  
    57.     NSString    * fileType;  
    58.       
    59.     [workspace getInfoForFile: [fileURL path]  
    60.                   application: &appName  
    61.                          type: &fileType];  
    62.       
    63.     BOOL removable = NO;  
    64.     BOOL writeable = NO;  
    65.     BOOL unmountable = NO;  
    66.     NSString *description;  
    67.     NSString *fileSystemType;  
    68.       
    69.     [workspace getFileSystemInfoForPath:[fileURL path]  
    70.                             isRemovable: &removable  
    71.                              isWritable: &writeable  
    72.                           isUnmountable: &unmountable  
    73.                             description: &description  
    74.                                    type: &fileSystemType];  
    75.       
    76.     self.fileInfo = [NSString stringWithFormat:  
    77.                      @"AppName: %@\ntype: %@"  
    78.                      @"\nremoveable: %d\nwriteable: %d\nunmountable: %d"  
    79.                      @"\ndescription: %@\nfileSystemType: %@",  
    80.                      appName, fileType,  
    81.                      removable, writeable, unmountable,  
    82.                      description, fileSystemType];  
    83.     NSLog (@" >> gather file info:\n%@", self.fileInfo);  
    84. }  
    85.   
    86. - (IBAction) copyFileToDesktop:(id) sender  
    87. {  
    88.     NSString * name  = @"About Xcode and iOS SDK.pdf";  
    89.     NSArray  * files = [NSArray arrayWithObject: name];  
    90.       
    91.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    92.       
    93.     [workspace performFileOperation: NSWorkspaceCopyOperation  
    94.                              source: @"/Developer/"  
    95.                         destination: @"/Users/tianyouhui/Desktop/"  
    96.                               files: files  
    97.                                 tag: 0];  
    98. }  
    99.   
    100. - (IBAction) moveFileToTrash:(id) sender  
    101. {  
    102.     NSString * name  = @"About Xcode and iOS SDK.pdf";  
    103.     NSArray  * files = [NSArray arrayWithObject: name];  
    104.       
    105.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    106.       
    107.     [workspace performFileOperation: NSWorkspaceRecycleOperation  
    108.                              source: @"/Users/tianyouhui/Desktop/"  
    109.                         destination: @""  
    110.                               files: files  
    111.                                 tag: 0];  
    112. }  
    113.   
    114. - (IBAction) gatherIconOfFile:(id) sender  
    115. {  
    116.     NSString * path    = @"/Developer/About Xcode and iOS SDK.pdf";  
    117.     NSURL    * fileURL = [NSURL fileURLWithPath: path];  
    118.       
    119.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    120.   
    121.     self.icon = [workspace iconForFile: [fileURL path]];  
    122.     //NSString    * path  = [workspace fullPathForApplication:@"Safari"];  
    123.     //self.xcodeIcon  = [workspace iconForFile: path];  
    124.   
    125.     self.xcodeIcon = [workspace iconForFileType:@"xcodeproj"];  
    126. }  
    127.   
    128. - (IBAction) openUrlBySafari:(id) sender  
    129. {  
    130.     NSURL * url = [NSURL URLWithString:@"http://blog.csdn.net/kesalin/"];  
    131.       
    132.     NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
    133.   
    134.     [workspace openURL: url];  
    135. }  

你可能感兴趣的:(workspace)