Tuesday, October 21, 2014

#1 2006-09-29 04:36:41 pm

fiftyfour123
Member
From: New York, NY
Registered: 2006-09-11
Posts: 388
Website

Check if file exists

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

Offline

 

#2 2006-09-29 05:07:17 pm

jobu
Member
From: phxaz
Registered: 2004-01-17
Posts: 908
Website

Re: Check if file 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.

Applescript:

try
   --> 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.

j


The saddest thing I ever did see was a woodpecker peckin' at a plastic tree.
- Shel Silverstein

Offline

 

#3 2006-09-29 06:23:12 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2649

Re: Check if file exists

You could also try using the `ls` command:

Applescript:

try
   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.

Offline

 

#4 2006-10-01 03:13:24 pm

Camelot
Member
From: San Jose, CA
Registered: 2002-12-14
Posts: 629

Re: Check if file exists

Or, ask the Finder:

Applescript:

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

Last edited by Camelot (2006-10-01 03:17:43 pm)


Filed under: Finder

Offline

 

#5 2006-10-01 07:51:57 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2649

Re: Check if file exists

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

Applescript:

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

display dialog msg


Filed under: Finder

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.031 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)