Applescript 从同一目录运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24918497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applescript run bash script from same directory
提问by Ryan
I'm trying to build an AppleScript to launch my shell script.
我正在尝试构建一个 AppleScript 来启动我的 shell 脚本。
Path structure is as follows
路径结构如下
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
我的 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
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:
问题是“我的路径”并不总是返回正确的路径。当使用 Maccd/dvd
自动播放时,行为folder_path
等于:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
有没有更好的方法来获取文件夹路径?
回答by mcgrailm
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
我的路径是指正在运行的applescript 的位置。因此,如果您的脚本在磁盘上,那么它将引用磁盘上保存脚本的位置
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
如果预计 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
回答by Simon White
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 脚本存储在单独的文件中?通常,您会将其内联放置在 AppleScript 代码中。据我所知,“do shell script”命令只对文本进行操作,而不对文件路径中的脚本进行操作。如果给它一个包含路径的变量,它会尝试将该路径作为命令运行。它不会将文件内容作为命令运行。
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
这是运行内联 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
… you can of course replace the above shell script with the contents of your own shell script.
...您当然可以用您自己的 shell 脚本的内容替换上面的 shell 脚本。
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 脚本放在应用程序包中。“我的路径”是运行脚本的应用程序的路径——不是脚本本身——但是如果你将你的 AppleScript 保存为一个应用程序,那么它会运行它自己的脚本,“我的路径”就像你最初的那样工作预期的。
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
下面是一个 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
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.
将上述脚本复制/粘贴到 AppleScript 编辑器中的新文档中,按住 Option 键并选择 File ? 另存为,在保存对话框中,在文件格式弹出菜单上,选择“应用程序”,当然给你的应用程序一个名字,然后点击保存。然后在 Finder 中,导航到您保存应用程序的位置,用两指点击(或右键单击)您的应用程序并选择“显示包内容”。这会将您的应用程序作为文件夹打开,从而暴露其中的文件系统。将名为“bash.sh”的 shell 脚本文件放在应用程序中的文件夹“Contents/Resources/Scripts”中,然后关闭代表应用程序的窗口。
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.
现在,当您从文件系统的任何位置运行您的应用程序时,它仍然能够找到并运行其合并的 shell 脚本。
回答by double_j
If this Script is in a static location, you can do this:
如果此脚本位于静态位置,则可以执行以下操作:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"