is it possible to locate file on Mac without knowing exact location? - objective-c

I am trying to locate a file on Machintosh without knowing exact file location. I am developing an app which needs to locate iTunes library XML file and parse it for data. However XML resides in,
/Users/**Current logged in username**/Music/iTunes
Now username may change, So how to set path of let my code find below named file programmatically. Language used is Objective - C.
File name : iTunes Music Library.xml
Possible solution I thought is to get username by code. Is there any method which returns a string of current logged in user's "username".
Thanks

You should be able to use ~/Music/iTunes. ~ represents the home directory of the current user.
Or you could could see this question: Mac OS X: Get current username and home directory for current user from Directory Services

Don't do this! The user's iTunes library doesn't have to be in the default location.
Instead, read the iTunesRecentDatabasePaths key from com.apple.iapps. It'll give you an array that represents the recently used iTunes libraries.
$ defaults read com.apple.iapps iTunesRecentDatabasePaths
(
"/Volumes/Media/iTunes/iTunes Library.xml"
)

NSHomeDirectory() is a Foundation function that returns an NSString of the path to the current user's home directory.
Apple Developer Library - NSHomeDirectory

Related

create a directory to save recorded files on iphone using objective c

I am letting user record audio from mic to .acc file
at the moment they are saved to default app document folder
in plist i enabled "supports opening documents in place"
Now in iPhones File App i can see my apps folder in Documents and also see and listen to the recorded .acc file
great... but...
Now the user can also see and then edit / mess with ini files and other files i dont really want them o see or edit etc...
So now this isnt a good solution.
So where do i save recorded audio so that users can still see them, find them on their phone, listen and share them with friends (without being to see the apps other files)?
For any files that don't need to change, just keep them as resources in your app bundle and load them from there at runtime.
For files that are generated at runtime (not at build-time) or config files that change, which you don't want to be exposed, put them in Application Support instead of in Documents.
In Objective-C:
NSArray * appSupportURLs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
In Swift:
let appSupportURLs = FileManager.default.urls(for: .applicationSupportDirectory,
in: .userDomainMask)
This Apple Developer video pretty much covers it: https://developer.apple.com/videos/play/tech-talks/204/

Does the "/Volumes" name changes based on system language, if yes how to adapt this?

I am using this code to check if the client runs from DMG /Volume, and show an alert and quit.
private void IsRunningFromDMG()
{
var currentPath = NSBundle.MainBundle.BundlePath;
if (currentPath.StartsWith("/Volumes", StringComparison.Ordinal))
{
using (var alert = new NSAlert
{
MessageText = "Warning",
InformativeText = "You cannot run this application from DMG, drag move to the " +
"\"/Applications\" folder"
})
{
alert.RunModal();
NSApplication.SharedApplication.Terminate(null);
}
}
}
This is fine if the system language is set to English. I doubt if the /Volumes reacts to localization in that case how to do it?
There should be a system defined constant that would take care of it?
If it is, please help me to know what is it.
Filesystem paths do not change based on localization, they are localized for "display" though (i.e. Pictures, Music, etc... would be localized in Finder for display to the user but the filesystem path is still /User/xxx/Pictures, etc...)
Localized names. The system provides localized names for many system directories, such as Applications, Library, Music, Movies. An app may similarly provide localized names for itself and for any directories it creates.
Display names do not affect the actual name of the file in the file system. Code that accesses a file or directory programmatically must specify the item’s actual name when opening or manipulating the item using the file system interfaces. The only time your app should ever use display names is when displaying the name of a file or directory to the user. You can get the display name for any file or directory using the displayNameAtPath: method of NSFileManager.
i.e.
var urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.ApplicationsDirectory, NSSearchPathDomain.System);
var applicationDisplayName = NSFileManager.DefaultManager.DisplayName(urls[0].AbsoluteString);
Note: You should use NSSearchPathDirectory.AllApplicationsDirectory within the NSSearchPathDomain.All domain to obtain a valid list of "Application" urls where your application might/should be located at, instead of hard coding it to the "System" application location.

The location of .config for geotiler on Windows

I'm working in jupyter notebook on Windows with matplotlib basemaps and I want to use geotiler with the basemaps. I'm writing a program and as part of it, it will generate a map and plot data points on it. However, the maps that my code generates often are over a small part of the world and have no defining features. My solution was to import the geotiler library and display it over the map with an alpha so the maps generated would be identifiable. However, when I use the geotiler.Map() function, I get a message saying that the configuration file does not exist.
The code and the error message
How do I locate the .config folder on Windows, if it exists, and where should I create it if it doesn't? I already tried my user folder but that didn't seem to work. Thanks in advance.
Figured it out.
The read_config() method in the source code tries to get the HOME environment variable, which for Windows is blank, and appends the path for the config to that. Importing os and manually setting the HOME variable to wherever you placed your .config folder seems to do the trick. You can do this with os.environ['HOME'] = 'C:\Users\YourName'.

How to build with a custom eglfs cursor atlas?

I'm trying to change the eglfs mouse cursor graphics for my embedded linux QT application (QT5.5). I have the new cursor atlas PNG and the new JSON descriptor file, but the documentation is rather vague:
".. a custom cursor atlas can be provided by setting the QT_QPA_EGLFS_CURSOR environment variable to the name of a JSON file. The file can also be embedded into the application via Qt's resource system."
I'd prefer to keep everything within the resource system if possible but I can't work out how to do it.. do I need a specific qrc file containing the path to the JSON file? I assume that the PNG file would also need to be added as a resource so that it gets built into the application?
If adding it via the resource system is a bad idea where's the correct place to set the QT_QPA_EGLFS_CURSOR environment variable? I'm currently specifying the platform on the command line via "-platform eglfs"; will this be ok or will I need to set the platform to eglfs in the build?
After much trial, error and digging around I have found the solution that I was looking for within the resource system.
Create a new resource file called "cursor.qrc", the contents of which needs to be two lines:
path/to/your/custom-cursor-atlas.png
cursor.json
The first line (path to your cursor atlas) must be relative to your resource directory.
You then need to put the JSON file (contents as described in the documentation) in the root of your resource directory. It must be called "cursor.json", and its image location line must must match the location in your new resource file and be of the format:
"image": ":/path/to/your/custom-cursor-atlas.png",
This will then include your cursor atlas in resources, and Qt will find it when your application starts.
Run time solution example:
export XDG_RUNTIME_DIR=~
export QT_QPA_EGLFS_CURSOR=~/cursor.json
In the cursor.json:
"image": "cursor.png",
Put your custom cursor.png atlas into your home dir (~) then run the Qt app from there.

How to get current directory in Appcelerator Titanium?

Assuming that my JS file is in /libs/qrcode/qrcode.js and i have another file qrcode.html at /libs/qrcode/, is it possible to get current directory/path (i.e. /libs/qrcode/) inside the qrcode.js?
what i'm trying to do is to load qrcode.html in the webview inside qrcode.js without worrying where is the qrcode folder reside in the Resource directory.
Any help?
Try Ti.Filesystem.resourcesDirectory or Ti.includeAbsolute
This should give you the path from your Resources folder or take a look at the Titanium.Filesystem.File