Passing arguments to a script

Hi MacScripters,
So I’m making a fairly large project in Python, but I need Applescript for certain parts of it (inputing data into Excel spreadsheets, reading messages from Mail). I’ve tried to use a Scripting Bridge so I can run what I need straight in Python, but it’s far to messy and isn’t working with me. Instead, what I’m going to end up doing is making a new AS script for every function I need (which should only be two or three) and then having it return it’s values. That way, I can easily use OSA Script to run it and get the return. My question is, how can I pass the AppleScript arguments? What would the syntax be (from both ends)? And furthermore, when I try to run this in bash, it works fine, but when I try to run it in Python through bash I get weird returns. Right now I’m using Bash to run the osa process, so my code looks like:

import os new_value = os.system("osascript ~/Desktop/test.scpt"); print new_value #prints 0
whereas in bash

osascript ~/Desktop/test.scpt //prints "TEST"


I figured out how to get the return into a variable using the subprocess module, but I’m still at a loss for how to pass variables the other way

I actually just figured it out from another tutorial on this site. For anyone with the same situation, here is my final code:

#test.scpt located on the desktop
on run (mytest)
    return "TEST: " & mytest
end run


import os
from subprocess import Popen, PIPE

def run_applescript(script, string_argument):
(stdout, stderr) = Popen([“osascript”, script, “"”+string_argument+“"”], stdout=PIPE).communicate()
return stdout

print run_applescript(“/Users/user_name/Desktop/test.scpt”, “test string”)
#prints “TEST: test string”[/code]

Hi chumponimys,

Here’s a script I want to pass parameters to on run:

on run params -- list of arguments
	set n to item 1 of params
	beep n
	return n
end run

To run the script from another script using osascript:

set script_ref to (choose file) as string -- choose the script to run
do shell script "osascript -e 'run script file " & script_ref & " with parameters {3}'"

Instead of do shell script when you’re running from python, I think you can use one the popens. I’m not sure how the os.system works.

Edited: too late.

Edited: oops posted the wrong script:

set script_ref to (choose file) as string -- choose the script to run
set cmd to "run script file \"" & script_ref & "\" with parameters {3}"
do shell script "osascript -e " & quoted form of cmd


Model: MBP
AppleScript: AS 2.2.4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)

As a Pythonista, remember that PyPI is your friend: