Find path to running app using [NSBundle bundlePath]

This commit is contained in:
Max Brunsfeld 2022-04-04 20:53:40 -07:00
parent fb2caf3c58
commit e566a8335f
4 changed files with 25 additions and 5 deletions

View file

@ -615,15 +615,23 @@ impl platform::Platform for MacPlatform {
if path.is_null() {
Err(anyhow!("resource could not be found"))
} else {
let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
let bytes = path.UTF8String() as *const u8;
let path = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap();
Ok(PathBuf::from(path))
Ok(path_from_objc(path))
}
}
}
}
fn app_path(&self) -> Result<PathBuf> {
unsafe {
let bundle: id = NSBundle::mainBundle();
if bundle.is_null() {
Err(anyhow!("app is not running inside a bundle"))
} else {
Ok(path_from_objc(msg_send![bundle, bundlePath]))
}
}
}
fn app_version(&self) -> Result<platform::AppVersion> {
unsafe {
let bundle: id = NSBundle::mainBundle();
@ -641,6 +649,13 @@ impl platform::Platform for MacPlatform {
}
}
unsafe fn path_from_objc(path: id) -> PathBuf {
let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
let bytes = path.UTF8String() as *const u8;
let path = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap();
PathBuf::from(path)
}
unsafe fn get_foreground_platform(object: &mut Object) -> &MacForegroundPlatform {
let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR);
assert!(!platform_ptr.is_null());