Create simple diff tool with AppleScript

I’m trying to create a simple diff tool with AS as a prelude to a longer set of tasks.

Here is what I have so far, but I’m stuck as to what I’m getting wrong:

#Choose files to work with

set file01 to choose file with prompt "First File:" default location path to desktop

set file02 to choose file with prompt "Second File:" default location path to desktop

do shell script "diff file01 file02"

This fails saying:

“diff: file01: No such file or directory
diff: file02: No such file or directory” number 2

I initially tried doing this with Automator, but it failed at the “Run Shell Script” stage every time and I just couldn’t figure out how to get it to diff the files and print the output to a new file.

If Automator is the better way to go, that’s fine, but I’ve really no idea how to make that work either (can supply app I created if that would help. Please just let me know where to post it).

Thanks,

bowjest

Hi,

both paths must be POSIX paths and the parameters must be the contents of the variable not the variable name


set file01 to quoted form of POSIX path of (choose file with prompt "First File:" default location path to desktop)
set file02 to quoted form of POSIX path of (choose file with prompt "Second File:" default location path to desktop)
do shell script "diff " & file01 & space & file02


Hi Stefan,

Thanks very much. This seems to be my perennial stumbling block. :frowning:

Is there an easy way to write the diff output to a new file? I suppose I would have to create a new file first?

Do I need to do something iike this?

tell application "Finder"
make new file at (path to desktop) with properties {name:file03}
end tell

I really don’t know how to get the diff output into a new, separate file so this can be worked on later.

Thanks,

bowjest

easiest solution


set file01 to quoted form of POSIX path of (choose file with prompt "First File:" default location path to desktop)
set file02 to quoted form of POSIX path of (choose file with prompt "Second File:" default location path to desktop)
set file03 to quoted form of POSIX path of ((choose file name with prompt "Result File:" default location path to desktop) as text)
do shell script "diff " & file01 & space & file02 & " > " & file03


Thanks, Stefan. As always, a perfect answer.

Appreciated,

bowjest

Stefan,

I’ve hit a brick wall here, unfortunately.

The reason for the script is that only those differences between two files are needed for further textual analysis. Using the diff script that we’ve been discussing, this puts in a lot of extra characters and does not produce a unique list.

Here is an example of what I get when I run it on two large files:

3d2
< to
4a4

to
6,9c6,7
< i
< you
< it
< that


her
was
11a10,12
that
she
s
13c14,15
< was


had
i
15,18d16

Is there perhaps a way to modify the script such that it can be run from within TextWrangler, give unique result values and not include the extra characters?

Thanks for your help and I’m sorry to be a pain.

Regards,

bowjest

I’m not familiar with diff, please look at the man page, there are a lot of parameters
or search google for examples or tutorials of diff

Thanks. I’ll do that.

bowjest

Hello!

It is late, but maybe this script by Simon Tãnner may help you! Look in post #6 here!

Thanks, McUsr,

I’ll read through that later today.

I appreciate you letting me know.

Regards,

Bowjest

" Is there perhaps a way to modify the script such that it can be run from within TextWrangler, give unique result values and not include the extra characters?"

See “process duplicates lines” and “find differences”
You can also grep with either AppleScript within TextWrangler, or using directly the “Find” with grep and replace with nothing

I’ll be interested to -try- to continue in the same the subject,
I was looking for a tool to compare different text files and remove the duplicates line, without having to sort the files.
I am using fairly large files and TextWrangler chokes to death.

The command uniq needs a previous sort and doesn’t like non English characters (export LC_ALL=‘C’ ), awk can do it, but within a single file , i.e: awk ’ !x[$0]++’ input.txt > output.txt

All I can think of would be a loop to compare a line to lineS, but it may take a lifetime to grep then delete if equal.

Does anyone has a clue on the subject?

Thanks

I’d Zap gremlins, if special characters is what you mean, in TextWrangler.

Maybe BBEdit would be something for you, as there you have worksheets, that you can set up to process files with.

I think it is a 30-day free trial! :slight_smile:

You can of course always write an applescript and process the files paragraph by paragraph, excluding duplicate lines from both, but it mgiht run for hours I believe, if your input isn’t sorted!

McUsr

Alas, I need some of the the Græmlins, can’t zap 'em! (completely)
I wish everybody was speaking Swiss or Canadian, but that’s not the case.

when I say big files, I mean “BIG FILES”
BBedit is DOA, and AWK is making trouble with huge swap size: slooooow
I’ll try a workaround as split | xargs | awk, and if not working I’ll try py.
After that, I’ll drop the ball …

Thanks for your input

Ha det bra!