Testing for condition prior to action....

Hi, I am having a bizzare issue. I want to test for the presence of a file, in this case, an external command the end user of my program would have to download and place inside the contents/resources folder of the ASS app, once it’s compiled. Because of that situation, I would like to test for the presence of the binary with the following code:


on awake from nib theObject
if the name of theObject is "disk" then
		set isdebug to quoted form of (POSIX path of (path to me)) & "Contents/Resources"
		set areuthere to do shell script "ls " & isdebug & "|grep -c externalcommand"
		if areuthere is greater than 0 then
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is properly installed."
		else
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is not properly installed."
		end if
	end if
end awake from nib

With the command in the Contents/Resources folder, this works as expected. The message “externalcommand is properly installed” shows up. The presense of the file is tested with the grep shell command with the “c” flag, which counts the number of instances of the file… so it’s going to be 0 or 1

But if the command isn’t there, I get the following error:

AppleScript Error
0(1)

what gives? There are many, many, conditions I’d like to test with this method!

Hey, Dean. :wink:

This should do the trick for you if you need to see if that file exists:

on awake from nib theObject
	if the name of theObject is "disk" then
		try
			set extTest to (path to me) & ":Contents:Resources:externalcommand" as alias
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is properly installed."
		on error theError
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is not properly installed."
		end try
	end if
end awake from nib

As for your testing method, can you provide some examples of things you’d want to use the method for testing?

Thanks Mikey-San,

this works with one small change (see below) but I knew I could do it this way. I really need to be able to do it using grep -c. The rationale is that I can check for the presense of any file in any directory, check even the cotents of certain configuration files or plists to determine if a something should be enabled/disabled. I have a feeling that the

AppleScript Error
0(1)

is related to get a “0” as the result of the setting the variable. For some reason Applescript doesn’t like this, I would like to know if a workaround exists…

works…

on awake from nib theObject
   if the name of theObject is "disk" then
       try
           set extTest to (path to me) & "Contents:Resources:externalcommand" as string
	  set extest2 to extTest as alias
           set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is properly installed."
       on error theError
           set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is not properly installed."
       end try
   end if
end awake from nib
on awake from nib theObject
	if the name of theObject is "disk" then
		set isdebug to quoted form of (POSIX path of (path to me)) & "Contents/Resources"
		try
			set areuthere to do shell script "ls " & isdebug & "|grep -c externalcommand"
		on error
			set areuthere to 0
		end
		if areuthere is greater than 0 then
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is properly installed."
		else
			set contents of text view "hftext" of scroll view "output" of window "thewindow" to "externalcommand is not properly installed."
		end if
	end if
end awake from nib

I think that will work.

For what it’s worth, when shell scripts return an error, AppleScript returns an error too, so you need to catch it.