如何从mac automator应用程序启动.command脚本

问题描述 投票:0回答:1

我正在尝试使用自动程序创建Mac“ app”,该程序基本上会调用.command文件来完成所有工作。命令文件将与.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?-基本上是因为,如果以这种方式进行操作,终端实际上​​不会弹出..为此演示是我需要做的。

bash applescript automator
1个回答
0
投票

由于您未包含另存为应用程序Automator workflow的明确详细信息,因此,我以以下内容作为示例来说明如何实现和Automator应用程式,例如my.app,执行例如my.command 脚本文件,与例如my.app是。出于示例目的,我在

Desktop

上创建了一个名为foofolder,其中my.appmy.command 脚本文件

Automator

应用程序工作流程使用Run AppleScript action来实现目标。
用以下

示例

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更改为实际的文件名example AppleScript code的其余部分不需要修改。
  • Automator App

如果启动了[[my.app]],并且

my.command

脚本文件my.app不在同一个文件夹中,则会出现错误消息将会显示,例如:Missing File Error如果启动了

my.app

,并且没有设置

my.command

脚本文件,则将显示此错误消息,例如:
File  I/O Error
此外,如果my.command

脚本文件

没有干净退出,它也会显示错误消息,例如:

Dirty Exit错误消息的内容将根据例如

my.command

    脚本文件

,其编码方式和失败方式。此示例是最坏的情况,因为它使您知道失败的原因,而不是失败的原因。
  • 注:
    示例

    AppleScript

  • 代码仅此而已,并且不包含任何适当的其他错误处理。用户有责任添加任何适当的,需要的或想要的。看一下try中的error statementAppleScript Language Guide statement。另请参见Working with Errors
    © www.soinside.com 2019 - 2024. All rights reserved.