2011-02-19 19 views
8

के साथ मैक ऐप लॉन्च करना कमांड लाइन के साथ पथ खोजक ऐप लॉन्च करते समय, मैं open -a Path Finder.app /Users/ का उपयोग करता हूं। इस विचार के आधार पर, मैं पथ खोजक लॉन्च करने के लिए निम्न कोड का उपयोग करता हूं।उद्देश्य-सी/कोको

क्या मेरे पास open कमांड लाइन का उपयोग किये बिना लॉन्च ऐप हो सकता है?

NSTask *task; 
task = [[NSTask alloc] init]; 
[task setLaunchPath: @"/usr/bin/open"]; 

NSArray *arguments; 
arguments = [NSArray arrayWithObjects: @"-a", @"Path Finder.app", @"/Users/", nil]; 
[task setArguments: arguments]; 

NSPipe *pipe; 
pipe = [NSPipe pipe]; 
[task setStandardOutput: pipe]; 

NSFileHandle *file; 
file = [pipe fileHandleForReading]; 

[task launch]; 

उत्तर

24
if(![[NSWorkspace sharedWorkspace] launchApplication:@"Path Finder"]) 
    NSLog(@"Path Finder failed to launch"); 
मानकों के साथ

NSTask *task = [[NSTask alloc] init]; 
NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"Path Finder"]]]; 
[task setLaunchPath:[bundle executablePath]]; 
NSArray *arguments = [NSArray arrayWithObjects:@"Argument1", @"Argument2", nil]; 
[task setArguments:arguments]; 
[task launch]; 
+0

आवेदन का पैरामीटर **? – prosseek

+1

आपको launchApplicationAtURL का उपयोग करने की आवश्यकता होगी: विकल्प: कॉन्फ़िगरेशन: त्रुटि: ऐसा करने के लिए। मैं अपनी पोस्ट में इसका उपयोग करके एक उदाहरण जोड़ूंगा। – ughoavgfhw

+0

खैर, मुझे यकीन नहीं है, लेकिन संक्षिप्तता के मामले में, मेरा जवाब (ओपनफाइल का उपयोग करके: आवेदन के साथ :) बेहतर लगता है (समझने में आसान और छोटा)। – prosseek

3

different posting में युजी के जवाब के आधार पर, NSWorkspace उपयोग करने के लिए उपकरण है, और मैं कोड के केवल दो लाइनों के साथ एक ही परिणाम मिल सकता है।

openFile पैरामीटर को Path Finder पर पैरामीटर पास करने के लिए उपयोग किया जा सकता है, जो आमतौर पर निर्देशिका है, फ़ाइल नहीं। हालांकि, यह ठीक काम करता है।

NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; 
NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:@"Path Finder"]]; 
//Handle url==nil 
NSError *error = nil; 
NSArray *arguments = [NSArray arrayWithObjects:@"Argument1", @"Argument2", nil]; 
[workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:arguments forKey:NSWorkspaceLaunchConfigurationArguments] error:&error]; 
//Handle error 

तुम भी तर्क पारित करने के लिए इस्तेमाल कर सकते हैं NSTask:

[[NSWorkspace sharedWorkspace] openFile:string2 withApplication:@"Path Finder"]; 
[[NSApplication sharedApplication] terminate:nil]; 
+0

इस यह काम नहीं करेगा _parameter_ एक फ़ाइल लेकिन एक नहीं है ** पैरामीटर ** पैरामीटर देने के लिए – Shebuka