Rename app on open

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

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

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.

@Axel Schneider
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 Bujardet
‘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?

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.

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 :wink: )

[code] #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[/code]

[quote=304457:@Marc Vos]‘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.[/quote]

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.

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!

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