http://blog.csdn.net/kesalin/article/details/6749107
Nsworkspace provides the following services for applications:
1) Open, manipulate files/devices, get File/device information
2) Tracking of files, equipment and database changes
3) Set or get Finder information for the file
4) Start the application.
Nsworkspace is a Singleton class, and we access it through SharedWorkspace. For example, the following statement opens the specified file with TextEdit:
[[Nsworkspace SharedWorkspace] openfile:@ "/myfiles/readme" withapplication:@ "TextEdit"];
The following code demonstrates most of the workspace applications and runs as follows:
Here's the code, the code is simple:
[CPP]View Plaincopyprint?
- -(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 is 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 Use Example