Get directory path from full filepath - objective-c

New to objective-c. How do I get the directory from a full file path?
so if I have
/User/Test/Desktop/test.txt
I want
/User/Test
Also is there an equivalent to Path.Combine from .NET in Objective-C?

-[NSString pathComponents] returns an NSArray of path components.
For instance
NSString *path = #"/User/Test/Desktop/test.txt";
NSArray *components = [path pathComponents];
NSLog(#"%#", components); //=> ( /, User, Test, Desktop, test.txt )
Then you can take only the components you need and build a new path with them, for instance
NSString *newPath =
[NSString pathWithComponents:
[components subarrayWithRange:(NSRange){ 0, components.count - 2}]];
NSLog(#"%#", newPath); // => /User/Test/

Read the Working with Paths section of Apple's NSString documentation, you will find methods that answer your questions. For example, stringByDeletingLastPathComponent removes the last component of a path leaving the containing directory.

Related

Can't read text file from desktop using objective-c

Can anyone help me to find what's wrong with my code while reading file from desktop
NSString *filename=#"~/Users/user12345/Desktop/Sample/Data.txt";
NSString *fileString=[NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:nil];
NSArray *namesArray=[fileString componentsSeparatedByString:#"\n"];
for(NSString *names in namesArray)
{
NSLog(#"names:%#",names);
}
If you want to use the tilde – which represents /User/<currentUser>/ – you have to ...expandingTildeInPath and remove /Users/user12345
NSString *filename = [#"~/Desktop/Sample/Data.txt" stringByExpandingTildeInPath];
that makes the path independent of the current user name, otherwise remove the tilde:
NSString *filename = #"/Users/user12345/Desktop/Sample/Data.txt";
Caveat: If your app is sandboxed the path does not point to the visible desktop.
NSString *filename=#"~/Users/user12345/Desktop/Sample/Data.txt";
The ~ used in a path is a convention which means your home directory, but it doesn't work in all contexts (e.g. when used in -stringWithContentsOfFile:`) and you've supplied an absolute path anyway. Remove it from the front of your path
NSString *filename=#"/Users/user12345/Desktop/Sample/Data.txt";
and it should work as long as the file does actually exist at that path.

Why my program can run in Xcode, but cannot running as a separate app?

My program loads some data from a file and then draws them.
The file-reading part is like this:
- (void)load_file
{
NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:#"map_data"];
NSData *myData=[inFile readDataToEndOfFile];
NSString *myText=[[NSString alloc]initWithData:myData encoding:NSASCIIStringEncoding];
NSArray *values = [myText componentsSeparatedByString:#"\n"];
for (NSString *string in values) {
NSArray *lines=[string componentsSeparatedByString:#" "];
if ([lines count] != 2) break;
NSPoint point= NSMakePoint([lines[0] floatValue], [lines[1] floatValue]);
[points addObject:[NSValue valueWithPoint:point]];
}
[self setNeedsDisplay:YES];
}
When debugging, I put the data file in the directory of [NSBundle mainBundle], and the program works fine.
However, when I use achieve to take the app out, it never runs. I put the data file in the same path with the app, but it seems fail to load it.
Update
I tried to use c++, but still fails.
- (void)load_file
{
ifstream inf("map_data");
double x, y;
while (inf >> x >> y) [points addObject:[NSValue valueWithPoint:NSMakePoint(x, y)]];
inf.close();
}
I tried to change the build scheme to release and run, which is fine. But whenever I go directly into the finder of app and double click it, it does not work and seems nothing is loaded.
add the file to the project as a Resource (this will cause it to be copied into the app wrapper in the right spot)
use `[[NSBundle mainBundle] pathForResource:#"map_data" ofType:nil];
That should give you the path to the file. The file should not be manually copied, it should not be next to the app wrapper, nor should you [conjecture] ever try changing or replacing the file once it is in your app wrapper.
The reason why it seems to work sometimes is mere coincidence. You are passing a partial path to NSFileHandle and it happens that the current working directory of your app sometimes points to the right spot such that the data file is available.
I'm not sure how relative paths are handled by NSFileHandle, but usually you set up paths using the NSBundle class.
NSString *path = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"ext"];
You can also simply initialize an NSString from the contents of a file, you don't need to first read it into an NSData using NSFileHandle.
NSString *text = [[NSString alloc] initWithContentsOfFile:path
encoding:NSASCIIStringEncoding error:nil];
(Use the error parameter, if you want proper error handling)

How do I copy a directory that is inside of my main bundle to an NSPasteBoard?

I have been tearing my hair trying different combinations of file paths and different types but basically what I am trying to do is copy a folder called "test" that is inside of my resources folder.
Copying folders onto the NSPasteBoard works when I give an absolute path (ex: /Users/dw/src/Menulet) but it doesn't work when I try using my mainBundle's resource path.
Here is the code I currently have:
NSString *resourceFolder = [[NSURL fileURLWithPath:[[NSBundle mainBundle]
resourcePath]] absoluteString];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:nil];
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
NSURL *SDKPathURL = [[NSURL alloc] initWithString:SDKPathString];
[pboard writeObjects:[NSArray arrayWithObject:SDKPathURL]];
The result is that it can't find the file:
__CFPasteboardIssueSandboxExtensionForPath: error for [/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/Menulet.app/Contents/Resources/test]
How can I copy the directory?
Edit:
My guess is that you're missing a '/' character when you do this:
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
Indeed resourceFolder is a path, I usually use something like this instead:
NSString* SDKPathString= [resourceFolder stringByAppendingPathComponent: #"test"];
Even if in your case you could simply correct the error by putting a '/' character before "test".But I think this is more mnemonic.
Update
Isn't said that if you create a group Xcode also creates a folder. If you want to be sure that Xcode does so, create a folder with finder and drag it to the project. Check these options:
This way the folder is actually created, and also added to the bundle.
Update 2
You shouldn't try to move your directory inside the project folder to put into the right place, instead put it wherever you want in the bundle, provided that the directory is copied in the bundle.
Once did so, the bundle will manage everything for you. So just search the directory using:
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension;
So in your case:
NSBundle* bundle= [NSBundle mainBundle];
NSString *SDKPathString = [ bundle pathForResource: #"test" ofType: #""];

LSCopyApplicationURLsForURL always returns null

When I try to log all available editors on my system for my temporary file (which is "toString" in this code) it always returns null, although I have many applications installed on my system.
NSArray *appUrls = (NSArray*)LSCopyApplicationURLsForURL((CFURLRef)[NSURL URLWithString:toString], kLSRolesViewer | kLSRolesEditor);
toString is containing the following file path:
/var/folders/pl/tcc5k3fd6tj2__9dprg9dm1m0000gp/T/tempFile
What should be the problem here?
[NSURL URLWithString:toString]
expects a complete URL string including scheme, such as "file://var/folders/...".
Use
[NSURL fileURLWithPath:toString]
instead to get a file URL with the specified path.
Another problem could be that your file name does not have any file extension (e.g. ".txt"), because Launch Services uses the extension (or file type/creator) to find a suitable application.
I was struggling with the this and I wanted to get all Bundles that could open a determined path/file extension.
If you have a file extension, you can get all bundles that can edit it by the following:
//All Bundle Ids
NSString *pathExtension = #"docx";
CFArrayRef utisRef = UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension,(__bridge CFStringRef) pathExtension,nil);
NSLog( #"UTI: utisRef %#", utisRef);
NSArray *utis = CFBridgingRelease(utisRef);
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
for (NSString *uti in utis) {
CFArrayRef bundleIDsRef = LSCopyAllRoleHandlersForContentType((__bridge CFStringRef) uti,kLSRolesEditor);
[mutableSet addObjectsFromArray:CFBridgingRelease(bundleIDsRef)];
}
NSLog( #"bundleIDs: %#", mutableSet);
If you have a path of file and you want to get all apps location that can edit it, you can use the following:
//Location of apps
NSString *str = #"/Users/ricardoanjos/Library/Developer/Xcode/DerivedData/EgnyteDrive-hforbniifiojczefbnwanzxakvlr/Build/Products/Debug/1.pdf";
NSURL* url = [[NSURL alloc] initFileURLWithPath:str];
CFURLRef urlRef = (__bridge CFURLRef)url;
CFArrayRef appUrlsRef = LSCopyApplicationURLsForURL(urlRef, kLSRolesEditor);
NSArray *appUrls = CFBridgingRelease(appUrlsRef);
NSLog(#"appUrls: %#", appUrls);
I hope it will help.

Reading text file with NSString:stringWithContentsOfFile?

Reading text file with NSString:stringWithContentsOfFile?
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:#"\help" ofType:#"txt"];
NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"File: %#",txtFileContents);
I get "null" as the result. How do I know what path to specify?
thanks
(I placed the file just under "Groups and Files" ... so not sure if I need to specify a path or just the file name. Maybe there is something else wrong ???
I think the backslash in your path is confusing things. Try this:
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:#"/help" ofType:#"txt"];
Also, as noted in the comments, you need to use the Build Phase of your project to copy the "help.txt" file.
Try this. What you want is the path to help.txt but you have to split it up into its name and extension for the method to work.
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource: #"help" ofType: #"txt"];
It also wouldn't hurt to specify NULL instead of nil for the error argument. This is because nil represents objects whereas NULL represents pointers.
Here's a common file that I include in a ton of my projects (.h first, followed by .m):
I name this one FileHelpers.h:
#import <UIKit/UIKit.h>
NSString *pathInDocumentDirectory(NSString *fileName);
I name this one (of course) FileHelpers.m:
#include "FileHelpers.h"
NSString *pathInDocumentDirectory(NSString *fileName)
{
// Get list of document directories in sandbox
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get one and only one document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
// Append passed in file name to that directory, return it
return [documentDirectory stringByAppendingPathComponent:fileName];
} // pathInDocumentDirectory
As an aside, I have to admit that I didn't come up with this solution myself, but it's been so long that I've used it that I can't remember now where I got it. If anyone knows, please feel free to attribute the appropriate credit!