Switching between background <-> foreground app type at run-time

  • How do I switch between BackgroundOnly (has no doc icon and menu) and
    Foreground (has dock icon and menu) application at runtime, both ways?

    I am writing a small tool that display temperature on the status.
    From the drop down menu a user can select "Configure" in which case
    i would like to put a dock icon and system menu, and go back to
    background only application once the "config window" is closed.

    This is what I found so far:

    1. Setting LSBackgroundOnly or LSUIElement in Info.plist to "1" makes
    the application without a dock icon and menu.

    Problem: This only works when the application is started, I need to
    be able to change in during run-time back and forth.

    2. "TransformProcessType(&psn,
    kProcessTransformToForegroundApplication);" can change background
    only app (LSBackgroundOnly in Info.plist set to 1) to a foreground
    application with a dock icon and menu.

    Problem: This only works one way (from background to foreground).
    Need a way to go backwards as well (foreground to background) to have
    this to be a viable solution

    3. Have 2 different applications: One with LSBackgroundOnly set to 1,
    that creates the status item and Another one without

    Problem: They will be 2 different processes and a lot of interprocess
    communication needs to be used between. Not very sure the proper IPC
    mechanism to be used here. Plus I have to maintain 2 different
    binaries instead of one for a very simple tool I am creating.
  • On Sep 26, 2007, at 11:06 AM, vance wrote:

    >
    > How do I switch between BackgroundOnly (has no doc icon and menu)
    > and Foreground (has dock icon and menu) application at runtime, both
    > ways?

    You can't.

    > 3. Have 2 different applications: One with LSBackgroundOnly set to
    > 1, that creates the status item and Another one without
    >
    > Problem: They will be 2 different processes and a lot of
    > interprocess communication needs to be used between. Not very sure
    > the proper IPC mechanism to be used here. Plus I have to maintain 2
    > different binaries instead of one for a very simple tool I am
    > creating.

    This is your only option. As you noted, you can use
    TransformProcessType to go from background-only to foreground-capable,
    but there's no way to go back.

    -eric
  • On Sep 26, 2007, at 2:06 PM, vance wrote:

    > How do I switch between BackgroundOnly (has no doc icon and menu)
    > and Foreground (has dock icon and menu) application at runtime,
    > both ways?
    Basically you can't. Your summary of the options is pretty accurate.

    > 1. Setting LSBackgroundOnly or LSUIElement in Info.plist to "1"
    > makes the application without a dock icon and menu.
    I think this is the most reasonable solution, assuming your
    configuration UI is relatively simple (single window). Make your
    application an LSUIElement app (not LSBackgroundOnly) that uses
    NSStatusItem. You can then bring up a window just like in any Cocoa
    app, you just won't get a Dock icon or menu bar. I believe copy/paste/
    etc. even work, but I can't remember for sure.

    > 3. Have 2 different applications: One with LSBackgroundOnly set to
    > 1, that creates the status item and Another one without
    A variant on this is to have your NSStatusItem app set as
    LSUIElement, and your non-background app be inside the main app's
    bundle. Just add another target to your Xcode project, and it should
    be reasonable to manage. You can do IPC using NSUserDefaults, -
    [NSUserDefaults synchronize], and either watch for the NSWorkspace
    notification that the GUI app has quit, or have the GUI app send an
    NSDistributedNotification to tell the background app to resync its
    prefs. (You may need to drop down to CFPreferences API to share the
    user defaults this way.)

    --
    Mike Blaguszewski / Cocoa Hacker / Ambrosia Software, Inc.
  • --- vance <cocoa...> wrote:

    > 3. Have 2 different applications: One with
    > LSBackgroundOnly set to 1,
    > that creates the status item and Another one without
    >
    > Problem: They will be 2 different processes and a
    > lot of interprocess
    > communication needs to be used between. Not very
    > sure the proper IPC
    > mechanism to be used here. Plus I have to maintain 2
    > different
    > binaries instead of one for a very simple tool I am
    > creating.

    It will be simpler than doing what you are trying to
    do. Check out Distributed Objects and
    NSDistributedNotificationCenter. One will probably be
    good enough for your purposes, and they're pretty
    easy. This is how it's normally done.

    Cheers,
    Chuck

          ____________________________________________________________________________________
    Luggage? GPS? Comic books?
    Check out fitting gifts for grads at Yahoo! Search
    http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs
    =bz
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today