Search code examples
pythonmacoscocoaapp-bundle

Create (sane/safe) app bundle identifier from any (unsafe) string


I want to create a sane/safe app bundle name (i.e. somewhat readable, no "strange" characters, etc.) from some random Unicode string (mich might contain just anything).

(It doesn't matter for me wether the function is Cocoa, ObjC, Python, etc.)

(This is related to the filename question and the bundle name question but the bundle identifier is much more restrictive. I think it cannot even contain spaces and I also would want to strip out the dots and put my own prefix.)

I think Xcode also hase some function to do that automatically from the app name. Maybe there is some standard function in Cocoa to do that.


Solution

  • Bundle identifiers are meant to be in reverse URL form (guaranteeing global uniqueness):

    com.apple.xcode, for example

    So really you need a domain name, then you can invent whatever scheme you like below that.

    Given this, and some knowledge of the characters in your input, you can either scan through your input composing a new string with only the bits you want, or use methods like stringByReplacingOccurrencesOfString: withString: and, if you like, lowercaseString.

    The permitted characters in bundle identifiers are named in the Property List Documentation as:

    The bundle ID string must be a uniform type identifier (UTI) that contains only alphanumeric (A-Z,a-z,0-9), hyphen (-), and period (.) characters. The string should also be in reverse-DNS format.