• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            羅朝輝(飄飄白云)

            關(guān)注嵌入式操作系統(tǒng),移動平臺,圖形開發(fā)。-->加微博 ^_^

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              85 隨筆 :: 0 文章 :: 169 評論 :: 0 Trackbacks
            NSWorkspace 使用示例
            CC 許可,轉(zhuǎn)載請注明出處

            NSWorkspace 為應(yīng)用程序提供如下服務(wù):
            1)打開,操作文件/設(shè)備,獲取文件/設(shè)備信息
            2)跟蹤文件,設(shè)備以及數(shù)據(jù)庫的變動
            3)設(shè)置或獲取文件的 Finder 信息
            4)啟動應(yīng)用程序。

            NSWorkspace 是個 Singleton 類,我們通過 sharedWorkspace 來訪問它。比如下面的語句用 TextEdit 打開指定的文件:
            [[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/README" withApplication:@"TextEdit"];
            下面的代碼演示了大部分 workspace 的應(yīng)用,運行效果圖如下:


            - (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://m.shnenglu.com/kesalin/"];
                
                NSWorkspace 
            * workspace = [NSWorkspace sharedWorkspace];

                [workspace openURL: url];
            }
            posted on 2011-09-05 16:04 羅朝輝 閱讀(3194) 評論(0)  編輯 收藏 引用 所屬分類: 移動開發(fā)Cocoa 開發(fā)
            狠狠色丁香久久婷婷综合五月 | 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久国产免费观看精品3| 综合人妻久久一区二区精品| 久久亚洲中文字幕精品一区| 日韩十八禁一区二区久久| 久久国产影院| 久久精品国产免费观看| 精品久久久久久亚洲| 国产精品成人99久久久久| 久久se这里只有精品| 色欲综合久久躁天天躁蜜桃| 久久久WWW成人| 久久ww精品w免费人成| 亚洲国产日韩欧美综合久久| 午夜不卡久久精品无码免费| 国内精品久久久久久不卡影院| 国内精品久久久久影院老司| 国产精品视频久久久| 伊人久久精品影院| 欧美亚洲另类久久综合| 伊人久久无码中文字幕| 欧美激情精品久久久久久| 国产韩国精品一区二区三区久久 | 亚洲国产成人精品无码久久久久久综合| 天天做夜夜做久久做狠狠| 国产精品久久久久久久午夜片| 久久天天躁狠狠躁夜夜avapp | 久久福利片| 国产精品视频久久久| 成人国内精品久久久久一区| 波多野结衣久久| 亚洲午夜无码久久久久小说 | 亚洲AⅤ优女AV综合久久久| 国产99久久久久久免费看 | 久久久久久狠狠丁香| 久久人妻少妇嫩草AV无码专区 | 久久精品国产精品亚洲艾草网美妙| 久久Av无码精品人妻系列| 久久免费的精品国产V∧| 久久久噜噜噜www成人网|