【问题标题】:how to close an active document in Mac OS using shell script如何使用 shell 脚本关闭 Mac OS 中的活动文档
【发布时间】:2018-11-02 06:31:24
【问题描述】:

我正在使用苹果脚本关闭活动文档,如下所示

  tell application "Microsoft Word"
                activate
                try
                    if not (exists active document) then error number -128

                    close active document saving yes

                on error

                end try
            end tell

想要使用 shell 脚本执行类似的操作。我想优雅地关闭它并且不想使用 kill 命令。而且我不想使用 osascript 来调用苹果脚本。我想要一种使用本机 shell 命令的优雅方式

【问题讨论】:

    标签: macos shell applescript macos-mojave


    【解决方案1】:

    您好,您可以在 shell 代码中使用 osascript

    #!/bin/sh
    osascript <<EOF
    tell application "$1"
      close (every window whose name is "$2")
    end tell
    EOF
    

    编译这段代码并生成filename.sh

    或者你可以使用

    #!/bin/sh
    osascript <<EOF
    tell application "Preview"
      close (every window whose name is "$1")
    end tell
    EOF
    

    要在 cmd 中使用它,请键入 $./file_name 预览应用程序名称

    这里我使用了https://ss64.com/osx/osascript.html的引用

    【讨论】:

    • 我不想以任何形式使用applescript,我正在寻找本机shell命令
    • @Dutt 为什么?首先,如果不使用 AppleScript,您将无法做到这一点。其次,这里提供的解决方案确实使用了本机shell命令,而这些shell命令恰好包含osascript,它以文本文件(或heredoc)作为其参数并将其作为一个苹果脚本。有什么问题 ?或者,如果“本机”是指 shell builtin 命令,那么您绝对没有希望实现您想要的目标。
    • 谢谢你这就是我想知道的@CJK
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2019-06-26
    • 2014-07-28
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多