using SRCROOT in Xcode - objective-c

I am trying to use SRCROOT path in my project like so:
// Create NSTask to interact with console
NSTask *task;
task = [[NSTask alloc] init];
// Set path to script
NSString *launch_path = #"$SRCROOT/Scripts/my_script.rb";
NSLog(launch_path);
[task setLaunchPath:launch_path];
I end up with (in log):
$SRCROOT/Scripts/my_script.rb
I want to end up to a path inside my project's directory like this:
/Application/Scripts/my_script.rb
Can anyone help me understand what I'm doing wrong? Thanks in advance.
Additionally, I've tried the following without success:
..
// Set path to script
NSString *launch_path = [NSString stringWithFormat:#"%#/Scripts/auto_checkin.rb", "$SRCROOT"];
NSLog(launch_path);
..

$SRCROOT is meaningless for runtime environment.
If you want to access the document in bundle, make sure the my_script.rb is added in the "Copy Bundle Resources", then try this:
NSString * path = [[NSBundle mainBundle] bundlePath] ;
path = [path stringByAppendingPathComponent:#"my_script.rb"] ;
$SRCROOT is path to the project file(*.xcodeproj) that defines the target. link here
The bundle term here represent Application bundle, the application bundle stores everything that the application requires for successful operation. link here

Related

Can't get path of image in NSBundle?

I am trying to get the path of an image while UITesting. I've tried multiple ways as shown under here, but none are working?
Name of image file is: TestImage.fff
NSBundle* testBundle = [NSBundle bundleForClass: [self class]];
NSString* testImage = [testBundle pathForResource:#"TestImage.fff" ofType:#"fff"];
NSString* testImage2 = [testBundle pathForImageResource:#"TestImage"];
NSURL* testImage3 = [testBundle URLForResource:#"TestImage" withExtension:#"fff"];
Can someone tell me where I am going wrong?
Inside XCode, select your test target inside the target list. Then select the Build Phases tab and inside the Copy Bundle Resources phase, add the required resources.

How to create few apps in one bundle?

I have an application that runs as an agent and has icon in the top bar. It should be able to run another app with window and icon in the dock. Both should to share same core data. Is there way to do it? How to open one app from another? Thank you.
Create a new cocoa application target, then add Copy Files build phase that embeds your subproject target into main app bundle:
Launch your embedded binary with NSTask class with code like this:
NSString *executablesPath = [[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent];
NSBundle *subProjBundle = [NSBundle bundleWithPath:[executablesPath stringByAppendingPathComponent:#"subproject.app"]];
NSTask *subBinaryTask = [[NSTask alloc] init];
subBinaryTask.launchPath = [subProjBundle executablePath];
[subBinaryTask launch];

Program directory in Objective-C (OSX)

I'm developing an OSX-application and in it, I'd like to know what the current directory is (i.e. the directory which holds .app-file).
At the moment, I'm using the following code:
NSString *dir=[[NSFileManager defaultManager] currentDirectoryPath];
[[NSAlert alertWithMessageText:#"dir"
defaultButton:#"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:dir] runModal];
When running from Xcode (Run-button), this gives me the debug directory (which is what I'm looking for), but when double-clicking the app in Finder (so, in the debug directory), it's giving me / which puzzles me.
Why does this happen and how can I get the current directory reliably?
That is the bundle folder:
NSString *appPath = [[NSBundle mainBundle] bundlePath];
(reference).
When programming in Apple Swift you will get the application path with:
let pathtoapplication: String = NSBundle.mainBundle().bundlePath

NSFileManager works when built in Xcode as release, but not when ran as standalone OS X Cocoa app

I have the following function written to randomly pick a file from a directory. It works totally fine when I build the project in Xcode for release with the application that automatically opens. However, if I open the application from finder, pressing the button that triggers this function will cause my program to freeze then crash. The only thing I could think of was changing the argument to contentsOfDirectoryAtPath: to not have the ./, but either version has the same exact issue.
Looking at Console tells me that my program exited abnormally with a Floating Point Exception, but I have no idea what's causing it. Is there something jumping out to you guys that I'm not seeing? I only started learning/using objective-C and cocoa about a week ago, so this is all fairly new to me.
Thanks for taking a look at this...
- (NSMutableString*)setFilePathRandom{
NSArray* files;
NSFileManager* fileManager;
fileManager = [[NSFileManager alloc] init];
files = [fileManager contentsOfDirectoryAtPath:#"./Random Trippy Pics" error:NULL];
NSString* directoryPath = (NSMutableString*)[fileManager currentDirectoryPath];
NSString* fileName;
do{
fileName = [files objectAtIndex:(arc4random()%[files count])];
}while([fileName isEqualToString:#".DS_Store"]);
filePath = [NSString stringWithFormat:#"%#/Random Trippy Pics/%#",directoryPath,fileName];
[fileManager release];
return filePath;
}
When an OS X application is run from Xcode, its current directory is the path to the build folder. When run "normally", the current directory is /. So your program is looking for a directory at /Random Trippy Pics, which almost certainly doesn't exist. Where is that directory normally?
Edit:
You could get the directory in which the application is currently stored with this bit of code:
NSString *currentStoragePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
However, if the Random Trippy Pics directory is required by the application, you should store it in a known location -- preferably the application's Resource directory. Then you can get the contents with:
NSArray *files = [[NSBundle mainBundle] pathsForResourceOfType:nil inDirectory:#"Random Trippy Pics"];

Getting Current Directory in Objective C

I am trying to get current directory, but its giving me path of DEBUG folder, how i can get the path of current directory. I am using the following code.
NSFileManager *filemgr;
NSString *currentpath;
filemgr = [[NSFileManager alloc] init];
currentpath = [filemgr currentDirectoryPath];
currentDirectoryPath returns the current working directory of the program. If you launch it from Xcode in Debug mode, the current directory of the program is the Debug directory.
Developing further on that. The current working directory and launch directory may be different! Take this example, the app is launched from the home directory (~).
Library/Developer/Xcode/DerivedData/MyApp-ehwczslxjxsxiob/Build/Products/Debug/MyApp.app/Contents/MacOS/MyApp -param value
NSString *p1 = [[NSFileManager defaultManager] currentDirectoryPath];
// p1 = ~/Library/Containers/com.MyApp.beta/Data
NSString *p2 = [[NSProcessInfo processInfo] environment][#"PWD"];
// p2 = ~
Depending on the situation you might want one or the other. In my case I want to provide a CLI which takes path arguments that may be relative to the current working directory in the shell.
When getting paths for a service loaded via launchd, the paths can be very different.
NSLog(#"argv: %s", argv[0]);
NSLog(#"NSProcessInfo: %#", NSProcessInfo.processInfo.arguments[0]);
NSLog(#"currentDirectoryPath: %#", [[NSFileManager defaultManager] currentDirectoryPath]);
NSLog(#"PWD: %#", [[NSProcessInfo processInfo] environment][#"PWD"]);
Output:
argv: /Users/dev/.ioi/default-runtime-manager/default-runtime-manager
NSProcessInfo: /Users/dev/.ioi/default-runtime-manager/default-runtime-manager
currentDirectoryPath: /Users/dev/Library/Application Support/IOI/default-runtime-manager
PWD: (null)
NSProcessInfo gives the location where the executable was found.
currentDirectoryPath returns the WorkingDirectory specified in the launchd plist.
The answer to getting the executable path was found here: StackOverflow: NSProcessInfo. The other paths were from: #lupdidup