【问题标题】:Cannot open a file with Applescript无法使用 Applescript 打开文件
【发布时间】: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。只需使用 unix open 命令:open -e /file/path。越简单越好。
  • 这不适用于以扩展名 rtxt 结尾的文件。但我明白为什么你会建议我应该声明我在我的 OP 中尝试过。
  • rtxt 是一个什么样的文件? open 实用程序的 -e 选项明确表示在 TextEdit 中打开文件,因此只要它是 TextEdit 可以打开的文件类型,它就应该工作。
  • rtxt = '富文本'。由于一些奇怪的错误,TextEdit 无法打开文件。这就是我打开这个线程的全部原因,即为了解决 textedit 无法打开文件的事实,嗯,大约需要一分钟。但如果文件是富文本格式,它可以打开文件。奇怪,但为你编程。

标签: applescript


【解决方案1】:

在变量script中说明Applescript是什么,例如:

 from subprocess import Popen, PIPE
file = 'hey_you.rtxt"
    script =  f'''
    tell application "TextEdit"
        open file "{file}"
    end tell
    '''

然后:

p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
...stdout, stderr = p.communicate(script)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多