我试图从python中调用apple脚本。我一直在使用py-applescript
。我试图从Mail应用程序中获取邮件并将内容转储到目录中。 (此邮件帐户只有50封电子邮件。)从"脚本编辑器"运行时,以下脚本运行良好。但是进入错误""在python中执行时的分支。
import applescript
fetch_mail_test_scpt = applescript.AppleScript('''
tell application "Mail"
set destinationFolder to ":Users:ranjit:Workspace:CRA:"
set my_counter to 0
repeat with theMessage in (every message of inbox)
set this_content to source of theMessage
set my_counter to my_counter + 1
set coerced_to_string to my_counter as string
set targetFile to destinationFolder & coerced_to_string
try
set my_file to open for access file targetFile with write permission
set eof of my_file to 0
write this_content to my_file
close access my_file
on error
try
close file targetFile
---return "coming here"
end try
---return "error!!?"
end try
end repeat
return my_counter
end tell
''')
print(fetch_mail_test_scpt.run())
此python脚本返回计数器的值。如果你取消注释"返回"错误!!?""然后它返回错误。基本上,它无法访问该文件。这是py-applescript的某种已知问题吗?或者有解决方法吗?谢谢! (一般来说,py-applescript对我编写的4-5个脚本一直很有用。)