如何将bash的命令替换和管道转换为Applescript?

时间:2011-08-30 20:06:47

标签: bash shell terminal applescript sh

我需要帮助将这个简单的shell脚本转换为苹果脚本。

关键是因为它将在Automator工作流程中使用,因此我需要打开终端窗口,这是使用shell脚本无法完成的。

shell脚本如下:

java -classpath `dirname "$1"` `basename "$1" | sed "s/.class//g"`

这将获取文件的位置,然后获取文件的名称,然后删除“.class”的文件扩展名,然后使用Java命令运行它。例如,它将生成以下命令:

java -classpath /users/desktop/ filename

我需要转换此命令,以便它可以与Applescript一起使用,以便我可以看到在终端窗口中运行的应用程序。它将从以下开始:

on run {input, parameters}
    tell application "Terminal"
        activate
        do shell script "java -classpath path/to/ file"
    end tell
end run

如何将文本转换移植到Applescript?

2 个答案:

答案 0 :(得分:2)

我现在看到的唯一问题是将do shell script更改为do script。除此之外,你已经正确地开始了。我假设您要将(a)文件引用传递给shell脚本。这很简单......

set these_files to (choose file with multiple selections allowed)
repeat with this_file in these_files
    tell application "Finder" to if the name extension of this_file is "class" then my do_shell_script(this_file)
end repeat

on do_shell_script(this_file)
    tell application "Terminal" to activate --makes 'Terminal' the frontmost application
    --'do shell script ...' goes here
    --To refer to a file/folder for a 'do shell script', do something like the command below...
    --do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of this_file
end do_shell_script

答案 1 :(得分:-1)

我根本不懂AppleScript,但我想你可以简单地在do shell script行中调用你现有的shell脚本,而不是试图重做AppleScript中的字符串操作。

注意:
看起来您希望能够通过单击(或拖放等)来调用Java类。您的方法仅适用于匿名包中的类,即源代码开头没有任何package ...;声明。对于其他人,您可能会尝试找出他们是哪个包。我不知道该怎么做。无论如何,可分发程序应该在jar档案中,您应该可以使用java -jar ...启动它。)