Mac开发 NSWorkspace的综合应用


NSWorkspace 的综合应用

/**
* 启动APP
*/
- (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];  
}  
  
/**
* 使用safari打开pdf文件
*/
- (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:@""];  
}  
  
/**
* 获取文件信息
*/
- (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"];  
}  
  
/**
* 使用Safari打开url
*/
- (IBAction) openUrlBySafari:(id) sender  
{  
    NSURL * url = [NSURL URLWithString:@"http://blog.csdn.net/kesalin/"];  
      
    NSWorkspace * workspace = [NSWorkspace sharedWorkspace];  
  
    [workspace openURL: url];  
}  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值