简体   繁体   中英

Call a Python script from a Applescript

My Applescript and Python script are in the present working directory. Now I need to call the Python script named test.py with admin privileges from the applescript using shell commands.

This code in Applescript gives the pwd

tell application "Finder" to get folder of (path to me) as Unicode text
set presentDir to POSIX path of result

This code in Applescript calls a python script from an Applescript manually

do shell script "/Users/mymac/Documents/'Microsoft User Data'/test.py"

How to add the presentDir to this command along with Admin privileges?

EDIT and UPDATE:

set py to "test.py "
set calldir to workingDir & py
do shell script calldir

It gives an error

error "sh: /Users/mymac/Documents/Microsoft: No such file or directory" number 127

But display dialog calldir shows

/Users/mymac/Documents/Microsoft User Data/test.py

Reason:

it gets breaked up after the word 'Microsoft' in shell script command because of space.

If you know the script is in the same directory, just use:

do shell script presentDir & "test.py " user name "me" password "mypassword" with administrator privileges

Notice the space after test.py before the close-quote. You may possibly need the string to be /test.py , rather than test.py , I'm not sure.

I got this info from http://developer.apple.com/library/mac/#technotes/tn2065/_index.html .

Edit: Try

set py to "test.py "
set calldir to quoted form of workingDir & py
do shell script calldir

Your initial script didn't work because you didn't escape the spaces with a backslash "\" as such: "/Users/mymac/Documents/Microsoft\ User\ Data/test.py"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM