1 頁 (共 1 頁)

[問題]Cocoa Application選擇多個檔案並使用NSWorkspace開啟應用

發表於 : 12/17/2008 6:55 pm
nat626
請問各位前輩
我正在嘗試自己寫一個應用程式
功能為選擇檔案後
自動將檔案載入到Preview.app應用程式裡
目前已經可以載入單一檔案
但選擇多個檔案該怎麼辦?
麻煩前輩指點一下
謝謝 :)
以下是開啓單一檔案的code

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
if ([openPanel runModalForTypes:nil] == NSOKButton) {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace openFile:[openPanel filename] withApplication:@"Preview.app"];
}

Re: [問題]Cocoa Application選擇多個檔案並使用NSWorkspace開啟

發表於 : 12/17/2008 7:48 pm
yllan
nat626 寫:請問各位前輩
我正在嘗試自己寫一個應用程式
功能為選擇檔案後
自動將檔案載入到Preview.app應用程式裡
目前已經可以載入單一檔案
但選擇多個檔案該怎麼辦?
麻煩前輩指點一下
謝謝 :)
以下是開啓單一檔案的code

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
if ([openPanel runModalForTypes:nil] == NSOKButton) {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace openFile:[openPanel filename] withApplication:@"Preview.app"];
}

代碼: 選擇全部

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
if ([openPanel runModalForTypes:nil] == NSOKButton) {
	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
	for (NSString *file in [openPanel filenames])
		[workspace openFile: file withApplication: @"Preview.app"];
}
[/code]

Re: [問題]Cocoa Application選擇多個檔案並使用NSWorkspace開啟

發表於 : 12/18/2008 10:18 am
nat626

代碼: 選擇全部

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
if ([openPanel runModalForTypes:nil] == NSOKButton) {
	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
	for (NSString *file in [openPanel filenames])
		[workspace openFile: file withApplication: @"Preview.app"];
}

謝謝yllan指點
但用for來處理的話他會將各個檔案載入到不同的Preview.app
我希望的功能是能將各個檔案載入到同一個Preview.app
就是可以用Preview.app的 "上一頁" 和 "下一頁" 來切換圖檔的功能
昨天查了一個晚上
NSWorkspace是不是沒有提供這樣的方法呀?
或是各位前輩是否有其他辦法可以達成呢?
麻煩前輩指點一下 THX :)

發表於 : 12/18/2008 11:34 am
yllan
哇…

那只能用 NSTask + /usr/bin/open 啦~

發表於 : 12/18/2008 11:52 am
nat626
yllan 寫:哇…

那只能用 NSTask + /usr/bin/open 啦~
沒用過NSTask + /usr/bin/open :(
我查一下該如何使用 :)
希望不會太困難~
謝謝yllan指點 thank you !! :)

發表於 : 12/18/2008 1:14 pm
nat626
謝謝yllan指點
目前將code改成以下
可以達成我希望的功能 :)

代碼: 選擇全部

NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
[openPanel setAllowsMultipleSelection:YES]; 
if ([openPanel runModalForTypes:nil] == NSOKButton) { 
   NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[openPanel filenames]];
   [task launch];
}
但目前好像都是用預設的應用程式開啟所選取的檔案
如果我想改成用 iPhoto.app 或其他應用程式開啟我所選擇的檔案
該如果達成呢?
還有請前輩指點
Thx :)

發表於 : 12/18/2008 3:37 pm
yllan
參數加 -a [application] 即可。

詳情請 man open

發表於 : 12/18/2008 5:21 pm
nat626
yllan 寫:參數加 -a [application] 即可。

詳情請 man open
謝謝yllan指點 :)
目前已經研究出如何使用
以下是更改後的code

代碼: 選擇全部

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
if ([openPanel runModalForTypes:nil] == NSOKButton) {
	NSString *application = [[NSString alloc] initWithFormat:@"-a%@", @"iPhoto"];
	NSMutableArray *args = [[NSMutableArray alloc] init];
	[args addObject: application];
	for(NSString *file in [openPanel filenames]){
		[args addObject:file];
	}		
	NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:args];
	[task launch];
}
只要將@"iPhoto"換成別的Application就可以更換開啓的應用程式
目前小弟還在練習當中
不知道這隻小程式目前寫到這裡是否有需要自改進的地方
請各位前輩多多指教 :)
謝謝