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

10.4: Pass command-line arguments to AppleScript UNIX
Tiger only hintI just discovered this, by looking at the man page for osascript.

From 10.4, you can now pass parameters to scripts run with oscascript. From that page:

  Any arguments following the script will be passed as a list of strings
  to the direct parameter of the ``run'' handler.  For example:

  a.scpt:
    on run argv
      return "hello, " & item 1 of argv & "."
    end run

  % osascript a.scpt world
    hello, world.
Cool!
    •    
  • Currently 3.14 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)
 
[67,529 views]  

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.
10.4: Pass command-line arguments to AppleScript
Authored by: nomis on Jun 08, '05 06:35:44PM
Note that the entire script must be placed between the on run argv and end run.

[ Reply to This | # ]
10.4: Pass command-line arguments to AppleScript
Authored by: wgscott on Jun 09, '05 03:41:17AM

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



[ Reply to This | # ]
10.4: Pass command-line arguments to AppleScript
Authored by: Krioni on Jun 13, '05 09:09:20AM

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.



[ Reply to This | # ]
10.4: Pass command-line arguments to AppleScript
Authored by: mark hunte on Jun 09, '05 06:03:45AM

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..

---
mh



[ Reply to This | # ]
10.4: Pass command-line arguments to AppleScript
Authored by: mark hunte on Jun 09, '05 06:58:15AM
Here a quick and small example to show potential.

Print.scpt

on run this_file
	
	tell application "TextWrangler"
		print this_file
		
	end tell
end run
When in terminal you can run this.

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

-

---
mh

[ Reply to This | # ]

10.4: Pass command-line arguments to AppleScript
Authored by: mzs on Jun 10, '05 01:36:44PM
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):


my_foo=val osascript foo.scpt 

Now at the beginning of the AppleScript add this:


set this_foo to system attribute "my_foo"

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.

[ Reply to This | # ]

10.4: Pass command-line arguments to AppleScript
Authored by: John Rethorst on Jul 29, '05 09:02:13PM

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



[ Reply to This | # ]
10.4: Pass command-line arguments to AppleScript
Authored by: JackRepenning on Dec 07, '11 01:43:54PM

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].

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



[ Reply to This | # ]