Python 3.x Apple脚本:如何将用户输入保存到文本文件
我在python应用程序中使用apple脚本,如何将用户提供的输入保存为文本文件Python 3.x Apple脚本:如何将用户输入保存到文本文件,python-3.x,applescript,Python 3.x,Applescript,我在python应用程序中使用apple脚本,如何将用户提供的输入保存为文本文件 firstname = """ display dialog "Enter your first name " default answer "" ¬ buttons {"Submit"} """ 考虑以下任一解决方案: 解决方案A:使用Python将用户输入保存到文本文件中。 导入操作系统 从子流程导入Popen、PIPE userPrompt=“” 告诉应用程序“查找器” 使活动 返
firstname = """
display dialog "Enter your first name " default answer "" ¬
buttons {"Submit"}
"""
考虑以下任一解决方案: 解决方案A:使用Python将用户输入保存到文本文件中。
导入操作系统
从子流程导入Popen、PIPE
userPrompt=“”
告诉应用程序“查找器”
使活动
返回的文本(显示对话框“输入您的名字”默认答案”按钮{“提交”})
结束语
"""
proc=Popen(['osascript','-',stdin=PIPE,stdout=PIPE,stderr=PIPE,universal_newlines=True)
firstname,error=proc.communicate(userPrompt)
filePath=os.path.join(os.path.expanduser(“~”)、“Desktop”、“result.txt”)
打开(文件路径“w”)作为文件:
file.write(名字)
- 这就利用构造函数来抛出基本上运行AppleScript的
命令osascript
- 目前,用户提供的输入被写入名为
的文件,该文件保存在桌面文件夹中。该模块用于确定目标文件路径。您需要根据需要对此进行更改results.txt
- 最后我们使用
解决方案B:使用AppleScript将用户输入从Python保存到文本文件。 另一种方法是使用AppleScript命令将用户输入保存到文本文件中 在这种情况下,
.py
文件如下所示:
userPrompt=“”
告诉应用程序“查找器”
使活动
将firstname设置为返回的文本(显示对话框“输入您的名字”默认答案”按钮{“提交”})
执行shell脚本“echo”和firstname的引号形式&“>~/Desktop/result.txt”
返回名字
结束语
"""
proc=Popen(['osascript','-',stdin=PIPE,stdout=PIPE,stderr=PIPE,universal_newlines=True)
firstname,error=proc.communicate(userPrompt)
#打印(名字)
该行内容如下:
do shell脚本“echo”和firstname的引用形式&“>~/Desktop/result.txt”
基本上利用shells实用程序将用户输入重定向/保存到名为results.txt
的文件中,该文件再次保存到桌面文件夹中