I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another?
is there some NSSomething method I can call?
Do I have to work with Input/Output streams?
Or is the best way maybe to just rely on passing commands to the finder?
Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods?
Thanks for your help!
NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you'd use NSFileManager since its easier to work with:
if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] )
[[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];
However, NSWorkspace can easily move files to the Trash, which NSFileManager can't do.
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:foldername destination:#"" files:filenamesArray tag:&tag];
Check the documentation for a more complete description of the two classes. (NSFileManager, NSWorkspace)
[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]
Here's the link to the class reference:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/copyItemAtPath:toPath:error:
You can also use FSCopyObjectAsync function. You can display file copy progress and you can also cancel file copy using FSCopyObjectAsync().
Take a look at this post.
FSCopyObjectAsync is Deprecated in OS X v10.8
copyfile(3) is alternative for FSCopyObjectAsync. Here is example of copyfile(3) with Progress Callback.
Related
i need to save a .plist file NOT to documents, but to the core of program.
For example my program called "123" and if i save data, then send my app to my friend and he opens this app he could see saved data, no matter where he puts this program. I can't find solution to this problem, please help me.
I'm making mac app.
and i save plist with
[NSKeyedArchiver archiveRootObject:FBCover1.text=
[NSString stringWithFormat:#"%#",Cover1.attributedStringValue]
toFile:#"/Users/admin/FBCover1.plist"];
General answer:
If you're trying to do this on iPhone (you didn't tag this for iOS or MacOS), this isn't going to work as this will break your code signing.
If you're doing this on MacOS and you're using code signing, you'll have the same problem.
There may be places where you could save and share data, such as Game Center or DropBox or Box or some other cloud storage mechanism, but you'll need to pick up and make use of some additional API's or frameworks.
Specific answer just for you:
Instead of:
[NSKeyedArchiver archiveRootObject:FBCover1.text=[NSString stringWithFormat:#"%#",Cover1.attributedStringValue] toFile:#"/Users/admin/FBCover1.plist"];
which is big and ugly and I don't know what the heck it's doing, why not save your string this way?
NSString * stringToSave = [NSString stringWithFormat:#"%#",Cover1.attributedStringValue];
if(stringToSave)
{
NSError * error = nil;
BOOL success = [stringToSave writeToFile: #"/Users/admin/FBCovert1.txt" atomically: YES encoding: NSUTF8StringEncoding error: #error];
if(!success)
{
NSLog( #"error in saving - %#", [error localizedDescription]);
}
}
This saves the raw string into a file.
If you want to do it as a plist, then create a NSDictionary and save your string as the value with some appropriate key.
Preamble: this is an awful idea. What you should do is create a document-based application and pass your document backwards and forwards.
Literal answer:
You can use NSBundle to get the path of the resources folder within your application bundle with something like:
[[NSBundle mainBundle] resourcePath]
The resources folder is where application resources, such as plists, are meant to go. You're supposed to consider your application bundle as read-only in general but that's as good a choice as any if you want to hack away.
I'm trying to get list of files in Library folder in cocoa, i use this code and NSHomeDrirectory() function without sandboxing and work well.
TempArray = [[NSFileManager defaultManager] directoryContentsAtPath:FolderURL]
but when I checked sandbox is code not work, Is it any entitlements should i add? or what code can replace?
I haven't tried if this will get all the files but it should since you can save and read files from the library.
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *libraryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:libraryPath error:nil];
If you add below entitlement you can access ~/Library or /Library folders
com.apple.security.temporary-exception.files.home-relative-path.read-write: /Library/
But after this you will get reply from apple is
2.31:Apps that are not sandboxed appropriately may be rejected
I didn't get answer of this. If anyone knows please post here.
I'm loading a folder with files using this method:
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:#"GBC/Caras/"];
fotosJugadores = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: #"/Users/exular/Developer/GBC/exular/GBC/Caras/" error: &error];
But the problem remains to get the same functionality to do it once the app will run in the device, how can I set a folder with all that images to be available in the device? Is there any bundle method similar to contentsOfDirectoryAtPath: to load images from project bundle, something like imageNamed: ?
thanks
Is there any bundle method...
When you start a question like that, the answer is usually found in the NSBundle class reference. In this case, the answer is yes, there is such a method. You will see that if you read the documentation. You should always check the documentation before asking for help.
I am tying to get the content of the trash in a Cocoa application and I was wondering if this is a special path or if I have to use dedicated functions.
Thanks for your help,
Regards,
There are several special paths for trash items.
Each user has a .Trash directory in their home directly.
Each mounted volume has a .Trashes directory in its root with a subdirectory for each user. This is so that trash items on removable drives like USB keys stay on the drive.
The above are implementation details (that have stayed constant since 10.0) so I'm not sure if it is possible to rely on them. An alternative to going to the directories is to use the scripting bridge to Finder. There's an example in the Scripting Bridge programming guide.
You can use URLForDirectory to get Trash directory related to the file, home directory or external Volume.
NSURL *trashURL = [[NSFileManager defaultManager] URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL:dirURL create:NO error:&error];
You can call this function for all volumes mounted by using " mountedVolumeURLsIncludingResourceValuesForKeys"
"Macintosh HD>Users>your username>.Trash"
It is hidden to finder but you can locate it in the terminal or otherwise. From then on it is a normal folder and you can do whatever functions you would like to it.
Could be done like such
NSError *error=nil;
NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:#".Trash"];
NSArray *folderList=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
I need some help in using the NSURLCache to download a simple file. It would need to be saved in the Documents folder of the app. I have seen the URLCache example that apple presented, but I didn't find it useful since it was an image that was being shown. In addition there was some complex code on the modification date. I actually need something when the application opens it will begin the download process. Can someone guide me through just the download process of any file?
Thanks
Kevin
NSURL *myURL = [NSURL urlWithString:#"http://www.google.com/theInternet.zip"];
NSData *myData = [NSData dataWithContentsOfURL:myURL];
[myData writeToFile:#"ThePath" atomically:YES];
This will block, but should be ok for small downloads. If you are going to be downloading something that is many megabytes then look into asynchronous downloads. You can see an example here:
http://github.com/erica/iphone-3.0-cookbook-/tree/master/C13-Networking/07-Asynchronous%20Downloads/