Re: AppleScript calling Python function
Re: AppleScript calling Python function
- Subject: Re: AppleScript calling Python function
- From: Steven Majewski <email@hidden>
- Date: Wed, 21 Apr 2004 15:13:47 -0400
On Apr 21, 2004, at 3:56 AM, Simon Forster wrote:
>
This is a bit of a neophyte question(s) which is probably more Python
>
than AppleScript but I need help and you guys may point out something
>
obvious that I'm missing!
>
>
I've written a very simple Python module which I want to call via
>
AppleScript from within FileMaker Pro.
>
>
Problem: I need to load my module so that I can call the Python
>
function with the appropriate arguments. To put this in some form of
>
context. From the command line:
>
>
python
>
>>> sys.path.append('/Users/simonforster/Development/Python/Lib')
>
>>> import regex_domain3
>
>>> regex_domain3.GetDomain('www.look4generics.com/storvas .htm')
>
'www.look4generics.com'
>
>
Now, in theory I can write an AppleScript which'll mimic this
>
approach. i.e. Set the python path, import the module, do the do. But
>
this seems incredibly wasteful. (Bear in mind that the AppleScript
>
call is stateless. i.e. each time I run it it'll be analogous to
>
starting a completely new shell).
>
>
So, how do I set python's path for my user? And, FWIW, if I "echo
>
$PYTHONPATH" in the Terminal, I get a blank line. My research suggests
>
that this is a valid environment variable though. Don't understand.
>
Read <
http://developer.apple.com/technotes/tn2002/tn2065.html> closely.
If you're setting $PYTHONPATH in .bashrc or .profile, it's not going to
be there in the environment that
Applescript's 'do shell script' is executing in.
>
On a wider front, being able to integrate Python with AppleScript
>
gives one a very powerful tool. Is there a better way to pass
>
arguments to a Python function from within AppleScript?
Already noted that you can pass multiple commands to python with the
"-c" switch:
do shell script "python -c 'import sys; print sys.path'"
You can also pass args:
do shell script "python -c 'import sys; print sys.path; print sys.argv'
one two three"
Python syntax requires that some commands can't go on a single line.
In that case you can use something like this:
set pycmd to "import sys
for x in sys.argv[1:] : print x"
do shell script "python -c " & quoted form of pycmd & " one two five"
or even:
do shell script "echo " & quoted form of pycmd & "| python -i -c ''
five six"
But if you're going to be doing anything complicated, you're better off
just writing
the commands out to a .py file. You can still pass parameters on the
command line
and grab them from sys.argv.
[ Although I much prefer Python to Perl, Perl's facility with
one-liners sure does
come in handy from applescript. ]
-- Steve Majewski
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.