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

发布于 2024-09-13 03:56:32 字数 870 浏览 4 评论 0原文

我有一个复杂的 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>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-09-20 03:56:32

我不确定“蚂蚁任务”是什么,但要创建一个单行代码,请这样做...

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

换句话说,每一行前面都有一个“-e”,并且您希望引用该行。

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.

糖粟与秋泊 2024-09-20 03:56:32

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

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

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文