user2152814
user2152814

Reputation: 257

Cocoa, output current App path

I am working on OS X application. In the code, I would like to output a path of the current Mac OS App to a variable for future use. So later I could read the files in the same folder. Could anyone tell me the command/method in the Xcode ?

Thanks very much.

UPDATE

To be more clear, I am using xcode and create a cocoa-application. My application is connected with applescript to control Mac software read files on the Mac. So I have to return the files' directory and name.

Actually I have no idea about what to do. So got stuck here.

Upvotes: 1

Views: 2564

Answers (2)

ingconti
ingconti

Reputation: 11636

my 2 cents for swift 5,x:

let path = Bundle.main.bundlePath
 print(path)
 

If You need a path to REAL exacutable: (i.e. you mast pass to shell or similar..)

 let appName =  AppName()

 let exePath = path + "/Contents/MacOS/" + appName 

  print(exePath)

where AppName() is:

func AppName()->String{
let bund = Bundle.main
//print(bund)
if let displayName = bund.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
    if displayName.count>0{
        return displayName
    }
}

if let name = bund.object(forInfoDictionaryKey: "CFBundleName") as? String {
    return name
}
return "no AppName"

}

Upvotes: 0

MrAsterisco
MrAsterisco

Reputation: 889

To get the URL of your application, you can use:

[[NSBundle mainBundle] bundleURL]

See the NSBundle Class Reference for further information.

Upvotes: 2

Related Questions