file as command line argument to script


I would like to read in a file as a command line argument into my applescript, but I cannot work out how to do it.

My script is as follows:

on run(file_name)
run application “PAUP 1.0b10 (Altivec)”
tell application “PAUP 1.0b10 (Altivec)”
PAUPCommand “execute file_name;”
end tell
end run

I would like to be able to run it from the command line using the following command:

./MyScript “someFile.txt”

enabling me to run it with any file that I choose.

I do not know if this is possible with applescript (this is my first script).

Any suggestions??


I’m not sure if I understand your question correctly, but if you are trying to save a unix command as a text file and run it from an AppleScript, you can try this.

tell application "Terminal" to do script "cd ~/bin/; ./testing"

1). It must be executable – chmod 755 filename
2). TextEdit files must be saved as “Make Plain Text”
3) My testing file has the command “ls -al | more” in it. When I run the above AppleScript, it lists the files in my ~/bin directory

Hope that helps…

Not quite, I need to read in a file and use it in my script as a variable (in the same way a parameter is passed to a function). That is, I think the first line of the script should look something like this:

on run(file_parameter)

which should be useable within the script as a literal file name.

I found a previous post which suggests that doing this using applescript is very difficultm (or impossible).

What I think I will do instead is generate the script automatically, then compile and run it on the fly. Unless you have any other suggestions???



You can’t pass command-line arguments to an AppleScript… it’s not currently supported by the OS

However, if you have a text version of the script, you can run that on the command line, so you might be able to dynamically write out the script including the parameters you want and then:

osascript /path/to/script

and it will compile and run the script.

It sounds like you are trying to run an applescript with a file-path as the input. The Finder’s ‘open’ command allows to open a file with a specific application. So, if you compile your AppleScript as an application, you could write a short osascript fromt he command-line that tells the Finder to open a Posix file YOUR_PATH using YOUR_APPLETS_PATH

open : Open the specified object(s)
open reference – list of objects to open
[using reference] – the application file to open the object with

tell app "Finder" to open {Posix file SOME_PATH} using Posx file "~/Applications/MY_APPLET"

Of course, you’ll have to wrap that in an osascript command to run it form the command-line.


What I have done is automatically generate the script as a text file using a java program then compile and run it from the command line using “osascript” from within a makefile.

This is maybe a bit clumsy but seems to work well.

Thanks for all your help.

Dave. :smiley:

actually, i’ve been able to do it by using an environment variable and the “system attribute” standard addition and a shell script to make it work:

  1. set up a shell script (I call mine wrapper) as follows:


export COMMAND_LINE=$*
$program $*

  1. at the beginning of your applescript do:

set command_line to system attribute “COMMAND_LINE” as string

  1. instead of invoking your applescript directly, invoke the shell script with
    the applescript as arg 1 followed by the rest of the command args.

  2. voila (of course you have to parse command_line) to get what you need.