What API is there for selecting iTunes library location? - macos

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

Related

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.

Accessing 'Internet Accounts' programmatically

I'm trying to access the 'accounts' that are stored in the Internet Accounts area on the system settings on OSX. I'm aware of the 'ACAccount' library, but this only really seems to be of any use for Social integration, for example Facebook/Twitter, what i would like is to be able to detect that you have an Exchange account there and open up certain features within an app. But i'm guessing i'll need to get my users to re-enter their details in my app?
I did try and use it, however i get an empty array.
ACAccountStore *store = [[ACAccountStore alloc] init];
NSArray *accounts = [store accounts]; //This is empty
Does anyone know if i'm missing something, or if it's not possible? Thanks!
Edit
I have gained access to the Twitter account for example, but it's not returned in the Accounts list, i had to request permission first. Which makes sense. However, i still see no way of getting access to the Exchange account.
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account
accountsWithAccountType:accountType];
}
}];

Local Notification When App is Downloaded

I want to have a local notification show up as soon as the application is downloaded off the app store and is opened. Thanks.
You can do that in your app delegate's didFinishLaunchingWithOptions. You need to do the following in this method:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
....
//Get the version number of the application using this technique: http://stackoverflow.com/questions/458632/how-can-my-iphone-app-detect-its-own-version-number
NSString version = [self appVersion];
//Because you only want to display the notification on first launch so have a flag in user defaults to track that. Also note that you need to include this in your registerDefaults and set to NO
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL alreadyDisplayedNotification = [defaults boolForKey:#"alreadyDisplayedNotificationOnStartForVersion"];
if ([version isEqualToString:#"VersionForWhichYouWantNotification"] && !alreadyDisplayedNotification) {
//Display Notification...
// Set the flag in user default to track that notification has been displayed
[defaults setBool:YES forKey:#"alreadyDisplayedNotificationOnStartForVersion"];
}
.....
}

Resources