繁体   English   中英

从 Applescript 调用 Python 脚本

[英]Call a Python script from a Applescript

我的 Applescript 和 Python 脚本在当前工作目录中。 现在我需要使用 shell 命令使用 applescript 的管理员权限调用名为 test.py 的 Python 脚本。

Applescript 中的这段代码给出了密码

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

Applescript 中的此代码从 Applescript 手动调用 python 脚本

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

如何将 presentDir 与管理员权限一起添加到此命令中?

编辑和更新:

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

它给出了一个错误

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

但显示对话框 calldir 显示

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

原因:

由于空间原因,它在 shell 脚本命令中的“Microsoft”一词之后被分解。

如果您知道脚本在同一目录中,只需使用:

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

注意test.py之后的空格,然后是右引号。 您可能需要字符串是/test.py ,而不是test.py ,我不确定。

我从http://developer.apple.com/library/mac/#technotes/tn2065/_index.html获得此信息。

编辑:试试

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

您的初始脚本不起作用,因为您没有使用反斜杠“\”转义空格,例如:“/Users/mymac/Documents/Microsoft\ User\ Data/test.py”

暂无
暂无

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

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