views:

278

answers:

2

In my objective-c program, I need to open a URL in Safari no matter what the system's default browser is. That means that this won't work, because it could launch Firefox or whatever other browser:

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURL: url];

I think I'm close with this:

[ws launchAppWithBundleIdentifier: @"com.apple.Safari"
                          options: NSWorkspaceLaunchWithoutActivation
   additionalEventParamDescriptor: NULL
                 launchIdentifier: nil];

only need to figure out how to pass in the URL as parameter... Is there an easier way?

Thanks!

Update: The following code launches Safari with the URL I want, but Safari terminates right away! Any ideas why this is?

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari" 
     options: NSWorkspaceLaunchDefault
additionalEventParamDescriptor: NULL
launchIdentifiers: NULL];

I observed the same behavior with LSOpenFromURLSpec. If a Safari instance is running, it works fine. If no Safari instance was running, it starts a new one and terminates it right away.

Update 2: Safari only crashes for web sites that have Flash embedded. With the code above, I can open google.com just fine, however Safari crashes for a YouTube video, for example.

+2  A: 

NSWorkspace's openURLs:(NSArray *)urls withAppBundleIdentifier:(NSString *)bundleIdentifier options:(NSWorkspaceLaunchOptions)options additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor launchIdentifiers:(NSArray **)identifiers method may do what you want.

jshier
A: 

The two options I listed above actually work for websites that don't include Flash movies.

The crash I described seems to be a bug that can even be reproduced with a single Applescript. I opened a separate question for this (http://stackoverflow.com/questions/2971924/applescript-to-open-url-in-safari-crashes-for-flash-based-websites)

For the record, the answer to my question is to either use LSOpenFromURLSpec or this code:

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
  [ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari" 
  options: NSWorkspaceLaunchDefault
  additionalEventParamDescriptor: NULL
  launchIdentifiers: NULL];
Mark