Open url in safari through cocoa app - macos

I want to open a URL in Cocoa through my app in Safari only. I am using:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: #"my url"]];
But the problem is that if my default browser is not Safari, then the URL gets open in some other browser. But I want my URL to open in Safari only. Please tell the solution.
Thanks :)

let url = URL(string:"https://twitter.com/intent/tweet")!
NSWorkspace.shared.open([url],
withAppBundleIdentifier:"com.apple.Safari",
options: [],
additionalEventParamDescriptor: nil,
launchIdentifiers: nil)

Use NSWorkspace's openURLs(_:​withAppBundleIdentifier:​options:​additionalEventParamDescriptor:​launchIdentifiers:):
let url = NSURL(string:"http://example.com")!
let browserBundleIdentifier = "com.apple.Safari"
NSWorkspace.sharedWorkspace().openURLs([url],
withAppBundleIdentifier:browserBundleIdentifier,
options:nil,
additionalEventParamDescriptor:nil,
launchIdentifiers:nil)

Use scripting bridge with safari to open a URL in safari, You will find a method to open url in the file Safari.h.
To know more about using Scripting bridge refer the link and to use scripting bridge with safari and generate Safari.h, refer my answer here.
The method to open a URL in Safari is:
NSDictionary *theProperties = [NSDictionary dictionaryWithObject:#"https://www.google.co.in/" forKey:#"URL"];
SafariDocument *doc = [[[sfApp classForScriptingClass:#"document"] alloc] initWithProperties:theProperties];
[[sfApp documents] addObject:doc];
[doc release];

You can't use URL, you need a NSString
if(![[NSWorkspace sharedWorkspace] openFile:fullPath
withApplication:#"Safari.app"])
[self postStatusMessage:#"unable to open file"];

To open a URL with any application, you can use the launch services.
The function you want to look at is LSOpenURLsWithRole ;
EDIT:
You will have to link the SystemConfiguration framework to your project for this method to be available.
Apple doc reference here
For example if you want to open http://www.google.com with safari :
//the url
CFURLRef url = (__bridge CFURLRef)[NSURL URLWithString:#"http://www.google.com"];
//the application
NSString *fileString = #"/Applications/Safari.app/";
//create an FSRef of the application
FSRef appFSURL;
OSStatus stat2=FSPathMakeRef((const UInt8 *)[fileString UTF8String], &appFSURL, NULL);
if (stat2<0) {
NSLog(#"Something wrong: %d",stat2);
}
//create the application parameters structure
LSApplicationParameters appParam;
appParam.version = 0; //should always be zero
appParam.flags = kLSLaunchDefaults; //use the default launch options
appParam.application = &appFSURL; //pass in the reference of applications FSRef
//More info on params below can be found in Launch Services reference
appParam.argv = NULL;
appParam.environment = NULL;
appParam.asyncLaunchRefCon = NULL;
appParam.initialEvent = NULL;
//array of urls to be opened - in this case a single object array
CFArrayRef array = (__bridge CFArrayRef)[NSArray arrayWithObject:(__bridge id)url];
//open the url with the application
OSStatus stat = LSOpenURLsWithRole(array, kLSRolesAll, NULL, &appParam, NULL, 0);
//kLSRolesAll - the role with which the applicaiton is to be opened (kLSRolesAll accepts any)
if (stat<0) {
NSLog(#"Something wrong: %d",stat);
}

spawning a process and executing open -a "Safari" http://someurl.foo also does the trick

Related

What API is there for selecting iTunes library location?

How does one programatically set the iTunes library location on macOS to custom locations using e.g. C / Obj-C or Swift API?
Alternatively, environmental settings, such as modifying plists, using the defaults CLI tool, or similar approaches, are also OK for me.
Ordinarily, selecting a custom iTunes library location is done by launching iTunes while holding down the option key. I need to be able to do this in e.g. a unit testing environment / programatically.
You may be able to set it via the prefs.
This is how I access it.
-(void)loadITunesPrefLibraryPath {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *userPref = [userDefaults persistentDomainForName:#"com.apple.iTunes"];
id dataBaseLoc = [userPref objectForKey:#"Database Location"];
NSLog(#"%s dataBaseLoc is:%#", __PRETTY_FUNCTION__, dataBaseLoc);
NSLog(#"%s dataBaseLoc class is:%#", __PRETTY_FUNCTION__, [dataBaseLoc class]);
NSData* dataBaseData = (NSData*)dataBaseLoc;
BOOL staleBook = NO;
NSError* bookError = nil;
NSURL* dataBaseURL = [NSURL URLByResolvingBookmarkData:dataBaseData options:NSURLBookmarkResolutionWithoutMounting relativeToURL:nil bookmarkDataIsStale:&staleBook error:&bookError];
self.libExtDBfile = dataBaseURL;
}
Once you get the userPrefs for iTunes.
And create a BookMarkData from URL.
You might be able to set it via
[userPref setObject:newDataBaseLoc forKey:#"Database Location"];
also see next answer for possible ITLibrary framework private API access

Cannot read external Fotos Library with MediaLibrary framework

I'm trying to read a Fotos Library with the MediaLibrary framework from an external disc ("/Seagate").
Whatever I try out, I only get the data from the default library which is located under "Pictures"
Reading the external library with Fotos app runs perfectly. ( I changed the path in Fotos)
In my app, I tried several options:
connecting to the MediaLibrary using the options dictionary to
include my external device by MLMediaLoadFoldersKey ( see code below). no success
reading mediaGroups with different identifiers. no success
I wonder why I cannot get 2 mediaSources, though I have defined the default folders and my external one
The aspect of lazy loading data from the library should be managed well, as I have set observers for all the steps: library -> sources -> groups -> objects
To point 1:
NSDictionary *options = #{
MLMediaLoadSourceTypesKey: #(MLMediaSourceTypeImage),
MLMediaLoadIncludeSourcesKey: #[MLMediaSourcePhotosIdentifier,MLMediaSourceiPhotoIdentifier],
MLMediaLoadFoldersKey:#[MLMediaLoadFoldersKey,#"//Seagate/Fotos Library"]
if ( mediaLibrary == Nil) {
MLMediaLibrary *MmediaLibrary = [[MLMediaLibrary alloc] initWithOptions:options];
mediaLibrary = MmediaLibrary;
}
[mediaLibrary addObserver:self
forKeyPath:#"mediaSources"
options:0
context:(__bridge void *)#"mediaLibraryLoaded"];
[mediaLibrary mediaSources];
To point 2:
//...
else if (context == (__bridge void *)#"rootMediaGroupLoaded")
{
MLMediaGroup *sharedAlbums = [mediaSource mediaGroupForIdentifier:#"TopLevelAlbums"];
MLMediaGroup *topLevelAlbums = [mediaSource mediaGroupForIdentifier:#"Albums"];
MLMediaGroup *allFotosAlbums = [mediaSource mediaGroupForIdentifier:#"Fotos"];
MLMediaGroup *iPhotoAlbums = [mediaSource mediaGroupForIdentifier:#"iPhotos"];
NSArray *albumList = [sharedAlbums.childGroups arrayByAddingObjectsFromArray:topLevelAlbums.childGroups];
albumList = [albumList arrayByAddingObjectsFromArray:allFotosAlbums.childGroups];
albumList = [albumList arrayByAddingObjectsFromArray:iPhotoAlbums.childGroups];
//albumList = [albumList arrayByAddingObjectsFromArray:#[topLevelAlbums]];
{
[self addObserver:self
forKeyPath:#"nextAlbumIndex"
options:0
context:#"nextAlbumIndex"]; // manages the loop over all albums
#pragma mark query all photos of one Album
//
MLMediaGroup *Nalbum = [[AlbumList objectAtIndex:nextAlbumIndex] group];
[Nalbum addObserver:self
forKeyPath:#"mediaObjects"
options:0
context:#"mediaObjects"];
[Nalbum mediaObjects]; // query the list and store result in Malbum
}}
}
I expect to retrieve the external Fotos library, but I always get the default one.
There is no error message, which could give me some hint.
After consulting Apple Support, I was told that reading Fotos library from another path than the default one is not yet supporrted.

Launch OS X Application Programmatically

How do I programmatically open an OS X app (.app) that is contained within the app I am building?
The preferred way of doing this on OS X is through the NSWorkspace class, which provides a couple of methods to launch applications. One of them, launchApplicationAtURL:options:configuration:error: allows you to specify a file URL to the application to launch. In addition of not having sandbox problems like the system() and Apple Event solution, it also gives you an easy way to manipulate how the application should be launched, eg. you can specify environment variables to be passed to the application.
Following Code snippet is used to launch an app programmatically:
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
path = [path stringByAppendingString:#"/MyApp.app"]; // App Path
NSWorkspace *ws=[NSWorkspace sharedWorkspace];
NSURL* url = [NSURL fileURLWithPath:path isDirectory:NO];
[ws launchApplicationAtURL:url
options:NSWorkspaceLaunchWithoutActivation
configuration:nil
error:nil];
You could use an Apple Script.
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSAppleScript* scriptObject;
scriptObject = [[NSAppleScript alloc] initWithSource:#"try\n
run application \"Macintosh HD:Applications:_Sandbox-AppleScript0.app\"\n
on error number -609 # 'Connection is invalid' error that is spuriously reported # simply ignore\n
end try"];
if (returnDescriptor != NULL) {
// successful execution
if (kAENullEvent != [returnDescriptor descriptorType]) {
// script returned an AppleScript result
if (cAEList == [returnDescriptor descriptorType]) {
// result is a list of other descriptors
}
else {
// coerce the result to the appropriate ObjC type
}
}
}

Best location to store config, plugins for my OS X application

I'm programming an OS X application and would like to know what is considered to be the best location to store application data like config-files and plugins for my program into.
The configs aren't in defaults format since I also deploy this application on Windows.
Usually in your app's subfolder within the Application Support directory is where stuff like is expected to be stored. Apple provides a nice function in their documentation for getting a standardized NSURL for your Application Support directory.
Extracted from their documentation:
- (NSURL*)applicationDirectory
{
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID to the URL for the
// Application Support directory
dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
// If the directory does not exist, this method creates it.
// This method is only available in OS X v10.7 and iOS 5.0 or later.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
attributes:nil error:&theError])
{
// Handle the error.
return nil;
}
}
return dirPath;
}
You can call the subpath within NSApplicationDirectory anything you want, but they recommend using your bundle identifier, as seen in the example above.

In-memory mime-type detection with Cocoa (OS X)?

My application is a viewer for a custom format, a zip file with a well defined XML manifest and resources, such as images and movies. I use zlib to open up the zip file in memory and then proceed to display said resources.
One problem I've ran into is that I'm unable to properly display videos, apparently because QTMovie cannot determine the mime-type. Movie loaded from file ([QTMovie movieWithFile]) works perfectly. Loaded from memory ([QTMovie movieWithData]) refuses to work.
This makes sense, because lacking the file extension, QTMovie cannot determine the mime-type information. After a bit of a search, I've resorted to using QTDataReference in the following mannner:
NSData *movieData = ...read from memory...;
QTDataReference *movieDataReference = [[QTDataReference alloc] initWithReferenceToData:movieData name:fileName MIMEType:#"video/x-m4v"];
QTMovie *mov = [QTMovie movieWithDataReference:movieDataReference error:&err];
This works nicely, however hardcoding MIMEType is far from ideal. I have access to the filename and the extension, so I've attempted to find the mime-type using UTI (thanks to the nice folks on #macdev):
- (NSString*)mimeTypeForExtension:(NSString*)ext {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)ext,NULL);
return NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)UTI,kUTTagClassMIMEType));
}
This however doesn't work. Clearly, there's an internal OS X database of extensions and corresponding mime-types, somewhere. Otherwise from-disk movies wouldn't work. How do I get access to it?
Thanks!
The problem your having is that m4v and m4p dont have a mime types registered with Launch Services (probably because the mime type for m4v and m4p is not standard). In any event, what you should probably do is handle edge cases like this and then check for nil when the function returns (in case the extension is both not registered and not handled by you).
The other thing is that you're leaking memory with your current use. I'm assuming you're using garbage collection, but the first call creates a CFString that is never released. Here is a suggested implementation of your method:
-(NSString*)mimeTypeForExtension:(NSString*)ext
{
NSAssert( ext, #"Extension cannot be nil" );
NSString* mimeType = nil;
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)ext, NULL);
if( !UTI ) return nil;
CFStringRef registeredType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
if( !registeredType ) // check for edge case
{
if( [ext isEqualToString:#"m4v"] )
mimeType = #"video/x-m4v";
else if( [ext isEqualToString:#"m4p"] )
mimeType = #"audio/x-m4p";
// handle anything else here that you know is not registered
} else {
mimeType = NSMakeCollectable(registeredType);
}
CFRelease(UTI);
return mimeType;
}
You can use NSWorkspace to have the system guess UTI of a file.
-(NSString *)mimeTypeForFileAtPath:(NSString *)path error:(NSError **)err {
NSString *uti, *mimeType = nil;
if (!(uti = [[NSWorkspace sharedWorkspace] typeOfFile:path error:err]))
return nil;
if (err)
*err = nil;
if ((mimeType = (NSString *)UTTypeCopyPreferredTagWithClass((CFStringRef)uti, kUTTagClassMIMEType)))
mimeType = NSMakeCollectable(mimeType);
return mimeType;
}
Suggest that people change the return to [mimeType autorelease]; - some of us still use the ancient ways!
And thanks! This was a big help.

Resources