【问题标题】:How can I convert bash's command substitution and pipe to an Applescript?如何将 bash 的命令替换和管道转换为 Applescript?
【发布时间】:2011-11-07 03:04:07
【问题描述】:

我需要帮助将这个简单的 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?

【问题讨论】:

    标签: bash shell terminal applescript sh


    【解决方案1】:

    我(现在)看到的唯一问题是将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
    

    【讨论】:

    • 记得使用quoted form of posix 路径
    • @Flambino 哇,我没看到。谢谢,我会修复它。 :D
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-07
      • 2011-01-23
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多