在 Mac 上快速、便携地启动 perl 脚本(相当于 Win 上的 Batch)
我在存储库中与 Mac 和 Windows 计算机上的多个用户共享了一个 perl 脚本。它对所在文件夹中的文件执行工作。因为我经常运行它,而且有些用户甚至不知道命令行是什么,所以我在目录中创建了一个批处理文件以方便启动。因为某些路径会有所不同,所以我使其可移植:
::figure out your directory path
pushd %~dp0
:: launch the script
perl my_perl_script.pl
popd
该批处理在 Windows 上完美运行,我将其与每个 Perl 脚本一起放在每个项目文件夹中,并告诉我的用户只需“双击那个东西”。
我怎样才能在 Mac 上做同样的事情?我对 bash 了解不多,我不知道如何
1) 让 bash 文件找出它的目录路径,并且
2) 为我的 Mac 用户使 bash 成为一个简单的“双击那个东西”可执行文件。
一个简单的 Applescript 会更好吗(我也不太了解 Applescript...)
感谢您的帮助。
编辑:谢谢,dj bazzie wazzie。我不想同时运行 bash 和 applescript 来运行我的 perl,但我确实使用了你的第一行来获得一个非常适合我的 applescript。
set currentWorkingDirectory to do shell script "dirname " & quoted form of POSIX path of (path to me)
tell application "Terminal"
set currentTab to do script "cd " & currentWorkingDirectory
do script "perl xml2epub_3689_7KeysSpWellness.pl" in currentTab
end tell
I have a perl script shared in a repository with several users across Mac and Windows machines. It performs its work on files located in the folder with it. Because I run it frequently and because some users don't even know what a command line is, I created a batch file in the directory to make it easy to launch. Because some paths will differ, I made it portable:
::figure out your directory path
pushd %~dp0
:: launch the script
perl my_perl_script.pl
popd
The batch works perfectly on Windows and I put it in each project folder with each perl script and tell my users to just "double-click that thingy."
How can I do the same on Mac? I don't know much about bash, and I can't figure out how to
1) Have a bash file figure out its directory path, and
2) Make the bash a simple "double-click that thingy" executable for my Mac users.
Would a simple Applescript be better (I don't really know Applescript either...)
Thanks for any help.
EDIT: Thanks, dj bazzie wazzie. I didn't want to run both a bash and applescript to get my perl to run, but I did use your first line to get an applescript that works perfectly for me.
set currentWorkingDirectory to do shell script "dirname " & quoted form of POSIX path of (path to me)
tell application "Terminal"
set currentTab to do script "cd " & currentWorkingDirectory
do script "perl xml2epub_3689_7KeysSpWellness.pl" in currentTab
end tell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 AppleScript,您可以运行 shell 命令。假设您的 bash 文件位于脚本旁边,名为 Maintenance.sh 您的脚本看起来像这样
编辑:(我无法评论您的帖子,所以我更改了我的帖子)
对于像您这样简单的命令,您不需要终端应用程序。我可以理解,一个工作应用程序就足够了,但对于更流畅的脚本,我会将该命令放入 do shell 脚本中。所以你的每个命令的代码将是这样的
With AppleScript you have the ability run a shell command. Let's say your bash file is located next to the script and is named maintenance.sh your script would look something like this
EDIT: (I can't comment your post so I change my post)
For such a simple command as yours you don't need the terminal application. I can understand that a working application is enough but for a much smoother script I would put that command into a do shell script. So the code for your per command would be something like this