Launch an app on OS X with command line

前端 未结 12 1007
北恋
北恋 2020-12-04 06:03

I want to launch an app on OSX from a script. I need pass it command line arguments. Unfortunately, open doesn\'t accept command line args.

The only opt

相关标签:
12条回答
  • 2020-12-04 06:42

    As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:

    open -n ./AppName.app --args -AppCommandLineArg

    0 讨论(0)
  • 2020-12-04 06:42

    I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:

    > Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
    

    I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)

    0 讨论(0)
  • 2020-12-04 06:43

    An application bundle (a .app file) is actually a bunch of directories. Instead of using open and the .app name, you can actually move in to it and start the actual binary. For instance:

    $ cd /Applications/LittleSnapper.app/
    $ ls
    Contents
    $ cd Contents/MacOS/
    $ ./LittleSnapper
    

    That is the actual binary that might accept arguments (or not, in LittleSnapper's case).

    0 讨论(0)
  • 2020-12-04 06:43

    I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChrome for normal browsing and altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.

    I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.

    What I ended up with was two AppleScript applications containing two commands each. Here is the one for altChrome:

    do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
    do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
    

    The second line starts Chrome with the alternate profile (the --user-data-dir parameter).

    The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.

    0 讨论(0)
  • 2020-12-04 06:44

    In OS X 10.6, the open command was enhanced to allow passing of arguments to the application:

    open ./AppName.app --args -AppCommandLineArg
    

    But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.

    0 讨论(0)
  • 2020-12-04 06:48

    With applescript:

    tell application "Firefox" to activate
    
    0 讨论(0)
提交回复
热议问题