从AppleScript调用Python脚本
我的AppleScript和Python脚本都在当前工作目录里。现在我需要从AppleScript中用管理员权限调用一个名为test.py的Python脚本,使用的是shell命令。
这段AppleScript代码可以显示当前工作目录
tell application "Finder" to get folder of (path to me) as Unicode text
set presentDir to POSIX path of result
这段AppleScript代码可以手动调用一个Python脚本
do shell script "/Users/mymac/Documents/'Microsoft User Data'/test.py"
我该如何将当前目录添加到这个命令中,并且还要有管理员权限呢?
编辑和更新:
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
但是 display dialog calldir 显示了
/Users/mymac/Documents/Microsoft User Data/test.py
原因:
在shell脚本命令中,遇到“Microsoft”这个词后因为空格的原因导致命令中断了。
2 个回答
0
你最开始写的脚本没有成功,是因为你没有用反斜杠 "\" 来处理空格。正确的写法应该是这样:"/Users/mymac/Documents/Microsoft\ User\ Data/test.py"
7
如果你知道脚本就在同一个文件夹里,那就直接使用:
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