Check if file exists

how do i make an applescript check to see if a file in usr/bin/ exists?

You can use a try block to catch errors generated by code that is likely to cause errors. In most cases, the try block will handle the error on behalf of the application and you can grab the error and do with it what you wish.

	--> error Causing Code HERE
on error errorMessage number errorNumber
	set _error to errorMessage
	set _errorNum to errorNumber
end try

set content of myTextField to ("(" & _errorNum & ") " & _error) as string

If you don’t mind using finder for testing file existence, it has a built-in “exists” command you can use to test for your file. You can also use creative methods like getting the info for a file (standard additions), where you know if you get a result it exists. There are also obj-c methods you could use i the NSFileManager class, such as “fileExistsAtPath” that could do this too.


You could also try using the ls command:

	do shell script "/bin/ls /usr/bin/blah"
on error errMsg number errNum
	-- handle any errors
end try

Edit: Separated posts; Asking unrelated questions makes it harder to search.

Or, ask the Finder:

set msg to "no"
tell application "Finder"
	if exists POSIX file "/usr/bin/gzip" then
		set msg to "yes"
	end if
end tell

display dialog msg

FWIW, that can fit on one line. (Also, changed so it doesn’t get the path until runtime.)

set msg to "no"
tell application "Finder" to if exists "/usr/bin/gzip" as POSIX file then set msg to "yes"

display dialog msg