2

Alright, this title might seem strange, but bear with me. I have an app which can be set on its preferences by the user to launch at login. That means I can expect sometimes the app will be launched by the user (clicking on the Dock/Finder, etc), but some other times the app will be launched automatically by the system, on login.

I would like to show a window when the app is launched by the user, but not when it is launched automatically (as I imagine that would be a pain for the user). How can I do that?

JWWalker
  • 22,385
  • 6
  • 55
  • 76
Alex
  • 5,009
  • 3
  • 39
  • 73

2 Answers2

1

Although it may depend on how you intend to automate the launch of the app, you could use command line arguments to distinguish between system launch vs. user launch.

So, the command line launch might like like this:

MyApp -autoLaunch "Y"

To parse the command line args in a Cocoa app, you could use NSUserDefaults (Yes, you can!):

if( ![[NSUserDefaults standardUserDefaults] objectForKey:@"autoLaunch"] isEqualToString:"Y"] ) {
    // do something for user initiated launch
}
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • How about using `[[NSWorkspace sharedWorkspace] launchApplication: @"MyApp" showIcon: NO autolaunch: YES]`? As I understand, that sets a specific user-defaults key to YES. The problem is that I can't find any documentation on what's the name of that key. Probably @"autolaunch"? Oh, and any idea what 'showIcon:' does? – Alex Sep 18 '12 at 14:52
  • @NSBum: thanks for your answer but how do you add this "Y" parameter ? In my case, I use LSSharedFileListRef as described here http://stackoverflow.com/questions/815063/how-do-you-make-your-app-open-at-login to start the app at session login. How to set and get this "Y" parameter ? – toto_tata Jan 04 '13 at 17:26
  • Unsure. The `LSSharedFileListInsertItemURL` doesn't seem to have a parameter for this. It does take a `CFDictionaryRef inPropertiesToSet` but there's nothing much in the header file about that param. – FluffulousChimp Jan 04 '13 at 19:57
0

I don't have an exact answer to your question. However, may I suggest that your app should show the window if the window was visible when the user last quit your app?

This may be more in-line with the Mac UI guidelines' suggestion on restoring apps and windows, and is within the user's expectations.

Also, a user who set your app to launch at login will probably understand to close the window and not have it restored the next time, or make the system also hide your app during login.

Heng-Cheong Leong
  • 824
  • 1
  • 8
  • 13
  • While what you propose makes sense to apps that have 'normal' windows, the window I want to show is based on the menu-bar, 'attached' to a NSStatusItem, and so not really in that 'normal' category. It makes sense to show that window automatically when the user opens the app because, if the user deliberately opened the app, that means he wants to do something with it, now. Showing the window automatically serves as a shortcut to the user's action. However, when the app is autolaunched, that does not apply, and would be a pain for the user to have to close the window every time he logs in. – Alex Sep 18 '12 at 15:04