2011-08-30 101 views
0

我需要帮助将这个简单的shell脚本转换为苹果脚本。如何将bash的命令替换和管道转换为Applescript?

重点是因为它要在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

我看到的唯一问题(现在)是将do shell script更改为do script。除此之外,你已经开始正确。我假设你想将(一个)文件引用传递给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

记得使用引用形式的posix路径 – Flambino

+0

@Flambino WOW我没有看到。谢谢,我会解决它。 :d – fireshadow52

-1

我不知道的AppleScript所有,但我想你可以简单地调用现有的shell脚本在do shell script线,而不是试图重做的AppleScript字符串处理。

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