Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

Running a script when a Mac package is executed

I have a Mac OS X application in the standard .app format and was wondering what is the easiest way to have a small script execute before the main program does. Is that possible?

Edit:

I only want to run the script once, so if I can set a flag or something after it runs that would be great!

Second Edit:

I do not have the XCode project so I cannot recompile anything, which is why I'm looking for another method.

Upvotes: 0

Views: 494

Answers (2)

Nik Reiman
Nik Reiman

Reputation: 40390

If the app is relatively simple, you could just repackage it in another bundle. You could write a short program to run the script and then launch their executable, which you'd want to include in your bundle as a resource.

Of course, you'd also need to copy over all the original resources from the application.

Might I ask which program you are trying to run in this way?

Upvotes: 0

mouviciel
mouviciel

Reputation: 67839

Two possibilities:

  • If you have access to the Xcode project, you can put your script in the resources of your app, get its pathname with:

    [[NSBundle mainBundle] pathForResource:@"preamble" ofType:@"sh"]
    

    and invoque it with a simple system() call at the beginning of your main() function.

  • If you don't have access to the Xcode project, I am not certain whether it is possible. I would try something like substituting actual executable with your script and add a way to call the executable at the end of the script with an exec command.

Upvotes: 3

Related Questions