I'm a beginner in this language.
I want to move a file from one folder to another my code is here.
The console give me file exists at path twice
And the moveItemAtPath gives me always false
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSString * originPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Downloads/1.wav"];
NSString * finalPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Desktop"];
NSFileManager * manager = [[NSFileManager alloc]init];
if( [ manager fileExistsAtPath:originPath]){
NSLog(#"The file exists at: %#",originPath);
}
if( [manager fileExistsAtPath:finalPath]){
NSLog(#"The file exists at: %#",finalPath);
}
if([manager moveItemAtPath:originPath toPath:finalPath error:nil]){
NSLog(#"True");
}else{
NSLog(#"FALSE");
}
}
return 0;
}
First of all you should use [NSFileManager defaultManager] instead of creating a new NSFileManager. It will return a singleton-instance of NSFileManager.
Add this to your second "ifFileExists"-check:
[[NSFileManager defaultManager] removeItemAtPath:finalPath error:nil];
This will remove the item at the target-path, before moving a new one to this destination.
Related
I want to get system-defined directories' (eg Desktop, Documents, Downloads) localized display names same as shown in Finder's Favorites.
I tried using NSFileManager's displayNameAtPath: but it only worked for custom Directory.localized directory containing *. strings files and not worked for system-defined directories.
Below is my code I checked this with.
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (argc < 2) {
// Use ~/Application directory if no command line arguments are provided
NSString *localizedApplicationDirectoryName = [[NSFileManager defaultManager] displayNameAtPath:NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSLocalDomainMask, YES).lastObject];
NSLog(#"%#", localizedApplicationDirectoryName);
[pool drain];
return 1;
}
// Use command line arguments
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *directory = [NSString stringWithUTF8String:argv[1]];
NSString *displayName = [fileManager displayNameAtPath:directory];
NSLog(#"%#", displayName);
[pool drain];
return 0;
}
What I'm doing wrong? Are there another methods to get this translations?
Get the localized name from the URL for example the desktop folder
NSURL *url = [[NSURL fileURLWithPath:NSHomeDirectory()] URLByAppendingPathComponent: #"Desktop"];
NSString *localizedName;
NSError *error;
[url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:&error];
if (error) {
NSLog(#"%#", error);
} else {
NSLog(#"%#", localizedName);
}
I was following a code sample from "Programming in Objective-C Fourth Edition" by Stephen Kochan.
The program looks for a file, and performs a few things on it:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSString *fName = #"testfile";
NSFileManager *fm;
NSDictionary *attr;
//Need to create an instance of the file manager
fm = [NSFileManager defaultManager];
//Let's make sure our test file exists first
NSLog([fm currentDirectoryPath]);
if ([fm fileExistsAtPath: fName] == NO) {
NSLog(#"File doesn't exist!");
return 1;
}
//now lets make a copy
if ([fm copyItemAtPath: fName toPath: #"newfile" error: NULL]) {
NSLog(#"File Copy failed!");
return 2;
}
//Now let's see test to see if the two files are equal
if ([fm contentsEqualAtPath: fName andPath: #"newfile"] == NO) {
NSLog(#"Files are Not Equal!");
return 3;
}
//Now lets rename the copy
if ([fm moveItemAtPath: #"newfile" toPath: #"newfile2" error: NULL] == NO) {
NSLog(#"File rename Failed");
return 4;
}
//get the size of the newfile2
if((attr = [fm attributesOfItemAtPath: #"newfile2" error: NULL]) == nil)
{
NSLog(#"Couldn't get file attributes");
return 5;
}
NSLog(#"File size is %llu bytes", [[attr objectForKey: NSFileSize] unsignedLongLongValue]);
//And finally, let's delete the original file
if([fm removeItemAtPath: fName error: NULL])
{
NSLog(#"file removal failed");
return 6;
}
NSLog(#"All operations were successful");
//Display the contents of the newly-createed file
NSLog(#" %#", [NSString stringWithContentsOfFile: #"newfile2" encoding:NSUTF8StringEncoding error: NULL]);
}
return 0;
}
I created a file named "testfile" and placed it in project directory. When I ran the program, It couldn't find the file. I added [NSFileManager currentDirectoryPath] to check the current path which was apparently something like this:
/Users/myusername/Library/Developer/Xcode/DerivedData/Prog_16.1-eekphhgfzdjviqauolqexkowfqfg/Build/Products/Debug
I went looking for the Library directory, but it doesn't exist. Is this some temporary directory that's created when the program is running, then deleted after it exits?
Edit: I've tried changing the path of the current directory using [NSFileManager changeCurrentDirectoryPath: newPath] and it fails to change the path. I tried setting to newPath to #"Users/username/Desktop" and that fails too!
The Library directory does exist but it is hidden as standard. Open up the terminal and type the command: chflags nohidden ~/Library/.
Then look again and it will be magically there!
Edit: for programming with NSFileManager there's a very useful function: NSSearchPathForDirectoriesInDomains(). E.g. To get the desktop do directory:
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:#"test.txt"];
NSString *test = #"hello";
[test writeToFile:path atomicallly:YES];
This will write a little txt file to your desktop. (As long as you app isn't sandboxed).
Hope this helps.
When you place a file in the project directory, it gets shipped as part of the app's bundle. To get at the bundle directory tree, use NSBundle...
// use the file extension (if any) for the ofType: param
NSString *path = [[NSBundle mainBundle] pathForResource:#"testfile" ofType:#""];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSLog(#"there it is! %#", path);
}
The following is my code and I have the newfile2 in the same directory as main.m is located(see image) but I am getting File read failed error. Where am I going wrong?
import
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSFileManager *fm;
NSData *fileData;
fm = [NSFileManager defaultManager];
//Read from the file nwefile2
fileData = [fm contentsAtPath:#"newfile2"];
if (fileData == nil) {
NSLog(#"File read failed");
return 1;
}
//Write the data to a file
if ([fm createFileAtPath:#"newfile3" contents:fileData attributes:nil] == NO) {
NSLog(#"File creation failed");
return 2;
}
NSLog(#"File copy successful");
}
return 0;
}
The problem is that this line doesn't say where to find the file:
fileData = [fm contentsAtPath:#"newfile2"];
You are assuming that a partial path will find the file (perhaps because you think the working directory is the same directory as your program?), but that is a false assumption. Nothing is known about what the working directory is! You have to be specific about where the file is.
The correct way to specify the path in-spite of hardcoding the path is use below api of NSBundle class:-
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
Write like that below:-
fileData = [fm contentsAtPath:[[NSBundle mainBundle]
pathForResource:#"newfile2" ofType:#"fileExt"]];
Note:- Before fetching the file path, your file should be present in the resource directory,.
I'd like to get all tracks from an album on iTunes using iTunes.h.
Right now I get the data of the current track by:
NSInteger trackID = iTunes.currentTrack.databaseID;
NSString *name = iTunes.currentTrack.name;
And the name of the album by:
NSString *trackAlbum = iTunes.currentTrack.album;
But know I don't know how to get all the tracks that are in the same album as the current track.
Any ideas? Thanks
The iTunes API is poorly written.
You have to filter the array with a predicate.
NSArray *allSongs = [self allSongs];
NSArray *songsOfAlbum = [allSongs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"album == %# && artist == %#", albumString, artistString]];
You can get all the songs like this:
// Get all Songs
- (NSArray *)allSongs {
if (_allSongs == nil) {
NSArray *tracksToPlay = [(SBElementArray *)[self.library tracks] get];
// Sort by artist
_allSongs = tracksToPlay;
}
return _allSongs;
}
- (iTunesLibraryPlaylist *)library {
if (_library == nil) {
// Whole Library
iTunesSource *source = [[[[self.iTunes sources] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"kind == %i", iTunesESrcLibrary]] objectAtIndex:0];
// Only the Music
_library = [[[[source playlists] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"specialKind == %i", iTunesESpKMusic]] objectAtIndex:0];
}
return _library;
}
You can use iTunes Library Framework (10.9 osx) for iTunes 11.
#import <iTunesLibrary/ITLibrary.h>
#import <iTunesLibrary/ITLibMediaItem.h>
#import <iTunesLibrary/ITLibAlbum.h>
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:#"1.0" error:&error];
if (library)
{
NSArray *tracks = library.allMediaItems; // <- NSArray of ITLibMediaItem
}
for (ITLibMediaItem *item in tracks) {
NSLog(#"tracks %#",item.album.title);
}
You can fetch album information from ITLibMediaItem
As an example to retrieve the artworks I have submitted a github repository:
It is an objective c code which uses the above code changed:
#import <Foundation/Foundation.h>
#import <iTunesLibrary/ITLibrary.h>
#import <iTunesLibrary/ITLibMediaItem.h>
#import <iTunesLibrary/ITLibArtwork.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:#"1.0" error:&error];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (library)
{
//NSArray *playlists = library.allPlaylists; // <- NSArray of ITLibPlaylist
NSArray *tracks = library.allMediaItems; // <- NSArray of ITLibMediaItem
[tracks enumerateObjectsUsingBlock:^(ITLibMediaItem* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.artworkAvailable) {
ITLibArtwork* artWork=obj.artwork;
//NSData* d=artWork.imageData;
switch (artWork.imageDataFormat) {
case ITLibArtworkFormatPNG:{
NSURL* fileURL=obj.location;
NSString* fs=fileURL.lastPathComponent;
NSString* path=[fileURL.path stringByReplacingOccurrencesOfString:fs withString:#"Folder.png"];
if (![fileManager fileExistsAtPath:path]){
[artWork.imageData writeToFile:path atomically:YES];
}
}
break;
case ITLibArtworkFormatJPEG:{
NSURL* fileURL=obj.location;
NSString* fs=fileURL.lastPathComponent;
NSString* path=[fileURL.path stringByReplacingOccurrencesOfString:fs withString:#"Folder.jpg"];
if (![fileManager fileExistsAtPath:path]){
[artWork.imageData writeToFile:path atomically:YES];
}
}
break;
default:
break;
}
}
}];
NSLog(#"End reached");
}
}
return 0;
}
https://github.com/ENees/iTunesGetImages
I have been trying to create a new file inside of my application support folder while using NSApplicationSupportDirectory; I can write a file to it, but I have been unable to create a folder inside of Application Support.
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the application support: (attempting to create the blasted directory inside of application support directory
NSString *fileName = [NSString stringWithFormat:#"%#/managersemail.txt",
applicationDirectory];
//create content - formats with the managersemail.txt location
NSString* content = [NSString stringWithFormat:#"%#",[nameField stringValue]];
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
NSDictionary* errorDict;
The code that I have listed above works great, except for the part about creating the folder in which I want to place the managersemail.txt. I tried to mimic the stringWithFormat that is listed in the NSString* content and then varying several ways however to no avail! Any thoughts?
NSAppleEventDescriptor* returnDescriptor = NULL;
Perhaps the solution provided on Cocoa with Love might be useful?
Excerpt:
- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory
inDomain:(NSSearchPathDomainMask)domainMask
appendPathComponent:(NSString *)appendComponent
error:(NSError **)errorOut
{
// Search for the path
NSArray* paths = NSSearchPathForDirectoriesInDomains(
searchPathDirectory,
domainMask,
YES);
if ([paths count] == 0)
{
// *** creation and return of error object omitted for space
return nil;
}
// Normally only need the first path
NSString *resolvedPath = [paths objectAtIndex:0];
if (appendComponent)
{
resolvedPath = [resolvedPath
stringByAppendingPathComponent:appendComponent];
}
// Check if the path exists
BOOL exists;
BOOL isDirectory;
exists = [self
fileExistsAtPath:resolvedPath
isDirectory:&isDirectory];
if (!exists || !isDirectory)
{
if (exists)
{
// *** creation and return of error object omitted for space
return nil;
}
// Create the path if it doesn't exist
NSError *error;
BOOL success = [self
createDirectoryAtPath:resolvedPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (!success)
{
if (errorOut)
{
*errorOut = error;
}
return nil;
}
}
if (errorOut)
{
*errorOut = nil;
}
return resolvedPath;
}
Maybe you can try using the NSFileManager to create the folder, then write the file into the folder.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *applicationSupport = [[NSString stringWithString:#"~/Library/Application Support/'YOUR APP'] stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath:applicationSupport] == NO)
[fileManager createDirectoryAtPath:applicationSupport withIntermediateDirectories:YES attributes:nil error:nil];
NSString *fileName = [NSString stringWithFormat:#"%#/managersemail.txt", applicationSupport];
NSString* content = [NSString stringWithFormat:#"%#",[nameField stringValue]];
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
So something like that should work. Feel free to leave comments to ask questions.