Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Pass command line arguments to GUI apps by default UNIX
Recently I've been wanting a way to pass command-line arguments to GUI apps by default, in my case a -geometry parameter to Emacs.app to make it start up in something approximating full-screen mode. It turns out there's a very easy way to do this.

As many already know, what appear as applications on OS X are in fact directories, containing the actual executable in their Contents » MacOS subdirectory. To cause this executable to be run with specific arguments, simply rename the existing executable to something else (I've used something like appname-bin) and replace it with a shell script that exec`s the renamed binary with whatever arguments you please (followed by chmod 755 or similar to make the script executable). For example, my script for Emacs.app looks like this:

#!/bin/sh
exec /Applications/Emacs.app/Contents/MacOS/emacs-bin -geometry 177x47 $@


The $@ at the end may or may not really be necessary, but I put it in to be on the safe side -- the OS apparently passes a parameter starting with -psn that I'm guessing is the position at which to place the app's window (remembered from the last time it was run). Emacs doesn't recognize this parameter, but other apps probably do. And as I discovered after a bit of experimentation, you do need to use an absolute path to the binary. I've only tested this on Tiger, but I'd be surprised if it didn't also work on Leopard.
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (11 votes cast)
 
[22,728 views]  

Pass command line arguments to GUI apps by default | 7 comments | Create New Account
Click here to return to the 'Pass command line arguments to GUI apps by default' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Pass command line arguments to GUI apps by default
Authored by: Phoenix1701 on Aug 12, '08 07:49:10AM
The -psn argument stands not for "position", but rather for "process serial number". Here's a well-informed post on MacRumors discussing this argument and why it's important:

http://forums.macrumors.com/archive/index.php/t-69397/t-207344.html

Needless to say, it's probably a good idea to pass this along. :)

[ Reply to This | # ]
Pass command line arguments to GUI apps by default
Authored by: rudedog on Aug 12, '08 08:43:29AM

Doing this on Leopard will very likely break the app if it's been signed.



[ Reply to This | # ]
Pass command line arguments to GUI apps by default
Authored by: jsenn on Aug 12, '08 08:55:33AM
Also doing something like this:

#!/bin/bash
exec "${0%/*}/emacs-bin" -geometry 177x47 $@

will prevent the obvious problem with the hard-coded absolute path if the Application is moved.

[ Reply to This | # ]

Pass command line arguments to GUI apps by default
Authored by: astrosmash on Aug 12, '08 10:54:21AM

You can set the startup size of Emacs by adding the following to your .emacs file:

(setq initial-frame-alist '((width . 150) (height . 60)))

[ Reply to This | # ]

Works great for starting firefox in a different profile
Authored by: lowbatteries on Aug 12, '08 10:57:37AM

I have two copies of Firefox, one is Firefox 3 that I use daily, the other is Firefox 2 I use for backwards compatibility testing.

It's always a pain to me to have each version use the same profile. Using this hint, I renamed "firefox-bin" to "firefox-bin-default", and created a new firefox-bin:

<code>
#!/bin/bash
exec "${0%/*}/firefox-bin-default" -p "TestProfile"
</code>

You could just leave this at "-p" with no profile specified to bring up the profile selection window.



[ Reply to This | # ]
Pass command line arguments to GUI apps by default
Authored by: jaysoffian on Aug 12, '08 04:53:09PM
You need to quote the $@ to properly handle any arguments which contain whitespace:

#!/bin/sh
exec /Applications/Emacs.app/Contents/MacOS/emacs-bin -geometry 177x47 "$@"
Combining this with the suggestion to allow the application bundle to be moved:

#!/bin/bash 
exec "${0%/*}/emacs-bin" -geometry 177x47 "$@"


[ Reply to This | # ]
Environment.plist
Authored by: zacht on Aug 12, '08 08:38:54PM
It might also be worth checking out some old hints such as this one:

[link:]http://www.macosxhints.com/article.php?story=20040715133738459




[ Reply to This | # ]