有,爲什麼你有一個理由將shell腳本存儲在單獨的文件中?通常情況下,您可以將它內聯在AppleScript代碼中。據我所知,「do shell腳本」命令只能對文本進行操作,而不能對文件路徑上的腳本進行操作。如果給它一個包含路徑的變量,它將嘗試將該路徑作爲命令運行。它不會將該文件的內容作爲命令運行。
這裏是運行一個內嵌的shell腳本,並把結果在文本編輯一個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腳本放在Application捆綁包中。 「我的路徑」是運行腳本的應用程序的路徑 - 而不是腳本本身 - 但是如果您將AppleScript保存爲應用程序,那麼它會運行自己的腳本,並且「我的路徑」與您最初一樣工作預期。
這裏是運行包含在存儲它自己的應用程序包內的文件內的外殼腳本一個AppleScript的例子:
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中,導航到您保存應用程序的位置,並在應用程序中雙擊(或右鍵單擊),然後選擇「顯示軟件包內容」。這會將您的應用程序作爲文件夾打開,從而將文件系統暴露在內。把你的shell腳本文件命名爲「bash」。sh「放入應用程序內的」Contents/Resources/Scripts「文件夾內,然後關閉代表您的應用程序的窗口。
現在,當您從文件系統中的任何位置運行應用程序時,它仍然能夠查找並運行其合併的shell腳本。
將腳本保存爲應用程序是否有所作爲? – Vic