【问题标题】:How to prevent the automatic dialog box in applescript?如何防止applescript中的自动对话框?
【发布时间】:2012-05-03 07:08:29
【问题描述】:

我有以下代码

on open the_Droppings


    -- set something to {item 1 of the_Droppings, item 2 of the_Droppings}
    set file1 to POSIX path of item 1 of the_Droppings
    set file2 to POSIX path of item 2 of the_Droppings
    set diff to (do shell script "diff " & file1 & " " & file2)
    if (diff is equal to "") then
        display dialog "files are the same"
    else
        set diff to ""
        tell application "TextEdit"
            activate
            set NewDoc to make new document
            set diff to text of NewDoc
        end tell

    end if
end open
end

有两个问题! 一:它打开一个很长的对话框。这么久,我什至无法点击确定退出它。 (我知道我可以按回车键) 问题,如何停止对话框? 二:它永远不会将文本放入它打开的新文本编辑器中。

【问题讨论】:

  • 它显示的是什么对话框?两个的答案很简单,顺便说一句:set text of NewDoc to diff - 你有相反的方式:)。
  • diff 的内容显示在对话框中。这是因为 do shell 脚本调用。它的外壳输出正在对话框中显示

标签: macos shell applescript


【解决方案1】:

您的对话框不是输出对话框,它是错误对话框。问题是diff 如果发现差异(0 是没有差异,1 是差异,2 是根据this question 的程序错误),则以错误代码退出,Applescript 认为这是 do shell script 命令的失败并且有帮助显示调试输出,其中当然包含完整的差异。但是,它永远不会分配给您的 diff 变量,因为它触发了错误。

假设你的 shell 是bash,执行以下操作

set diff to (do shell script "diff '" & file1 & "' '" & file2 & "'; [[ $? = 1 ]] && exit 0")

将解决这个问题——你取消退出代码 1,AppleScript 会愉快地获取stdout 上的输出并将其分配给你的变量(注意我在你的文件路径中添加了引号——你也可以使用quoted form of POSIX path)。要通过 AppleScript 将其插入到新的 TextEdit 文档中,您还必须反转分配 as per my comment,即

set text of NewDoc to diff -- not "set diff to text of NewDoc"

这应该可以解决所有问题。

【讨论】:

    【解决方案2】:

    当 do shell 脚本显示对话框时,问题是 shell 命令没有返回 0。错误的原因可能是您没有使用 引用形式的。您还可以将文本通过管道传输到文本编辑器。

    on open these_Items
        set file1 to quoted form of POSIX path of item 1 of these_Items
        set file2 to quoted form of POSIX path of item 2 of these_Items
        do shell script "diff " & file1 & " " & file2 & " | open -f -e"
    end open
    

    【讨论】:

    • 基本正确,虽然这种情况下的问题不是缺少引用,而是diff 发现差异时退出非零这一事实。您的代码仍然会失败 - 请尝试使用 my answer 中的代码。
    【解决方案3】:

    这是另一种方法:

    set resultsPath to "~/Desktop/results.txt"
    
    try
        do shell script "diff " & file1 & space & file2 & " >" & resultsPath
    end try
    
    set yyy to do shell script "cat " & resultsPath
    
    if yyy is "" then
        display dialog "files are the same"
    else
        do shell script "open " & resultsPath
    end if
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      相关资源
      最近更新 更多