views:

126

answers:

2

I have a complex AppleScript that for some reasons has to be executed as a single line command. My Script looks like:

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

I do execute the script using terminal by:

echo '<SCRIPT>' | osascript

where the is the multiline script above - and that works absolutely fine. Now, to be more specific, I want this script to be run using an ant-task, like:

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

Since is multiline, it somehow gets ignored / not executed, but it doesn't throw an exception either. I see two solutions: either a single line command, which is preferable, or a standalone applescipt that gets called. Here's the thing: the script above needs some dynamic variables, that have to be generated from the antscript on runtime - so creating the script on the fly might not be an option.

A: 

I'm not sure what an "ant-task" is but to create a one-liner do it this way...

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

In other words, every line gets a "-e" in front of it and you want the line quoted.

regulus6633
I tried this, but it failed and was totally unreadable.
gamma
+5  A: 

If the AppleScript should be embedded directly into the Ant build script, the most readable solution is to wrap the script into a CDATA section.

You can then define an Ant macro which passes the script data to the exec task via its inputstring parameter:

<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>
sakra
I like that answer very much. It is easy, reusable and semantically very nice. Thanks.
gamma