【问题标题】:Applescript run bash script from same directoryApplescript 从同一目录运行 bash 脚本
【发布时间】:2014-07-23 18:47:24
【问题描述】:

我正在尝试构建一个 AppleScript 来启动我的 shell 脚本。

路径结构如下

/Users/ryan/myscript/
    applescript.scpt
    bash.sh

我的AppleScript如下:

tell application "Terminal"
          set folder_path to path to me
          set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
          do script run_cmd
          activate
end tell

问题是“我的路径”并不总是返回正确的路径。使用Mac执行时cd/dvd自动播放行为folder_path等于:

disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:

有没有更好的获取文件夹路径的方法?

【问题讨论】:

  • 将脚本保存为应用程序有影响吗?

标签: macos bash applescript


【解决方案1】:

我的路径是指正在运行的 AppleScript 的位置。因此,如果您的脚本在磁盘上,那么它将引用磁盘上保存脚本的位置

如果预计 shell 脚本将始终存在于当前用户文件夹中名为“myscripts”的文件夹中,那么您可以使用当前用户文件夹的路径并从那里构建

set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))

tell application "Terminal"
    activate
    set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
    do script run_cmd
end tell

【讨论】:

  • 好吧,在我的 mac 上,“我的路径”并不总是等于 applescript 所在的路径。不幸的是,我需要它来运行相对于 applescript 路径的 bash.sh
  • 你有没有试过把我从终端告诉块带出来?
  • 没有任何区别,它仍然引用 Apple CoreServices 系统文件夹
【解决方案2】:

如果此脚本位于静态位置,您可以这样做:

do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"

【讨论】:

  • 除了是一行之外,这与我发布的内容有何不同?
  • 这是运行无需通过终端应用程序激活等。为了干净的脚本,我将你的 7 行 260 个字符压缩成一行 94 个字符。
【解决方案3】:

是否有理由将 shell 脚本存储在单独的文件中?通常,您会将其内嵌在 AppleScript 代码中。据我所知,“do shell script”命令只作用于文本,而不作用于文件路径的脚本。如果你给它一个包含路径的变量,它会尝试将该路径作为命令运行。它不会将文件的内容作为命令运行。

这是一个运行内联 shell 脚本并将结果放入 TextEdit 的 AppleScript 示例:

property theShellScript : "#!/bin/bash
echo Hello World"

tell application "TextEdit"
    activate
    set theScriptResult to do shell script theShellScript
    make new document
    set the text of document 1 to theScriptResult
end tell

……你当然可以将上面的shell脚本替换成你自己的shell脚本的内容。

如果您确实需要将脚本保存在单独的文件中,最好的方法可能是将您的 AppleScript 保存为应用程序,并将 shell 脚本放入应用程序包中。 “我的路径”是运行脚本的应用程序的路径 - 不是脚本本身 - 但是如果您将 AppleScript 保存为应用程序,那么它会运行自己的脚本,并且“我的路径”就像您最初一样工作预计。

下面是一个 AppleScript 示例,它运行包含在一个文件中的 shell 脚本,该文件存储在它自己的应用程序包中:

property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"

tell application "TextEdit"
    open alias theShellScriptPath
    set theShellScript to the text of document 1
    set theScriptResult to do shell script theShellScript
    make new document
    set the text of document 1 to theScriptResult
end tell

将上述脚本复制/粘贴到 AppleScript Editor 中的新文档中,按住 Option 键并选择文件 ▶ 另存为,然后在保存对话框中,在文件格式弹出菜单中,选择“应用程序”并当然给你的应用程序一个名字,然后点击保存。然后在 Finder 中,导航到您保存应用程序的位置,用 2 指点击(或右键单击)您的应用程序并选择“显示包内容”。这会将您的应用程序作为文件夹打开,从而暴露其中的文件系统。将名为“bash.sh”的 shell 脚本文件放在应用程序的“Contents/Resources/Scripts”文件夹中,然后关闭代表应用程序的窗口。

现在,当您从文件系统中的任何位置运行应用程序时,它仍然能够找到并运行其合并的 shell 脚本。

【讨论】:

  • 西蒙,第二部分或您的答案可能是 OP 需要的,但这只是一个猜测。至于将 bash 脚本存储在单独的文件中,我猜想它很长,而且混合两种语言可能非常困难。那是我的两分钱;)
猜你喜欢
  • 2016-10-14
  • 1970-01-01
  • 2019-06-26
  • 2014-06-30
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
相关资源
最近更新 更多