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

                [workspace openURL: url];
            }
            posted on 2011-09-05 16:04 羅朝輝 閱讀(3184) 評論(0)  編輯 收藏 引用 所屬分類: 移動開發Cocoa 開發
            青青国产成人久久91网| 人妻无码精品久久亚瑟影视 | 久久久久综合网久久| 久久久久亚洲精品天堂| 久久人人爽人人爽人人AV东京热| 女人高潮久久久叫人喷水| 一本色道久久88综合日韩精品 | 成人a毛片久久免费播放| 久久男人AV资源网站| 99久久夜色精品国产网站| 99久久国语露脸精品国产| 久久精品一区二区影院| 亚洲第一极品精品无码久久| 99久久国产精品免费一区二区 | 香蕉久久夜色精品国产尤物| 亚洲午夜久久久久妓女影院 | 亚洲成色www久久网站夜月| 久久国产精品久久国产精品| 香蕉久久AⅤ一区二区三区| 欧洲精品久久久av无码电影| 久久综合久久综合久久| 中文字幕热久久久久久久| 99久久免费国产特黄| 热99RE久久精品这里都是精品免费| 国内精品久久久久久99| 日产精品久久久久久久| 久久电影网2021| 亚洲va久久久噜噜噜久久天堂| 国产精品日韩欧美久久综合| 久久人人爽爽爽人久久久| 亚洲国产成人久久综合野外| 久久国产精品-国产精品| 亚洲精品无码久久久影院相关影片 | 久久久久久毛片免费看| 久久精品国产亚洲欧美| 亚洲精品无码专区久久久| 国内精品久久久久影院亚洲| 久久播电影网| 精品99久久aaa一级毛片| 国产成人精品久久综合| 94久久国产乱子伦精品免费 |