加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:52:14 所属栏目:安全 来源:网络整理
导读:我有一个复杂的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 st
我有一个复杂的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生成 – 因此动态创建脚本可能不是一个选项.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读