Setting a folder as a device in Finder with Cocoa

Is there a way to set a folder on your hard drive as a device in Finder. The goal here is to provide the user with easy access to the folder that my application uses to store data. I don't want my user to start looking for data in Application Data. I'd rather let them make this data available as a mounted volume or device in the Finder. I would also like this volume or device to be read / write, so that if the user makes any changes to the data files, the changes will appear in the original folder.

Is there a way to do this in cocoa, carbon, or applescript.

+1


source to share


5 answers


Try learning FUSE. You can have all kinds of psuedo filesystems.

But I would give a little caution about what you are trying to do. It might make sense to just create a button that opens a folder in your app rather than creating a new device. I personally would find it difficult to keep using an application that does such a thing. It is not suitable for the rest of the available applications.



You can also use an alias to point to the application data directory.

+4


source


You can use a sparse disk image to create a "fake" disk.

But why not create a data directory in your application? Or use a subdirectory in ~/Documents/

?



Alias ​​/ symlink on desktop would be the simplest solution:

ln -s '~/Application Data/Yourapp' '~/Desktop/Yourapp Data'

      

+1


source


May I suggest rethinking this completely? A symbolic link or alias will work, but if possible it is a better idea to register files of the types that people will move to that folder and then respond to opening them by moving or copying them to the correct folder. I'm thinking of something like the Dashboard interface where if you double-click the downloaded .wdgt file it asks if you want to "install" the widget and then, if you do, copies it to ~ / Library / Widgets. Obviously, if you are dealing with general types such as images, folders, or general text files, this may not be practical.

For implementation, you simply add document types to your Info.plist and process them in the App Delegate -application: openFile: method.

+1


source


I would also highly recommend this, it seems somewhat confusing for most users. However, you thought you were just creating a softlink for this directory?

0


source


I am using NSWorkspace. In my case, I am doing an initial check with the function - (BOOL) isMountedPath;

Installation code:

    NSURL *path=[NSURL URLWithString:@"smb://server.resource/KEYS_DB"];
    if(NO==[self isMountedPath:[path absoluteString]])
    {
        NSWorkspace *ws=[NSWorkspace sharedWorkspace];
        [ws openURL:path];
    }

      

Code to check the set path:

-(BOOL)isMountedPath:(NSString *)share
{
    NSArray * keys = @[NSURLVolumeURLForRemountingKey];
    NSArray * mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];

    NSError * error;
    NSURL * remount;

    for (NSURL * mountPath in mountPaths) {
        [mountPath getResourceValue:&remount forKey:NSURLVolumeURLForRemountingKey error:&error];
        if(remount){
            if ([[[NSURL URLWithString:share] host] isEqualToString:[remount host]] && [[[NSURL URLWithString:share] path] isEqualToString:[remount path]])
            {
                printf("Already mounted at %s\n", [[mountPath path] UTF8String]);
                return YES;
            }
        }
    }
    return NO;
}

      

Another possible useful method is:

-(NSString *)mountedPath:(NSString *)share
{
    NSArray * keys = @[NSURLVolumeURLForRemountingKey];
    NSArray * mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];

    NSError * error;
    NSURL * remount;

    for (NSURL * mountPath in mountPaths) {
        [mountPath getResourceValue:&remount forKey:NSURLVolumeURLForRemountingKey error:&error];
        if(remount){
            if ([[[NSURL URLWithString:share] host] isEqualToString:[remount host]] && [[[NSURL URLWithString:share] path] isEqualToString:[remount path]])
            {
                printf("Already mounted at %s\n", [[mountPath path] UTF8String]);
                return [mountPath path];
            }
        }
    }
    return nil;
}

      

0


source







All Articles