check if file exists on network homes

I’m working on an applescript and need to know if a plist file exists. Looking through the forum, I found this thread (check if file exists) from 2006 that was helpful to a degree, but since we are using network homes the full path to the file is not known…

Perhaps I am going about this the wrong way… Here is my modified script from Bruce Phillips response in the older thread:

set msg to "no"
tell application "Finder" to if exists "~/Library/Preferences/edu.labLaunch.plist" as POSIX file then set msg to "yes"

display dialog msg

It doesn’t appear that the script is able to follow the ~, so in an attempt to create the full path to the file, I tried this:

set currentUser to do shell script "whoami"
set currentHome to do shell script "/usr/bin/dscl localhost -read /Search/Users/" & currentUser & " homeDirectory | cut -d ' ' -f 2"
set msg to "no"
tell application "Finder" to if exists "" & currentHome & "/Library/Preferences/edu.labLaunch.plist" as POSIX file then set msg to "yes"

display dialog msg

This returns the following error message: “Finder got an error: Can’t get POSIX file "".”

Perhaps I’m missing something obvious?

To try to work around it, I tried the following code:

set plistExist to do shell script "ls -alF ~/Library/Preferences/ | grep -c edu.labLaunch.plist"

display dialog plistExist

If the file does not exist it should return a value of 0. If the file does exist then the value should be 1. However when I run it I get an Applescript Error: 0. According to the Applescript Language Guide, Applescript Error: 0 is no error. Not sure what that means…

Certain folders have a special meaning to applescript and as such you can use them. You can get a list of those folders by looking at the “path to” command in the standard additions dictionary. The user’s preferences folder is one such folder.

set prefFolderPath to path to preferences folder as text
set prefFileName to "com.apple.mail.plist"
set prefFilePath to prefFolderPath & prefFileName

tell application "Finder"
	if (exists file prefFilePath) then
		display dialog "yes"
	else
		display dialog "no"
	end if
end tell

One other thing regarding your errors. I notice that your “if exists” statements are formatted wrong. The part you are checking for must be in parenthesis. What you want to be saying is something like…

if (something is true) then
do something
else
do something different
end

Note that the (something is true) part must be in parenthesis to make applescript evaluate the trueness first before it evaluates the “if” statement. As such you need something like the following…

if (exists the file path) then
do something
else
do something different
end

And if you wanted to check for something being false instead of true then use the word “not” like so…

if not (exists the file path) then
do something
else
do something different
end

Here’s another way to check for a file’s existence. The advantage of this way is it bypasses using the Finder. It works because when you try to coerce a string to an alias file applescript automatically checks for the existence of the file, and will throw an error if the file doesn’t exist.

set prefFolderPath to path to preferences folder as text
set prefFileName to "com.apple.mail.plist"
set prefFilePath to prefFolderPath & prefFileName


try
	get prefFilePath as alias
	display dialog "yes"
on error
	display dialog "no"
end try

That works brilliantly, many thanks for your detailed explanations, regulus6633! This is going to be the foundation for some controls on our lab deployments, thanks again!

I’m glad I could help. Good luck.

That’s incorrect. You proved yourself wrong with the not example. By your statement, it would have to be:

if (not (exists the file path)) then

Also, parentheses are not mentioned in the documentation for if statements.

See also: path to (folder)

You’re right Bruce. Oh well, it was 3AM after all! :smiley: I guess I just got used to using the parenthesis because it helps me keep things grouped together and straight in my head. I know many times I need them to keep the order of operations correct, but in this case they’re not needed as evidenced by the following… which works fine.

set prefFolderPath to path to preferences folder as text
set prefFileName to "com.apple.mail.plist"
set prefFilePath to prefFolderPath & prefFileName

tell application "Finder"
	if exists file prefFilePath then
		display dialog "yes"
	else
		display dialog "no"
	end if
end tell

While working on this script I ran into an error when running it on 10.4.

tell application "System Events"
	if not (exists process "iMovie HD") then
		
		--variables
		set prefFolderPath to path to preferences folder as text
		set prefFileName to "edu.labLaunch.plist"
		set prefFilePath to prefFolderPath & prefFileName

display dialog prefFilePath

end if
end tell

This generates an error: System Events got an error: Can’t make preferences folder into type constant. The same code appears to run just fine in 10.5

If I switch the code to this:



--variables
set prefFolderPath to path to preferences folder as text
set prefFileName to "edu.GSEIS.labLaunch.plist"
set prefFilePath to prefFolderPath & prefFileName

tell application "System Events"
	if not (exists process "iMovie HD") then
		
		display dialog prefFilePath
		
	end if
end tell

It runs just fine in 10.4. So I guess my question is why can’t I set variables within the tell/if nest?