Applescript to compare files


I am using apple script in Automator to find out if there is a diff in my file. I know how to do this in applescript by using “do shell script”. However, I am trying to completely move away from using shell at all. Is there any way I can do that?

I have searched a lot of places but couldnt find any solution. So far this fourm, I got all my answers :slight_smile: (So I am really hoping someone would have a solution for me!!)

Thanks in advance for anyone who will look into this!



shell does many things better and faster than applescript. I guess, this one too.
you can size like this
set ifile to “Leopard:Users:chris:Desktop:r scripts.scpt”
set x to size of (info for file ifile)
then you compare sizes.

also, set x to (info for file ifile)
you can see other info

Thanks! Chris,

I am trying to verify multiple files at once.

I have a test case where I am having to verify if all the files in an expected folder matches with the files in another folder(s).

I was doing this with do shell script. everything was working fine until i upgraded to 10.6.1. The same script works fine for 10.5.8 or even 10.6 :frowning:

So trying to find a more universal ways of getting this done.

Any suggestions?

A shell script is probably the best way to do it. You might check file size in AppleScript, but that won’t necessarily work. You might also try comparing that data in the file by reading them into variables, but that could be a time consuming process and might now work for all file types.

Yeah, I have spent pretty much all day today trying to find whats wrong with my shell code. I can run the exact code in my 10.5.7, 10.5.8, and 10.6 As soon as I run the code in 10.6.1, the code doesnt work.

Here is the code I have, if anyone can help me fix it so that it works on both 10.6.1 and rest that would make my day (weekend!).



#Set Variable

export eMMC=“No_eMMC_Found”
export SD=“No_SD_Found”
export user=$(whoami)
export path=/Users/$user/Desktop/Test_Results_Detail.txt
export path_summery=/Users/$user/Desktop/Test_Results.txt

export title=“16.10 Compare song’s order on the device”
export Expected=“/Users/administrator/Desktop/Media_Sync/16 Media Sync - Transfer of Music/16.10 Compare song’s order on the device/Expected”

export failed_result=‘/Volumes/SV&V_Mac_Desktop/Automation/Mac_Desktop/Detail_Results/failed_result.txt’
export failed_data=“/Volumes/SV&V_Mac_Desktop/Automation/Mac_Desktop/Detail_Results/$title”

Scrool below to modify acutal folder if Needed

#Find which one is SD card and which one is eMMC - This also finds out the Volume name (ie. BLACKBERRY1 or BLACKBERRY2… and such)

if [ -e “/Volumes/BLACKBERRY/appdata/rim/” ]
elif [ -e “/Volumes/BLACKBERRY/” ]

if [ -e “/Volumes/BLACKBERRY1/appdata/rim/” ]
elif [ -e “/Volumes/BLACKBERRY1/” ]

if [ -e “/Volumes/BLACKBERRY2/appdata/rim/” ]
elif [ -e “/Volumes/BLACKBERRY2/” ]

if [ -e “/Volumes/BLACKBERRY3/appdata/rim/” ]
elif [ -e “/Volumes/BLACKBERRY3/” ]

if [ -e “/Volumes/BLACKBERRY4/appdata/rim/” ]
elif [ -e “/Volumes/BLACKBERRY3/” ]

#Find the difference between two folders

echo $SD
echo $eMMC
export Actual=“/Volumes/$SD/BlackBerry/music/Media Sync/”

find “$Actual” . -name “.DS” -type f -exec rm -rf {} ;
find “$Expected” . -name “.DS” -type f -exec rm -rf {} ;
find “$Actual” . -name “.dat" -type f -exec rm -rf {} ;
find “$Expected” . -name "
.dat” -type f -exec rm -rf {} ;

echo $SD
echo $eMMC

echo “$Actual”
echo “$Expected”

diff -r -q “$Actual” “$Expected”

diff -r “$Actual” “$Expected” &> /dev/null
if [ $? -eq 0 ]

time stamp the log file

echo -n date "+%Y-%m-%d @ %H:%M:%S" >> “$path”

echo " PASSED $title" >> “$path”
echo “$title-Passed” >> “$path_summery”

echo passed
echo “--------------------------------------------------------------------------------------------------------------------” >> “$path”

time stamp the log file

echo -n date "+%Y-%m-%d @ %H:%M:%S" >> “$path”

echo " FAILED $title" >> “$path”
echo “$title-Failed” >> “$path_summery”
diff -r -q “$Actual” “$Expected” >> “$path”
echo failed

echo “—” >> “$failed_result”
echo “$title” >> “$failed_result”
mkdir “$failed_data”
cp -r -f “$Actual” “$failed_data”
diff -r -q “$Actual” “$Expected” >> “$failed_result”

echo “--------------------------------------------------------------------------------------------------------------------” >> “$path”

#open “$path”

It is good to see the shell code, but it would also help if you could be more specific no how it actually fails to work as you expect.

I read through the shell code, but the only thing that popped out at me was the change from BLACKBERRY4 to BLACKBERRY3 in the middle of the fourth volume mount point probing stanza. That seems like it could cause a problem, but it might not be the kind of failure that you are actually seeing.

Hi Chris,

Thanks for the reply. Sorry for the delayed reply (weekend break). Basically when I connect the BB, sometimes it can have a built in memory (eMMC), at that point, the Mac will automatically call it BLACKBERRY and any additional SD card I have it will call it BLACKBERRY1 and so on. I have also noticed that if you unplugh and plug in quick enough a lot of times the Volume does not unmount fast enough so for those cases, it gets renamed to BLACKBERRY#.

To explain “it doesnt work”, meaning on my reporting, file I only see “FAILED and the name of test case”. It doesnt give me reason why it failed (like it does for all other OS).

I have done some troubleshooting, I am thinking this is more of a 10.6.1 bug. I run the exact same test cases in 10.5.8 or even 10.6.0 - everything works fine.

I stipped out all the code to see if a folder exists then say folder exists and if not otherwise. These shell scripts are ran in Automator’s shell script option. If i run the same code in terminal, it works fine.

export Expected=“/Users/administrator/Desktop/Media_Sync/16 Media Sync - Transfer of Music/16.10 Compare song’s order on the device/Expected”

The most crazy thing is, if I run the same code case 10 times, 3-4 times it will report that folder exists and other time it says it doesnt exists.

Any idea how else I can get around this? I may have to rewrite the whole thing in apple script (which will be a painful additional task).

Thanks in advance for looking into it.


So, your ~/Desktop/Test_Results_Detail.txt file gets a timestamp, a FAILED line and then no output from the diff -r -q? Try using diff -r -q “$Actual” “$Expected” 2>&1 >> “$path” (add “2>&1” before the stdout redirection) to also capture the stderr to the file. Also maybe add echo “diff -r -q exit code: $?” >> “$path” right after that. If, diff is encountering some kind of error (named file/directory does not exist), it will exit non-zero, and may not produce any output on stdout, but there will probably be something on stderr.