罗朝辉(飘飘白云)

关注嵌入式操作系统,移动平台,图形开发。-->加微博 ^_^

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  85 随笔 :: 0 文章 :: 169 评论 :: 0 Trackbacks
NSWorkspace 使用示例
CC 许可,转载请注明出处

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

NSWorkspace 是个 Singleton 类,我们通过 sharedWorkspace 来访问它。比如下面的语句用 TextEdit 打开指定的文件:
[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/README" withApplication:@"TextEdit"];
下面的代码演示了大部分 workspace 的应用,运行效果图如下:


- (IBAction) launchApplication:(id) sender
{
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    
//BOOL wasLaunched = [workspace launchApplication:@"Safari"];
    
    
// launch without activation
    
//
    BOOL wasLaunched = [workspace launchAppWithBundleIdentifier: @"com.apple.Safari"
                                                        options: NSWorkspaceLaunchWithoutActivation
                                 additionalEventParamDescriptor: NULL
                                               launchIdentifier: nil];
    
if ( wasLaunched )
        NSLog (
@"Safari was launched");
    
else
        NSLog (
@"Safari was not launched");
    
    NSArray 
* apps = [workspace valueForKeyPath:@"launchedApplications.NSApplicationName"];
    self.launchedApplications 
= [NSString stringWithFormat:@"Launched Applications:\n%@", apps];
    NSLog(
@"Launched Applications:\n%@", apps);
}

- (IBAction) openPdfByDefault:(id) sender
{
    NSString 
* path    = @"/Developer/About Xcode and iOS SDK.pdf";
    NSURL    
* fileURL = [NSURL fileURLWithPath: path];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    [workspace openURL: fileURL];
}

- (IBAction) openPdfBySafari:(id) sender
{
    NSString 
* path    = @"/Developer/About Xcode and iOS SDK.pdf";
    NSURL    
* fileURL = [NSURL fileURLWithPath: path];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    [workspace openFile:[fileURL path] withApplication:
@"Safari"];
}

- (IBAction) selectFileInFinder:(id) sender
{
    NSString 
* path    = @"/Developer/About Xcode and iOS SDK.pdf";
    NSURL    
* fileURL = [NSURL fileURLWithPath: path];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    [workspace selectFile:[fileURL path] inFileViewerRootedAtPath:nil];
}

- (IBAction) gatherFileInfo:(id) sender
{
    NSString 
* path    = @"/Developer/About Xcode and iOS SDK.pdf";
    NSURL    
* fileURL = [NSURL fileURLWithPath: path];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    
    NSString    
* appName;
    NSString    
* fileType;
    
    [workspace getInfoForFile: [fileURL path]
                  application: 
&appName
                         type: 
&fileType];
    
    BOOL removable 
= NO;
    BOOL writeable 
= NO;
    BOOL unmountable 
= NO;
    NSString 
*description;
    NSString 
*fileSystemType;
    
    [workspace getFileSystemInfoForPath:[fileURL path]
                            isRemovable: 
&removable
                             isWritable: 
&writeable
                          isUnmountable: 
&unmountable
                            description: 
&description
                                   type: 
&fileSystemType];
    
    self.fileInfo 
= [NSString stringWithFormat:
                     
@"AppName: %@\ntype: %@"
                     
@"\nremoveable: %d\nwriteable: %d\nunmountable: %d"
                     
@"\ndescription: %@\nfileSystemType: %@",
                     appName, fileType,
                     removable, writeable, unmountable,
                     description, fileSystemType];
    NSLog (
@" >> gather file info:\n%@", self.fileInfo);
}

- (IBAction) copyFileToDesktop:(id) sender
{
    NSString 
* name  = @"About Xcode and iOS SDK.pdf";
    NSArray  
* files = [NSArray arrayWithObject: name];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    
    [workspace performFileOperation: NSWorkspaceCopyOperation
                             source: 
@"/Developer/"
                        destination: 
@"/Users/tianyouhui/Desktop/"
                              files: files
                                tag: 
0];
}

- (IBAction) moveFileToTrash:(id) sender
{
    NSString 
* name  = @"About Xcode and iOS SDK.pdf";
    NSArray  
* files = [NSArray arrayWithObject: name];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];
    
    [workspace performFileOperation: NSWorkspaceRecycleOperation
                             source: 
@"/Users/tianyouhui/Desktop/"
                        destination: 
@""
                              files: files
                                tag: 
0];
}

- (IBAction) gatherIconOfFile:(id) sender
{
    NSString 
* path    = @"/Developer/About Xcode and iOS SDK.pdf";
    NSURL    
* fileURL = [NSURL fileURLWithPath: path];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];

    self.icon 
= [workspace iconForFile: [fileURL path]];
    
//NSString    * path  = [workspace fullPathForApplication:@"Safari"];
    
//self.xcodeIcon  = [workspace iconForFile: path];

    self.xcodeIcon 
= [workspace iconForFileType:@"xcodeproj"];
}

- (IBAction) openUrlBySafari:(id) sender
{
    NSURL 
* url = [NSURL URLWithString:@"http://www.cppblog.com/kesalin/"];
    
    NSWorkspace 
* workspace = [NSWorkspace sharedWorkspace];

    [workspace openURL: url];
}
posted on 2011-09-05 16:04 罗朝辉 阅读(3159) 评论(0)  编辑 收藏 引用 所属分类: 移动开发Cocoa 开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理