【问题标题】:How to start a .command script from a mac automator app如何从 mac automator 应用程序启动 .command 脚本
【发布时间】:2019-11-07 06:51:22
【问题描述】:

我正在尝试使用基本上调用 .command 文件来完成所有工作的自动机创建一个 mac“应用程序”。命令文件将与 .app 位于同一目录中,但我首先遇到的是 - 获取已单击的 .app 文件的当前目录以确定 .command 文件的文件位置。

我试过了

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
echo "-- $SCRIPTPATH"

这只是返回我的用户主管 - 基本上〜 应用程序本身位于桌面示例的目录中:~/Desktop/foo/my.app

我也试过

 here="`dirname \"$0\"`"
   echo "cd-ing to $here"
   cd "$here" || exit 1

都不工作。

最后我需要调用my.command 来运行命令,但需要知道它的实际位置——甚至是相对于应用程序的位置,以便它会触发。目前我得到的错误是它找不到 my.command,因为它不在我的用户帐户的根目录中(因为我无法控制它可以放置在最终用户机器上的位置)。

任何关于我可以做些什么来解决这个问题的指针都非常感谢。

注意:要回答 - 为什么我使用具有终端脚本的应用程序来调用本质上是脚本的 .command - 基本上是因为如果你这样做,终端实际上​​不会弹出......这个演示是我需要做的。

【问题讨论】:

  • 为什么不直接使用 Run Shell Script action 并将 .command 文件中的 code 放在那里?
  • 您可以使用 Run AppleScript 操作来获取 NSBundle 的 bundlePath,但如上所述,您也可以使用 Run Shell Script 操作。跨度>
  • @user3439894 @red_menace 谢谢 - 我注意到我为什么要这样做,但为了参数的缘故,只是按要求执行 - 当我将以下代码放在自动机的 shell 脚本中时.. `` ` here="dirname \"$0\"" /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app=file://"$here"/mywebpage.htm ``` 存在同样的挑战.. @ 987654326@ 评估为 . 意味着 chrome 失败并出现错误..“不知道如何打开 file://./mywebpage.htm

标签: bash applescript automator


【解决方案1】:

由于您没有包含保存为 应用程序 的 Automator 工作流程 的明确详细信息,因此我将以下内容作为示例如何拥有和 Automator 应用程序,例如my.app,执行例如my.command 脚本文件,与例如位于同一文件夹中的文件。 my.app 是。

出于示例的目的,我在我的 Desktop 上创建了一个名为 foo 的 文件夹,其中 my.app 与 my.command 脚本文件 一起保存。

Automator 应用程序工作流使用Run AppleScript 动作来完成目标。

将默认的代码替换为以下示例 AppleScript 代码:

set myCommandFilename to "my.command"

set myAppPathAlias to path to me

tell application "System Events"
    set myDirName to POSIX path of container of myAppPathAlias
    set myCommandFilePathname to myDirName & "/" & myCommandFilename
    set myCommandFilenameExists to exists file myCommandFilePathname
end tell

if myCommandFilenameExists then
    try
        do shell script myCommandFilePathname's quoted form
    on error eStr number eNum
        display dialog eStr & " number " & eNum ¬
            buttons {"OK"} default button 1 ¬
            with title "File I/O Error..." with icon stop
    end try
else
    display dialog "A necessary file, ' " & myCommandFilePathname & ¬
        "', is missing!" buttons {"OK"} default button 1 ¬
        with title "Missing File..." with icon stop
end if
  • 注意:将my.command 更改为实际的文件名。 示例 AppleScript 代码 的其余部分不需要修改。

如果 my.app 启动并且 my.command 脚本文件 不在同一个文件夹 my.app,则会显示错误信息,例如:

如果 my.app 已启动并且 my.command 脚本文件 没有其可执行位 设置,则显示此错误信息,例如:

另外,如果 my.command 脚本文件 没有完全退出,它也会显示错误消息,例如:

  • 错误消息的内容将根据例如的内容而有所不同。 my.command 脚本文件,它是如何编码的以及它是如何失败的。这个例子是最坏的情况,它让你知道失败了,但不知道失败了。

注意:example AppleScript code 就是这样,不包含任何额外的error酌情处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2017-06-28
    • 1970-01-01
    • 2015-04-04
    • 2019-12-24
    • 2023-03-31
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多