如何将复杂的AppleScript转换为终端的单行命令

时间:2010-08-09 13:40:43

标签: shell ant applescript

我有一个复杂的AppleScript,出于某些原因必须作为单行命令执行。我的脚本看起来像:

tell application "Finder"
    tell disk "'myDiskName'"
        open
        set current view of container window to icon view
        set toolbar visible of container window to false
        set statusbar visible of container window to false
        set the bounds of container window to {400, 100, 968, 421}
        close
        open
        eject
    end tell
end tell

我使用终端执行脚本:

echo '<SCRIPT>' | osascript

这是上面的多行脚本 - 并且工作得很好。现在,更具体地说,我希望使用ant-task运行此脚本,例如:

<exec executable="echo">
    <arg line="'<SCRIPT>' | osascript" />
</exec>

由于是多行的,它会以某种方式被忽略/不执行,但它也不会抛出异常。我看到两个解决方案:一个是单行命令,更可取,或者是一个被调用的独立的applecipt。事情就是这样:上面的脚本需要一些动态变量,这些变量必须在运行时从antscript生成 - 所以动态创建脚本可能不是一个选项。

2 个答案:

答案 0 :(得分:7)

我不确定什么是“蚂蚁任务”,但创造一个单行的方式就这样做......

/usr/bin/osascript -e "tell application \"Finder\"" -e "tell disk \"'myDiskName'\"" -e "open" -e...

换句话说,每一行前面都有一个“-e”,你想要引用这行。

答案 1 :(得分:6)

如果AppleScript应直接嵌入到Ant构建脚本中,那么最易读的解决方案是将脚本包装到CDATA部分。

然后,您可以定义一个Ant宏,通过其exec参数将脚本数据传递给inputstring任务:

<project name="AppleScript" default="applescript">

    <macrodef name="applescript">
        <text name="text.script" trim="false" optional="false" />
        <sequential>
            <exec executable="/usr/bin/osascript" inputstring="@{text.script}" />
        </sequential>
    </macrodef>

    <target name="applescript">
        <applescript>
            <![CDATA[
tell application "Finder"
    open startup disk
end tell
            ]]>
        </applescript>
    </target>

</project>