• <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),移動(dòng)平臺(tái),圖形開發(fā)。-->加微博 ^_^

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

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

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


            - (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 羅朝輝 閱讀(3199) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 移動(dòng)開發(fā)Cocoa 開發(fā)
            99久久婷婷国产综合亚洲| 久久强奷乱码老熟女网站| 伊人久久大香线蕉av一区| 久久久久亚洲av无码专区导航 | 一本久久a久久精品vr综合| 少妇久久久久久被弄高潮| 亚洲国产精品久久久久| 久久激情亚洲精品无码?V| 中文字幕久久久久人妻| 青青青青久久精品国产| 精品久久久久久国产| 九九精品99久久久香蕉| 久久久久久午夜精品| 一本伊大人香蕉久久网手机| 国内精品久久久久影院老司| 久久精品国产亚洲AV不卡| 亚洲精品乱码久久久久久自慰| 久久久久中文字幕| 日产精品久久久一区二区| 国产成人综合久久精品红| 人人狠狠综合久久亚洲88| 久久大香香蕉国产| 亚洲国产精品无码久久青草 | 久久午夜无码鲁丝片秋霞| 91秦先生久久久久久久| 久久99精品久久久久久久久久| 久久婷婷五月综合国产尤物app | 久久被窝电影亚洲爽爽爽| 久久国产热精品波多野结衣AV| 蜜桃麻豆WWW久久囤产精品| 亚洲综合久久夜AV | 久久久精品久久久久久| 99久久综合国产精品二区| 精品久久久久久久久久中文字幕| 99久久无码一区人妻| 国产精品99久久久久久董美香| 久久精品国产精品青草app| 久久久噜噜噜www成人网| 久久精品aⅴ无码中文字字幕不卡| 久久综合香蕉国产蜜臀AV| 久久久亚洲欧洲日产国码aⅴ |