0

I'm having trouble moving a file created by a shell script in my Mac application.

The file seems to be saved in:

/Users/USERNAME/Library/Developer/Xcode/DerivedData/APPLICATION_NAME/Build/Products/Debug/

I have tried using the NSFileManager as follows but nothing happens:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[[NSFileManager defaultManager] movePath:[NSString stringWithFormat:@"%/Users/USERNAME/Library/Developer/Xcode/DerivedData/APPLICATION_NAME/Build/Products/Debug/%@", filename] toPath:documentsDirectory handler:nil];

As well as trying to move the file using a shell script with NSTask.

I'm probably doing something wrong, does anyone have any idea how I can move a file from that folder to perhaps the Documents folder or even better the Desktop?

Thanks in advance everyone

Cristian
  • 6,765
  • 7
  • 43
  • 64
  • Can you be more specific about when you're trying to move this file? It looks like you're trying to do something at build time. Is this a build phase that builds a file using a script and then you want to load that into your final app? – gaige Jun 08 '12 at 10:33
  • No it isn't, when the app runs, I download a file, and I then want to move it, it doesn't matter where but in the Home folder – Cristian Jun 08 '12 at 11:37

1 Answers1

1

To locate the file inside of your Application bundle, you will need to use the mainBundle. Assuming you've got a file foo.txt that you need to locate, and it's being stored in the standard resource location, do the following:

NSBundle *appBundle = [NSBundle mainBundle];
NSString *srcPath = [appBundle pathForResource: @"foo" ofType: @"txt"];

at this point, you should have the path to your foo.txt in srcPath.

If you are downloading the file for the user, you should place it into the user's Downloads directory. If you are downloading the file for internal use, it belongs in either /Library/Application Support/<yourapp>/... or ~/Library/Application Support/<yourapp>....

The right way to find these is using NSSearchPathForDirectoriesInDomains.

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDownloadsDirectory, 
                    NSUserDomainMask, YES);

will yield an array (should be 1 element), with the current user's download directory path in it. If they're requesting the file, you should use this as the destination path.

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory,
                    NSUserDomainMask, YES);

will yield an array (should be 1 element), with the current user's Application Support path in it. If this is a per-user download for the App's internal use, it should be stored in a sub-directory created by using your App name or identifier appended to this path.

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, 
                   NSSystemDomainMask, YES);

will yield an array (should be 1 element), with the machine's Application Support path in it. If this is a machine-wide download for the App's internal use, it should be stored in a sub-directory created by using your App name or identifier appended to this path.

Once you've created your source and destination path, you'll want to use NSFileManager to move the file into place. In the case of the application support directories, you'll need to first verify that the directory you want exists, and if it doesn't, create it (using -createDirectoryAtPath:withIntermediateDirectories:attributes:error).

NSFileManager *manager = [[NSFileManager alloc] init];
NSError *error;
if (![manager copyItemAtPath: srcPath toPath: dstPath error: &error]) {
    NSLog("Error copying %@ to %@, %@", srcPath, dstPath, error);
}
[manager release];
gaige
  • 17,263
  • 6
  • 57
  • 68
  • thanks very much, I'm aware on how to get these directories, but how can i place a file within them? – Cristian Jun 08 '12 at 12:05
  • thanks for the update, is the srcPath the bundle resourcePath? and do I have to include the filename? for example resourcePath/filename? Thanks very much, it's nearly working – Cristian Jun 08 '12 at 12:27