Mac App Command Line Arguments

  1. Mac Run App From Command Line With Arguments
  2. Mac Command Line Basics
  3. Mac App Command Line Arguments 2

10.4: Pass command-line arguments to AppleScript 8 comments Create New Account
Click here to return to the '10.4: Pass command-line arguments to AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.

I have Chrome installed in Applications, and a shortcut to it in the Dock. How can I achieve to force some command-line arguments to each start-up of this app? Let me be clearer, in the Terminal, I can do this: open /Applications/Google -args -explicitly-allowed-ports=6666 And that way I modify Chrome's behavior as I like it. Download the Command Line Tools package from the Developer website. The Command Line Tools package is available for download on the Download for Apple Developers page. Log in with your Apple ID, then search and download the Command Line Tools package appropriate for your machine such as macOS 10.12 as shown in Figure 1.

Note that the entire script must be placed between the on run argv and end run.

Hasn't this always worked if you just put the osascript in a shell script?

Not the entire script: if you have properties, they'd go before the 'on run' line and if you have handlers (functions), they'd go outside of the run block as well. It's just similar to the main() function in C.

Mac command line

Mac Run App From Command Line With Arguments


Yes but this is not putting the AS in a shell script this is calling a AS script and using it like a shell command.
I did not know you could do it this way, and it may come in handy when I am playing..

Here a quick and small example to show potential.


When in terminal you can run this.

osascript Print.scpt /path/to/file/foo.txt



Here is a trick I learned from the ORA AppleScript book to pass args into a compiled AppleScript that works with all versions of OS X.

In a Bourne style shell do this (in csh style shells do a setenv):

Now at the beginning of the AppleScript add this:

The 'system attribute' corresponds to the environment variables, so what you are doing is passing an argument as an environment variable. You can now use this_foo in your script to get your passed value. In practice the overhead involved with accessing a separate AppleScript file and using the system extension to extract the value of the environment variable takes more time than compiling a SHORT AppleScript so keep this in mind. You will only see a speed increase for your longer AppleScripts, the ones that take a longer time to compile.

Can the Terminal script ('% osascript a.scpt world') be saved as an application, so double-clicking it will run it?

For completeness, note that the name of the script (minus directories and type extensions) is available as 'my name as string'. This is (more or less) what the C programmer sees as argv[0].

Mac Command Line Basics

Where is the knowledge we have lost in information?
-- T. S. Eliot, 'Choruses from 'The Rock'

Mac App Command Line Arguments 2

Recently I've been wanting a way to pass command-line arguments to GUI apps by default, in my case a -geometry parameter to 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 looks like this:
exec /Applications/ -geometry 177x47 [email protected]

The [email protected] 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.