• <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>

            羅朝輝(飄飄白云)

            關注嵌入式操作系統,移動平臺,圖形開發。-->加微博 ^_^

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

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

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


            - (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 羅朝輝 閱讀(3184) 評論(0)  編輯 收藏 引用 所屬分類: 移動開發Cocoa 開發
            91久久国产视频| 亚洲αv久久久噜噜噜噜噜| 亚洲嫩草影院久久精品| 久久精品国产精品亚洲精品| 精品久久久久久无码中文字幕| 污污内射久久一区二区欧美日韩| 久久精品国产99国产精品亚洲| 亚洲国产精品久久久天堂| 品成人欧美大片久久国产欧美| 欧美成人免费观看久久| 久久91精品国产91久久小草| 综合久久精品色| 国产精品久久久久…| 97精品国产97久久久久久免费| 久久精品国内一区二区三区| 伊人久久大香线蕉综合影院首页 | 久久丫精品国产亚洲av不卡| 久久国产乱子精品免费女| 97香蕉久久夜色精品国产| 久久国产视屏| 久久精品国产亚洲AV大全| 性做久久久久久免费观看| 精品久久久久久亚洲精品| 久久久噜噜噜久久中文字幕色伊伊| 久久九九全国免费| 成人久久精品一区二区三区| 久久久久亚洲AV无码专区首JN | 狠狠色综合网站久久久久久久高清 | 久久91精品综合国产首页| 久久久无码精品午夜| 久久精品国产网红主播| 亚洲国产精品一区二区久久hs| 久久经典免费视频| 伊人久久精品无码二区麻豆| 久久天天躁狠狠躁夜夜不卡| 久久久久亚洲国产| 国产精品久久久久久久人人看| 欧美精品乱码99久久蜜桃| 狠狠色丁香婷婷久久综合| 2021国内精品久久久久久影院| 亚洲七七久久精品中文国产|