简体   繁体   English

从 Applescript 调用 Python 脚本

[英]Call a Python script from a Applescript

My Applescript and Python script are in the present working directory.我的 Applescript 和 Python 脚本在当前工作目录中。 Now I need to call the Python script named test.py with admin privileges from the applescript using shell commands.现在我需要使用 shell 命令使用 applescript 的管理员权限调用名为 test.py 的 Python 脚本。

This code in Applescript gives the pwd Applescript 中的这段代码给出了密码

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 Applescript 中的此代码从 Applescript 手动调用 python 脚本

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

How to add the presentDir to this command along with Admin privileges?如何将 presentDir 与管理员权限一起添加到此命令中?

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但显示对话框 calldir 显示

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

Reason:原因:

it gets breaked up after the word 'Microsoft' in shell script command because of space.由于空间原因,它在 shell 脚本命令中的“Microsoft”一词之后被分解。

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.注意test.py之后的空格,然后是右引号。 You may possibly need the string to be /test.py , rather than test.py , I'm not sure.您可能需要字符串是/test.py ,而不是test.py ,我不确定。

I got this info from http://developer.apple.com/library/mac/#technotes/tn2065/_index.html .我从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"您的初始脚本不起作用,因为您没有使用反斜杠“\”转义空格,例如:“/Users/mymac/Documents/Microsoft\ User\ Data/test.py”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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