本文介绍了Applescript 从同一目录运行 bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build an AppleScript to launch my shell script.

路径结构如下

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

我的 AppleScript 如下:

My AppleScript is as follows:

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 等于:

Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:

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

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

Is there is a better way of getting the folder path?

推荐答案

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

Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the "do shell script" command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.

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

Here is an example of an AppleScript that runs an inline shell script and puts the results in Text

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脚本.

… you can of course replace the above shell script with the contents of your own shell script.

如果您确实需要将脚本保存在一个单独的文件中,最好的方法可能是将您的 AppleScript 保存为一个应用程序,然后将 shell 脚本放在应用程序包中.我的路径"是运行脚本的应用程序的路径——不是脚本本身——但是如果你将你的 AppleScript 保存为一个应用程序,那么它会运行它自己的脚本,我的路径"就像你最初的那样工作预期.

If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. "Path to me" is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and "path to me" works as you originally expected.

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

Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:

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 中,导航到您保存应用程序的位置,用两指点击(或右键单击)您的应用程序并选择显示包内容".这会将您的应用程序作为文件夹打开,从而暴露其中的文件系统.将名为bash.sh"的 shell 脚本文件放在应用程序内的文件夹Contents/Resources/Scripts"中,然后关闭代表应用程序的窗口.

With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose "Application" and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose "Show Package Contents." That opens your application up as a folder, exposing the file system within. Put your shell script file named "bash.sh" inside the folder "Contents/Resources/Scripts" within your application and then close the window that represents your application.

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

Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.

这篇关于Applescript 从同一目录运行 bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:55