0

I have the following code

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

two problems with it! One: it opens a dialog box that is very long. So long that I cant even hit the ok to exit it. (I know i can hit the return key) Question, how do I stop the dialog box? Two: It never puts the text in the new textedit that it opens.

Cripto
  • 3,581
  • 7
  • 41
  • 65
  • What dialog box is it showing? The answer to two is easy, BTW: `set text of NewDoc to diff` – you have it the other way around :). – kopischke May 03 '12 at 08:16
  • the content of diff is showing up in a dialog box. It is because of the do shell script call. Its the shell output is being displayed in a dialog box – Cripto May 03 '12 at 09:47

3 Answers3

2

Your dialog box is not an output dialog, it is an error dialog. The problem is that diff exits with an error code if it finds differences (0 is no differences, 1 is differences, 2 is program error according to this question), Applescript considers this a failure of the do shell script command and helpfully displays the output for debugging, which of course contains the full diff. However, that is never assigned to your diff variable, as it triggered an error.

Assuming your shell is bash, doing the following

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

will solve the issue – you suppress the exit code 1 and AppleScript happily picks up the output on stdout and assigns it to your variable (note I added quotes to your file paths – you could also use quoted form of POSIX path). For this to be inserted into a new TextEdit document via AppleScript, you also have to invert your assignment as per my comment, i.e.

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

That should solve all issues.

Community
  • 1
  • 1
kopischke
  • 3,393
  • 1
  • 21
  • 40
0

When a do shell script shows a dialog the problem is that the shell command didn't return a 0. The cause of the error could be that you didn't use quoted form of. You can also pipe text to text editor.

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
dj bazzie wazzie
  • 3,472
  • 18
  • 23
  • Basically true, although the problem in this case is not lack of quoting, but the fact `diff` exits non-zero when it finds differences. Your code will still fail – try the one in [my answer](http://stackoverflow.com/a/10429862/990363) instead. – kopischke May 03 '12 at 10:51
-1

Here is another approach:

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
adayzdone
  • 11,120
  • 2
  • 20
  • 37