|Click here to return to the '10.4: Pass command-line arguments to AppleScript' hint|
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 Chrome.app -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.
on run argvand
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 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..
When in terminal you can run this.
osascript Print.scpt /path/to/file/foo.txt
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.
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
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:
exec /Applications/Emacs.app/Contents/MacOS/emacs-bin -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.