【发布时间】:2020-06-16 00:58:34
【问题描述】:
我正在按照本页上列出的相同说明进行操作 https://discussions.apple.com/thread/7761153?login=true 但是我无法打开这个文本文件
tell application
"TextEdit" open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r.rtxt"
end tell
我得到的错误信息是:预期的“given”、“with”、“without”、其他参数名称等,但找到了类名称。
最终我需要用 python 脚本打开它。因此,当我使用 Python 时,我会收到一条不同的错误消息:
file='Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt'
os.system(
... f'''/usr/bin/osascript -e
... 'tell application
... "TextEdit" open file "{file}"
... end tell' ''')
/usr/bin/osascript: option requires an argument -- e
usage: osascript [-l language] [-e script] [-i] [-s {ehso}] [programfile] [argument ...]
sh: line 3: tell application
"TextEdit" open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"
end tell: command not found
我还应该补充一点,我确定该文件存在,因为当我放入一个伪造文件时,我收到以下错误消息:没有这样的文件或目录
++++++++++++++++++++ 更新 +++++++++++++++++++
好的,我可以用 Script Debugger 打开它,但这没用,因为我需要能够用 Python 执行苹果脚本。当我跑步时
tell application "TextEdit"
open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"
end tell
在脚本调试器上它可以工作。但是Python语法:
os.system(
... f'''/usr/bin/osascript -e
... 'tell application "TextEdit"
... open file "{file}"
... end tell' ''')
/usr/bin/osascript: option requires an argument -- e
usage: osascript [-l language] [-e script] [-i] [-s {ehso}] [programfile] [argument ...]
sh: line 3: tell application "TextEdit"
open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"
end tell: command not found
另外,以下失败:
os.system(
... f'''/usr/bin/osascript -e 'tell application "TextEdit" open file "{file}" end tell' ''')
28:32: syntax error: Expected end of line but found command name. (-2741)
os.system(
... f'''/usr/bin/osascript -e 'tell application "TextEdit" open file "{file}" ' ''')
28:32: syntax error: Expected end of line but found command name. (-2741)
我知道 Python 可以使用 Applescript,因为以下工作:
os.system(
f'''/usr/bin/osascript -e 'tell app "TextEdit" to save (every window whose name is "{file}")' ''')
【问题讨论】:
-
tell application和"TextEdit"之间不应有换行符。 -
如果您所做的只是在 TextEdit 中打开一个文件,并且您正在使用
os.system,请不要打扰osascript。只需使用 unixopen命令:open -e /file/path。越简单越好。 -
这不适用于以扩展名
rtxt结尾的文件。但我明白为什么你会建议我应该声明我在我的 OP 中尝试过。 -
rtxt是一个什么样的文件?open实用程序的-e选项明确表示在 TextEdit 中打开文件,因此只要它是 TextEdit 可以打开的文件类型,它就应该工作。 -
rtxt = '富文本'。由于一些奇怪的错误,TextEdit 无法打开文件。这就是我打开这个线程的全部原因,即为了解决 textedit 无法打开文件的事实,嗯,大约需要一分钟。但如果文件是富文本格式,它可以打开文件。奇怪,但为你编程。
标签: applescript