Set environment variable for the process before startup - macos

I have the following situation:
I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment variable, let's name it ENV_VAR. I want to set ENV_VAR to some value for my application only because if I set it for the whole system it may breaks some other apps. And it should work transparently to the user i.e. he just run my app from the Application folder by double clicking it. How can I achieve it?
NOTE: dynamic libraries are loaded before main functions starts hence setting this variable in the main doesn't help.

You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).
<key>LSEnvironment</key>
<dict>
<key>ENV_VAR</key>
<string>value</string>
</dict>
However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed
This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.

I am not sure if the following works because I don't have such an app to try. The idea is to set the environment variable from the terminal, then call your application:
ENV_VAR=something open -a YourApplication

Related

How is "Open with" on macOS populated internally?

I know I can associate my application with a certain extension by editing the Info.plist file in my app bundle:
...
<key>CFBundleTypeExtensions</key>
<array>
<string>myext</string>
</array>
While this works, my "Open with" list gets littered with duplicate entries of the different versions of the application I'm developing on that machine. There are even entries for really old versions that I've never opened on that very computer! (It's a new machine being synced with Dropbox so there are older releases in my file system.)
It seems as macOS would scan for app bundles even outside of /Application and adds all applications it finds to "Open with". A workaround is to reset the "Open with" list with this command I found somewhere here on SO I think:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user;killall Finder
How is the "Open with" list populated internally? Is macOS really scanning my drive looking for Info.plist files with CFBundleTypeExtensions entries? And is there any way to avoid having my "Open with" list littered with older versions other then deleting (or zipping) them?
NB: I'm not using Xcode but build my application on the command line using the clang compilers and creating Info.plist by a bash script.
How is the "Open with" list populated internally? Is macOS really
scanning my drive looking for Info.plist files with
CFBundleTypeExtensions entries?
I think so, yes. Check the documentation for Launch Services, specifically the section "Application Registration". Specifically, the OS can find your app and register is in the following ways:
A built-in background tool, run whenever the system is booted or a new
user logs in, automatically searches the Applications folders in the
system, network, local, and user domains and registers any new
applications it finds there. (This operation is analogous to
“rebuilding the desktop” in earlier versions of Mac OS.)
The Finder
automatically registers all applications as it becomes aware of them,
such as when they are dragged onto the user’s disk or when the user
navigates to a folder containing them.
When the user attempts to open
a document for which no preferred application can be found in the
Launch Services database, the Finder presents a dialog asking the user
to select an application with which to open the document. It then
registers that application before launching it.
And is there any way to avoid having my "Open with" list littered with older versions other then deleting (or zipping) them?
I'm not sure about that part. Probably not, although it seems like once you reset the Launch Services database using the command in your question, the old versions shouldn't get added back to the Open With list unless you are doing something to trigger getting re-added. Is that the case?

How to clear UserDefaults for Xcode build of Mac app?

I'm trying to make sure my first-run code works properly, and so I'd like to clear the preferences file created by UserDefaults.standard.set calls. Where is it located, or is there at least a way to clear it out (other than writing it into my app)?
Running Product > Clean does not clear out the defaults.
I've looked in DerivedData, ~/Library/Preferences, /Library/Preferences, and haven't found what I'm looking for.
If the app is sandboxed the preferences are located in
~/Library/Containers/[bundle-identifier]/Data/Library/Preferences
If it's not sandboxed the preferences are at the usual location
~/Library/Preferences
You can use defaults command In Terminal
$ defaults delete com.bundle.identifier
Also you can delete any value in defaults by key, if you don't want to delete whole application defaults plist.
$ defaults delete com.bundle.identifier kSomeKey

Resetting mac application in xcode

How can I reset a cocoa application in Xcode? When I run my application from Xcode all user default are saved. I would like to reset (delete) my application to create a new one with new settings and run as it would be run first time.
I found one way - I change a name of my app to a different one and it is started as new. But how can I do this with an old name?
You can use the defaults command line tool to remove all the settings, you just need the bundle id of your app (which you can find in your Info.plist file), e.g. if your bundle id is "com.foo.barApp" then you can run this from a terminal shell:
defaults delete com.foo.barApp
Is your app limited to the App Store? If so, I don't think there's a way to delete your entire application, specifically because of the sandbox restrictions. We'd need more information on how you are planning to distribute it to help.
If you just want to delete your app settings, then clear whatever database you store your app data in - [NSUserDefaults +resetStandardUserDefaults], SQLite, App Library directory, whatever. That is dependent on how/where you are storing user data, there's no one size fits all solution.

overriding Mac app file associations via CFBundleDocumentTypes in info.plist

I develop a Mac app that saves and loads files of a unique type. The type is properly declared in the info.plist under CFBundleDocumentTypes, listed as LSHandlerRank: Owner and CFBundleTypeRole: Editor. I am releasing a new version of my app and I would like that if users who already have a previous version of my app on their machine install the new version but keep the old version also installed, the new version automatically takes over the file association for this type from the old version of the app. But the default OS behavior seems to be to grant the earliest installed app associated with a file type to be it's permanent owner unless the user manually changes it. I know that the command line tool duti can make association changes, and also the system file com.apple.LaunchServices.plist can be edited, but these don't seem like the best or most reliable way to go about this programmatically from an app. Is there any "right"/Apple-sanctioned way to do what I want?
The official, Apple-sanctioned way for an application to set the default application for a file type is probably to use their provided LSSetDefaultRoleHandlerForContentType function. This function sets the values stored in LaunchServices.
Here is the limited official documentation on how to use this API in Objective-C and Swift.
There doesn't appear to be any way to do this via Info.plist, as the first application get's set as the user preferred application. The user must change their preferred application, which you can facilitate with the above API.

How to remove the dock icon of a shell executable?

I have a java application built with Eclipse, for Mac OS X.
This app is installed via a .pkg file and supposed to be daemonized. Everything works fine, with Macbooks (Pro, Retina), but with the iMac I work on, when the application launches, i have an icon on the Dock, as you can see on the
following link.
I have already tried the solution in this topic: stackoverflow.com/a/620950/3641679 but it didn't work (I still have the Dock icon). Currently the Info.plist looks like this.
What can I do?
Thank you for the time you'll be taking to help me.
Informations (assuming the app name is testapp)
I stop or start the daemon using launchctl start/stop
When I double click on the executable (in testapp.app/Contents/MacOs/testapp) i have the testapp.app in the Dock (with the icon file specified in the Info.plist). Here is a screenshot res.cloudinary.com/doit0eqlo/image/upload/v1400750376/app_k3adzh.png
Sorry for some links, I must have 10 reputation to post more than 2 links.
So, I finally found the solution! The solution is in several steps.
Step 1: Editing the Info.plist
I had to add the LSBackgroundOnly key. It is a string and has to be set to 1.
As said in the Apple Documentation :
LSBackgroundOnly
specifies whether this app runs only in the background. If this key exists and is set to “1”, Launch Services runs the app in the background only.
Information:
Although the documentation specifies this key is a boolean, setting it to a string with the value 1 in it does the trick.
To do so:
Go to your app's folder
Go into the .app's folder then the Contents one (eg. /Applications/test.app/Contents)
Open the Info.plist with any text editor you want (SublimeText, TextEdit,Xcode, etc.)
Add the following lines before the closing dict tag (</dict>)
<key>LSBackgroundOnly</key>
<string>1</string>
Save everything.
Step 2: Edit the appname.ini
Initially I only did the part 1, so it wasn't enough. I found the solution in this question.
The file is in the 'Contents/MacOs' folder inside your application's .app (e.g /Applications/testapp.app/Contents/MacOs/testapp.ini).
Open the .ini file (with any text editor you want).
Before the -vmargs line, add the following line: -nosplash
After the -vmargs line, add the following two lines:
-Xdock:hidden
-Dapple.awt.UIElement=false
Save the file, and now you can launch the app: it shouldn't be any icon neither in the Dock nor in the 'Force Quit' window, but your app should be running in the background.
Hope this helps,
Add this to your info.plist: -
<key>LSUIElement</key>
<true/>
Note that the value here is set to 'true' and not 1
As the Apple docs state: -
Specifies whether the app is an agent app, that is, an app that should not appear in the Dock or Force Quit window. See “LSUIElement” for details.

Resources