• <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://www.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ā)
            2021最新久久久视精品爱 | 精品久久久久久无码人妻热 | 久久亚洲精品无码观看不卡| 久久伊人五月丁香狠狠色| 人妻久久久一区二区三区| 久久精品成人免费观看97| 亚洲AV无码久久精品成人| 久久久久久毛片免费看| 日日噜噜夜夜狠狠久久丁香五月| 好属妞这里只有精品久久| 国内精品伊人久久久久777| 国产一区二区三精品久久久无广告| 亚洲精品无码久久久久去q| 亚洲国产日韩欧美久久| 99久久无码一区人妻| 狼狼综合久久久久综合网| 久久久久这里只有精品 | 久久久99精品成人片中文字幕| 久久棈精品久久久久久噜噜| 一本一道久久a久久精品综合| 秋霞久久国产精品电影院| 久久精品国产亚洲av麻豆色欲| 亚洲国产综合久久天堂| 久久久99精品成人片中文字幕| 99久久免费国产特黄| 97久久天天综合色天天综合色hd| 欧美日韩精品久久免费| 婷婷久久五月天| 国产香蕉久久精品综合网| 性高湖久久久久久久久AAAAA| 国产亚洲精午夜久久久久久| 91超碰碰碰碰久久久久久综合| 7777久久亚洲中文字幕| 国产亚洲欧美成人久久片| 亚洲色欲久久久综合网东京热| 亚洲AV无码1区2区久久| 亚洲国产精品久久久天堂| 久久国产色AV免费观看| 色综合久久精品中文字幕首页| 狠狠88综合久久久久综合网| 久久97精品久久久久久久不卡|