Rename app on open

  1. 10 months ago

    Marc V

    20 Dec 2016 Pre-Release Testers, Xojo Pro Netherlands
    Edited 10 months ago by Marc V

    Hi all,

    I have a question: I have an app which, when compiled, I duplicate and then rename. For example, /Applications/xojoxapps/myapp.app duplicated to /Applications/xojoxapps/yourapp.app.

    However, when I run yourapp, everything from the menubar to CMD-TAB shows the name myapp.

    Which means that when I run both myapp and yourapp, I see myapp running twice.

    Is there a way to set the app's menubar- and application name, on app-start, to the file name, so that when you CMD-TAB, one sees myapp and yourapp?

    Mac OS X 10.11.6
    Xojo 2016 R4.1

    - -
    Thanks for any input,
    Marc

    Found the answer in the comments in this stack overflow question
    http://stackoverflow.com/questions/28808226/changing-cocoa-app-icon-title-and-menu-labels-at-runtime

    I tested here and it worked in Xojo 2016R3 on Mac OS X 10.10.5 (not all of us can afford fancy new software and computers ;) )

     #if TargetMacOS
        'NSMenu* theMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; theMenu.title = @"Test"
        
        Declare Function NSClassFromString Lib "Cocoa" (className As CFStringRef) As Ptr
        Declare Function sharedApplication Lib "Cocoa" Selector "sharedApplication" (aNSApplication As Ptr) As Ptr
        Declare Function mainMenu Lib "Cocoa" Selector "mainMenu" (aNSApp As Ptr) As Ptr
        Declare Function itemAtIndex Lib "Cocoa" Selector "itemAtIndex:" (aNSMenu As Ptr, index As Integer) As Ptr
        Declare Function submenu Lib "Cocoa" Selector "submenu" (aNSMenuItem As Ptr) As Ptr
        Declare Sub setTitle Lib "Cocoa" Selector "setTitle:" (aNSMenu As Ptr, NewTitle As CFStringRef)
        
        
        DIM NSApp As Ptr = sharedApplication(NSClassFromString("NSApplication"))
        DIM NSAppMainMenu As Ptr = mainMenu(NSApp)
        DIM applicationNSMenuItem As Ptr = itemAtIndex(NSAppMainMenu, 0)
        DIM applicationNSSubMenu As Ptr = submenu(applicationNSMenuItem)
        setTitle applicationNSSubMenu, "hello"
      #endif
  2. Axel S

    is not verified 20 Dec 2016 Europe (Germany, Erfurt)

    I think the app name is also stored in 'info.plist'

  3. Michel B

    20 Dec 2016 Pre-Release Testers, Xojo Pro
    Edited 10 months ago by Michel B

    Why not simply name the app correctly from the start instead of renaming the bundle after compile ? You do that in the Build settings inspector for Mac OS X.

    At any rate, the app internally could not care less about the name of the bundle file.

  4. Marc V

    20 Dec 2016 Pre-Release Testers, Xojo Pro Netherlands

    @Axel S
    That's okay and can stay that way. I just want to change it while it is running, so OS X shows the user its bundle-name.

    @Michel B
    'Simply name correctly from the start' assumes I make a mistake and certainly doesn't answer my question.

    The app has its correct name when compiled. Its purpose is to be duplicated x times.
    I've checked on Windows and there the app shows its new name, when running. So 'could not care less' is probably only valid for Mac OS X.

    Let's start with the name of the application in the menu-bar ... is that changeable at runtime?

  5. Tim P

    20 Dec 2016 Pre-Release Testers, Xojo Pro
    Edited 10 months ago by Tim P

    @Marc V Let's start with the name of the application in the menu-bar ... is that changeable at runtime?

    You are really really not supposed to. It's deceptive to the user.

    It does, however, look possible.
    http://stackoverflow.com/a/27166231

    I leave turning that into declares to you though.

  6. Deleted 10 months ago by shao s
  7. shao s

    is not verified 20 Dec 2016 Answer Sudbury, Ontario, Canada
    Edited 10 months ago by shao s

    Found the answer in the comments in this stack overflow question
    http://stackoverflow.com/questions/28808226/changing-cocoa-app-icon-title-and-menu-labels-at-runtime

    I tested here and it worked in Xojo 2016R3 on Mac OS X 10.10.5 (not all of us can afford fancy new software and computers ;) )

     #if TargetMacOS
        'NSMenu* theMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; theMenu.title = @"Test"
        
        Declare Function NSClassFromString Lib "Cocoa" (className As CFStringRef) As Ptr
        Declare Function sharedApplication Lib "Cocoa" Selector "sharedApplication" (aNSApplication As Ptr) As Ptr
        Declare Function mainMenu Lib "Cocoa" Selector "mainMenu" (aNSApp As Ptr) As Ptr
        Declare Function itemAtIndex Lib "Cocoa" Selector "itemAtIndex:" (aNSMenu As Ptr, index As Integer) As Ptr
        Declare Function submenu Lib "Cocoa" Selector "submenu" (aNSMenuItem As Ptr) As Ptr
        Declare Sub setTitle Lib "Cocoa" Selector "setTitle:" (aNSMenu As Ptr, NewTitle As CFStringRef)
        
        
        DIM NSApp As Ptr = sharedApplication(NSClassFromString("NSApplication"))
        DIM NSAppMainMenu As Ptr = mainMenu(NSApp)
        DIM applicationNSMenuItem As Ptr = itemAtIndex(NSAppMainMenu, 0)
        DIM applicationNSSubMenu As Ptr = submenu(applicationNSMenuItem)
        setTitle applicationNSSubMenu, "hello"
      #endif
  8. Michel B

    20 Dec 2016 Pre-Release Testers, Xojo Pro
    Edited 10 months ago by Michel B

    @Marc V 'Simply name correctly from the start' assumes I make a mistake and certainly doesn't answer my question.

    The app has its correct name when compiled. Its purpose is to be duplicated x times.
    I've checked on Windows and there the app shows its new name, when running. So 'could not care less' is probably only valid for Mac OS X.

    Indeed, Mac and Windows do not work the same way. That does not help but it is a fact. Especially when trying to run several instances of the same app... But hey, since I did not answer your question, have fun now.

  9. Marc V

    24 Dec 2016 Pre-Release Testers, Xojo Pro Netherlands

    Thanks for the answers!
    I'll give it a try next year and report back here.

    Happy holidays and a good and healthy new year!

  10. 9 months ago

    Marc V

    1 Jan 2017 Pre-Release Testers, Xojo Pro Netherlands

    @shao s your code works very good! Even in the CMD-TAB application list, it now shows the new application name! Thanks!

or Sign Up to reply!