mac :NSWorkspace

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)用,運(yùn)行效果圖如下:



- (IBAction) launchApplication:(id) sender

{

NSWorkspace?*?workspace?=?[NSWorkspace?sharedWorkspace];

//BOOL?wasLaunched?=?[workspace?launchApplication:@"Safari"];

//?launch?without?activation

//

BOOLwasLaunched?=?[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];

BOOLremovable?=?NO;

BOOLwriteable?=?NO;

BOOLunmountable?=?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://blog.csdn.net/kesalin/"];

NSWorkspace?*?workspace?=?[NSWorkspace?sharedWorkspace];

[workspace?openURL:?url];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容