[Cocoa] Nsworkspace Use Example

Source: Internet
Author: User

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?
    1. -(Ibaction) Launchapplication: (ID) sender
    2. {
    3. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    4. //bool waslaunched = [Workspace launchapplication:@ "Safari"];
    5. //Launch without activation
    6. //  
    7. BOOL waslaunched = [Workspace launchappwithbundleidentifier: @"Com.apple.Safari"
    8. Options:nsworkspacelaunchwithoutactivation
    9. Additionaleventparamdescriptor:null
    10. Launchidentifier:nil];
    11. if (waslaunched)
    12. NSLog (@"Safari was launched");
    13. Else
    14. NSLog (@"Safari is not launched");
    15. Nsarray * apps = [workspace valueforkeypath:@"Launchedapplications.nsapplicationname"];
    16. Self.launchedapplications = [NSString stringwithformat:@"launched applications:\n%@", apps];
    17. NSLog (@"launched applications:\n%@", apps);
    18. }
    19. -(Ibaction) Openpdfbydefault: (ID) sender
    20. {
    21. NSString * Path = @"/developer/about Xcode and IOS sdk.pdf";
    22. Nsurl * FileURL = [Nsurl Fileurlwithpath:path];
    23. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    24. [Workspace Openurl:fileurl];
    25. }
    26. -(Ibaction) Openpdfbysafari: (ID) sender
    27. {
    28. NSString * Path = @"/developer/about Xcode and IOS sdk.pdf";
    29. Nsurl * FileURL = [Nsurl Fileurlwithpath:path];
    30. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    31. [Workspace openfile:[fileurl Path] withapplication:@"Safari";
    32. }
    33. -(Ibaction) Selectfileinfinder: (ID) sender
    34. {
    35. NSString * Path = @"/developer/about Xcode and IOS sdk.pdf";
    36. Nsurl * FileURL = [Nsurl Fileurlwithpath:path];
    37. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    38. [Workspace Selectfile:[fileurl Path] infileviewerrootedatpath:nil];
    39. }
    40. -(Ibaction) Gatherfileinfo: (ID) sender
    41. {
    42. NSString * Path = @"/developer/about Xcode and IOS sdk.pdf";
    43. Nsurl * FileURL = [Nsurl Fileurlwithpath:path];
    44. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    45. NSString * APPNAME;
    46. NSString * FILETYPE;
    47. [Workspace getinfoforfile: [FileURL Path]
    48. Application: &appname
    49. Type: &fileType];
    50. BOOL removable = NO;
    51. BOOL writeable = NO;
    52. BOOL unmountable = NO;
    53. NSString *description;
    54. NSString *filesystemtype;
    55. [Workspace Getfilesysteminfoforpath:[fileurl Path]
    56. IsRemovable: &removable
    57. IsWritable: &writeable
    58. Isunmountable: &unmountable
    59. Description: &description
    60. Type: &fileSystemType];
    61. Self.fileinfo = [NSString stringWithFormat:
    62. @"AppName:%@\ntype:%@"
    63. @"\nremoveable:%d\nwriteable:%d\nunmountable:%d"
    64. @"\ndescription:%@\nfilesystemtype:%@",
    65. AppName, FileType,
    66. Removable, writeable, unmountable,
    67. Description, Filesystemtype];
    68. NSLog (@">> gather file info:\n%@", self.fileinfo);
    69. }
    70. -(Ibaction) Copyfiletodesktop: (ID) sender
    71. {
    72. NSString * name = @"About Xcode and IOS sdk.pdf";
    73. Nsarray * files = [Nsarray arraywithobject:name];
    74. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    75. [Workspace performfileoperation:nsworkspacecopyoperation
    76. Source: @"/developer/"
    77. Destination: @"/users/tianyouhui/desktop/"
    78. Files:files
    79. TAG:0];
    80. }
    81. -(Ibaction) Movefiletotrash: (ID) sender
    82. {
    83. NSString * name = @"About Xcode and IOS sdk.pdf";
    84. Nsarray * files = [Nsarray arraywithobject:name];
    85. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    86. [Workspace performfileoperation:nsworkspacerecycleoperation
    87. Source: @"/users/tianyouhui/desktop/"
    88. Destination: @""
    89. Files:files
    90. TAG:0];
    91. }
    92. -(Ibaction) Gathericonoffile: (ID) sender
    93. {
    94. NSString * Path = @"/developer/about Xcode and IOS sdk.pdf";
    95. Nsurl * FileURL = [Nsurl Fileurlwithpath:path];
    96. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    97. Self.icon = [Workspace iconforfile: [FileURL Path]];
    98. //nsstring * Path = [Workspace fullpathforapplication:@ "Safari"];
    99. //self.xcodeicon = [Workspace Iconforfile:path];
    100. Self.xcodeicon = [Workspace iconforfiletype:@"Xcodeproj"];
    101. }
    102. -(Ibaction) Openurlbysafari: (ID) sender
    103. {
    104. Nsurl * url = [nsurl urlwithstring:@"http://blog.csdn.net/kesalin/"];
    105. Nsworkspace * Workspace = [Nsworkspace SharedWorkspace];
    106. [Workspace Openurl:url];
    107. }

[Cocoa] Nsworkspace Use Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.