How to check if file exists with ~/ path?

I have a script that needs to look into the directory of the local user running the script. Inside there it needs to test to see if a file exists.

For example this is what i want to do:


if exists "~/Library/Preferences/test.plist"

I've tried using posix to resolve the local path but it won't remove the ~.
The closes i've come is doing something like this:

set userHome to (path to home folder as text)
set UserPath to userHome & "Library:Preferences:test.plist"
if exists POSIX file UserPath then

But when i run this i get an error:


"Can’t make file \":Main HD/Users/chrisrice/Library/Preferences/test.plist\" into type reference."


I've literally been searching for how to do this for hours, can anyone please help?

Dual 1.25 PowerMac G4 mdd, G5 1.8, Powerbook 1.25, Mac Pro 2.66 Quad X1900XT, Mac OS X (10.5.1)

Posted on Mar 12, 2010 11:07 AM

Reply
6 replies

Mar 12, 2010 11:38 AM in response to Christopher Wetzel

Here are a couple of the many ways to do this...

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
set itExists to false
try
((path to preferences) & "test.plist" as string) as alias
--file exists
set itExists to true
on error
--file does not exist
end try
</pre>

or

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
tell application "System Events" to set itExists to ¬
exists of alias ((path to preferences) & "test.plist" as string)
</pre>

Hope this helps...

Mar 12, 2010 5:43 PM in response to Christopher Wetzel

The first problem is:

if exists "~/Library/Preferences/test.plist"


You're checking to see if a string object exists. Of course it does.

There's nothing here that tells AppleScript you're trying to check a file. As far as AppleScript is concerned, this is just a string of characters.

So, given that, the string needs to be coerced to some kind of file reference, and that's where you're falling down - there are several different ways of defining paths in AppleScript and you need to know which one you're dealing with. In this case, any UNIX-style path needs to be identified as such, via the POSIX file class:

if exists POSIX file "~/Library/Preferences/test.plist"…


Now AppleScript understands that the string of characters represents a POSIX path and it can react accordingly.

At the end of the day, though, this is completely the wrong approach. You should avoid hard-coding paths like this since it makes your script non-portable. For example, when running Mac OS X in other languages you might not have a 'Library' directory, nor a 'Preferences' directory - you'd likely have a local translation of those words.

To that end, AppleScript provides the path to command which uses the built-in System routines for finding common directories. In this case you want to know if the 'test.plist' file exists in the user's home directory, so this is how you should check:

set prefsFolder to (path to preferences from user domain)
tell application "System Events"
if exists file "test.plist" of prefsFolder then
return yes
else
return no
end if
end tell


This code is sure to work on any version of Mac OS X, regardless of the internationalization settings and it doesn't worry about Mac-style paths, UNIX-style paths, POSIX files or any of that other cruft.

Mar 16, 2010 12:13 PM in response to Christopher Wetzel

As for the last reply. if exists posix file does not work because the method it calls doesn't understand the ~/ to mean the users directory. Or at least it refused to evaluate it.


Maybe interpreting ~/ is a Snow Leopard thing, but my point was that you should not be using this form anyway.

You should use the path to command to evaluate the user's directory (or, in this case, Preferences directory) at run time, rather than rely on a hard-coded string in your script.

This line of code:

set prefsFolder to (path to preferences from user domain)


will locate the user's Preferences folder, regardless of its path, internationalization settings, or any other variable you care to throw at it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to check if file exists with ~/ path?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.