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://blog.csdn.net/kesalin/"]; NSWorkspace * workspace = [NSWorkspace shareDWorkspace]; [workspace openURL: url];}总结
以上是内存溢出为你收集整理的[Cocoa] NSWorkspace 使用示例全部内容,希望文章能够帮你解决[Cocoa] NSWorkspace 使用示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)