Get Application Directory on MacOS return path with appName.app/Contents/MacOS - macos

I am building a cross-platform app that creates a file for the user.
To get the file's full path I wrote the following code:
QString fullPath = QCoreApplication::applicationDirPath();
fullPath.append("/").append(filename);
After searching for the file, I found in the debugger that my file exists under <myAppName>.app/Contents/MacOS.
The user does not have a way to get those file (except via the terminal).
The question: What should I do to receive "normal" path ?
I do not want use substring functions because same code should work well on Linux and Windows.
Thanks in advance.

I finally asked the question in Qt forum here and the answer I got is:
I cannot create files there (app directory) because normal users don't have permissions for that. QStandardPaths can be used as a solutions for the above issue.

Related

Is there a way to find out the location that the executable file is being run from during runtime?

I am attempting to create a program that places a copy of itself into the windows start-up folder. if the program is being run from anywhere that isn't the start-up folder, I would like the program to add the users username to a text file where the original .exe is. hence I need a way to tell where the program is being run from. is there any inbuilt functions in the default units or will I have to write my own function? if so how would I accomplish this?
ParamStr(0) will get you the full path and application name.
Disclaimer: This could be invalid for some OS implementations.

Accessing an external file from MacRuby

I have done several small scripting/automation projects before but this is my first time using MacRuby and I couldn't find out at all why my project is not working.
This task would have been easy to fix if it was just a plain Ruby script. However, it needs a Mac GUI so I decided to use MacRuby for its Cocoa bindings.
The app itself is a simple form that will perform a calculation based from some data from an external CSV and some text fields and then show the results of the calculation.
My problem is that this code does NOT seem to work:
#arr_from_csv = CSV.read("data.csv")
Upon building the file, I get the following error:
[...]/ruby/1.9.2/CSV.rb:1335:in `open': No such file or directory - open() failed (Errno::ENOENT)
At first, I thought that I must have put the CSV file into the wrong directory inside XCode project's folder structure. I tried putting on the same folder as the script itself (app_delegate.rb). Didn't work. I tried putting it in the Resources folder, still didn't work.
Then, I decided to just use an absolute file path. So, I changed to code into:
#arr_from_csv = CSV.read("~/data.csv")
and copied the file into my home directory. It still can't read the CSV file.
Am I missing something? I'm sorry, this is my first time using MacRuby.
Thanks.
You should change the code to "/Users/xxx/data.csv".
Ruby does not expand "~" to "/Users/xxx".
Notice: xxx represents your login id.
I was able to solve my problem by using:
#arr_from_csv = CSV.read(File.expand_path("~/data.csv"))

Application permission issues on OS X, when distributing application

we are making an Application for OS X, however, when the .app is copied on another MAC, we have problems with reading and writing files
on one MAC, everything works great from the start...
the other one will not write certain files and another 2mac will not write certain different files
if i go to show application contents and wants to edit the file by myself, i get a writing permission denied
how to distribute an MAC application so there are no such issues? so all files can be read and write by the current user
does there have to be some authorization or code sign, or smth. different in this form, in addition to normal code?
thank you
It sounds like your application tries to write files within its own application bundle. The correct solution here is: DON'T DO THAT! The only time your application bundle should be written to is when it's installed or updated.
Files that the application needs to write to should be stored in the user's home folder, generally under ~/Library. See this note in Apple's dev docs.
Preference and settings files in ~/Library/Preferences/<appbundleid>.plist; use NSUserDefaults.
Data the app manages for the user in ~/Library/Application Support/<appname> (the docs say to use the bundleid, but everyone -- Apple included -- uses the app name instead).
Cache files in ~/Library/Caches/<appbundleid>.
Temp files, use NSTemporaryDirectory
If you need to share settings & files between users, that should generally go in /Library/Application Support/<appname>, except that you really shouldn't be doing that at all.
In the past I've always used PackageMaker to create installers. An installation package can authenticate with root privileges so you can set permissions after the install. I don't have it in front of me right now but if you look around you should be able to see a Post-Installer script line. Write a shell script that manually sets the permissions of each file you have in question and then have the package execute that script after the install is finished.
You can find PackageMaker at /Developer/Applications/Utilities/PackageMaker

MacOSX open file with an application: where does it go to?

I posted this basic question before, but didn't get an answer I could work with.
I've been writing applications on my Mac, and have been physically making them into .app bundles
(i.e., making the directories and plist files by hand). But when I open a file in the application by right clicking on the file in finder and specifying my app, how do I then reference that file?
I mostly use python, but I'm looking for a way that is fairly universal.
My first guess was as an argument, as were the answers to my previous post, but that is not the case.
Py:
>>> print(sys.argv[1:])
'-psn_0_#######'
Where is the file reference?
Thanks in advance,
The file is passed by the Apple Event, see this Apple document. You need to receive that from inside your Python script. If it's a PyObjC script, there should be a standard way to translate what's explained in that Apple document in Objective-C to Python.
If your script is not a GUI app, but if you just want to pass a file to a Python script by clicking it, the easiest way would be to use Automator. There's an action called "Run Shell Script", to which you can specify the interpreter and the code. You can choose whether you receive the file names via stdin or the arguments. Automator packages the script into the app for you.
This is not an answer but it wouldn't fit in the comments. To respond to #Sacrilicious and to give everyone else insight on this:
#Sacrilicious You're talking about something different. Download this sample application, it's a python script wrapped as an "App". Look inside and find a 4-line python script: myscript.app/Contents/MacOS/myscript - which will print the arguments using
file = open("/tmp/test.txt", "w")
file.writelines(sys.argv[1:])
Stick it in your Applications folder. Then right click some file and choose "Open With" and select this myscript.app.
Now take a look at /tmp/text.txt and you'll see that something like -psn_0_####### is there and not the name of the file you had selected "open with". This is because the file is passed using Apple Events and not a filename as an argument.
So this question is asking how can you access the filename of the thing that was passed in the python script wrapped in an OS X .app application wrapper, and if someone can let me know that they'll get the Bounty :)
Are we referring to the file where per-user binding of file types/extensions are set to point to certain applications?
~/Library/Preferences/com.apple.LaunchServices.plist
The framework is launchservices, which had received a good amount of scrutiny due to 'murkiness' early in 10.6, and (like all property list files) can be altered via the bridges to ObjectiveC made for Python and Ruby. Here's a link with Python code examples for how to associate a given file type with an app.
I've never heard of it being done without a Cocoa / Carbon wrapper.
I described how to link certain filetypes to py2app-bundled Python applications at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html

GetOpenFilename api call in Windows 7 won't allow direct access to My Documents

In many of my Access (2002) programs I use the GetOpenFileNameA and GetSaveFileNameA functions from comdlg32.dll. I often set the initial directory to the user's My Documents folder (using calls to SHGetSpecialFolderLocation and SHGetPathFromIDListA from shell32). This all works fine under Windows XP.
However, I recently switched to Windows 7 as my development environment and have been getting the following error message:
You can’t open this location using
this program. Please try a different
location.
The function I use to get the My Documents location is returning the correct folder. However, even if I hard code that directory location into the GetOpenFileNameA call, I still get the error.
I came across this post: http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/thread/3391f1dd-25b0-4102-9d5c-58309cc72c9d but, even adapting it to work with Access instead of Excel, I had no luck.
EDIT: Suddenly this is no longer a problem for me. I suspect a windows update went out addressing this issue. Does anyone know if that's true or not?
EDIT: It turns out this is still a problem. Also, in case it helps in the troubleshooting I have found that I get this error message for any of the special folder locations (My Music, My Documents, etc). Also, if I change the location of the My Music folder to, say, C:\Test then I get this message when I try to open the folder C:\Test, while the folder C:\Users\Mike\Music (the original My Music location) opens without a hitch.
Windows 7 adds the concept of a "library", which is basically a virtual folder that includes the contents of at least two actual subdirectories. One place it uses the library is the "My Documents" folder, which (at least by default) is a library that includes both the user's documents directory ("c:\users\whoever\documents") and the public documents directory (C:\users\public\documents").
As such, the basic approach you're using simply can't work -- there is no path that denotes the Documents folder. The documents folder needs to be specified by a PIDL, not a path.
Edit: It's not clear what's going on if you can't open C:\users\user\Documents. A quick test in C++ works fine using code like:
OPENFILENAME data = {0};
wchar_t filename[256] = {0};
data.lpstrInitialDir = L"C:\\users\\jerry\\documents";
data.lStructSize = sizeof(data);
data.lpstrFile = filename;
data.nMaxFile = sizeof(filename);
GetOpenFileName(&data);
OTOH, there's no real need to specify the initial path -- the Documents folder is the default anyway.
The link I posted in my original question (http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/thread/3391f1dd-25b0-4102-9d5c-58309cc72c9d) held the answer after all. I'll summarize things here. To fix this behavior, you need to do away with the STRIPFOLDERBIT flag in the shell compatibility registry entry for all affected programs.
Keep in mind (and this is what tripped me up for so long) that 32-bit programs have entries in a special registry section if you have 64-bit windows. Here's the quick and dirty:
Rename STRIPFOLDERBIT to xSTRIPFOLDERBIT for the following keys (as applicable):
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\excel.exe
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\msaccess.exe
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\excel.exe
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\msaccess.exe

Resources