Applescript 从同一目录运行 bash 脚本

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

我正在尝试构建一个 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
4个回答
2
投票

如果这个脚本在静态位置,你可以这样做:

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

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

1
投票

为什么必须将 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 编辑器中的新文档中,按住 Option 键并选择文件 ▶ 另存为,然后在保存对话框的文件格式弹出菜单中选择“应用程序”,当然为您的应用程序命名并单击保存。然后在 Finder 中,导航到您保存应用程序的位置,用 2 指点击(或右键单击)您的应用程序并选择“显示包内容”。这会将您的应用程序打开为一个文件夹,从而暴露其中的文件系统。将名为“bash.sh”的 shell 脚本文件放入应用程序的“Contents/Resources/Scripts”文件夹中,然后关闭代表应用程序的窗口。

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


0
投票

用户在某个 Finder 文件夹中是什么意思? 这意味着它在显示某个文件夹或桌面的前面的 Finder 窗口中。

即需要确定前台Finder窗口目标文件夹的Posix路径,传递给Terminal应用程序。

tell application "Finder"
    if (count windows) = 0 then
        set aTarg to desktop as alias
    else
        tell front window to set aTarg to target as alias
    end if
end tell

tell application "Terminal" to if (count (windows whose visible is true)) = 0 then
    do script "cd " & quoted form of (POSIX path of aTarg)
else
    do script "cd " & quoted form of (POSIX path of aTarg) in front window
end if
© www.soinside.com 2019 - 2024. All rights reserved.