symbolic home directory like Unix ~ question

How do I specify a folder for something in a home directory similar to the Unix tilde (~)?

For example:


tell application "Finder"
	set myfile to posix file "~/Library/Preferences/Microsoft/VBA Preferences"
	delete file myfile
	set myfile to posix file "~/Library/Preferences/Microsoft/Office Registration Cache X"
	delete file myfile
end tell

Which does not work. TIA.

You can use the “path to” command to get references to common folders, including the home directory and (best for this case) the preferences folder:


tell application "Finder"
	set prefsFolder to path to preferences folder as string
	set myfile to file prefsFolder & "Microsoft:VBA Preferences"
	delete myfile
	set myfile to file prefsFolder & "Microsoft:Office Registration Cache X"
	delete myfile
end tell

Note that you also have to remove the word “file” from the delete lines, because what you’re working with is already a file reference. Trying to make a new file reference out of it doesn’t work.

Thanks for the reply. Unfortunately it doesn’t quite work. I suspect the problem is that this is an AFP mounted home directory. The error displays the path that results from your suggestion. It looks to this novice like an incomplete path:

“Users-home:myusername:Library:Preferences:Microsoft:VBA Preferences”

Shouldn’t it look more like:

“Network:Servers:…:Users-home:myusername:Library:Preferences:Microsoft:VBA Preferences”

I guess my theory was not right either. I hard coded the network part of the path string but that didn’t help either. What am I doing wrong? TIA.

I think mounted volumes are read as regular directories since both applescript and a shell will consider “home” only the startup disk -the root volume-. So, in my computer:

do shell script "cd ~; PWD" --> "/Users/julifos"
path to home folder --> alias "julifos:Users:julifos:"

I don’t think you can (pseudo-code):

path to home folder from disk "afpdisk" --> alias "afpdisk:users:julifos:"

So, you should hardcode the entire path in your script, know the names you need previously (eg, the afpdisk name and user name of the afpdisk), or ask the user.

Thanks. I thought there should have been a way to do it with Applescript. Oh, well. Back to Unix scripting. A .command filetype seems to be easier then Applescript.

I am surprised the AFP directory had anything to do with the problem. What error exactly came up when it tried to use the path:

“Users-home:myusername:Library:Preferences:Microsoft:VBA Preferences”

That looks good to me. Path to preferences has correctly determined that your folder is on a mounted volume, and returned a path which looks fully qualified, to me.

You’re right, AppleScript is not exactly easy. But you are going to have an especially hard time if you expect UNIX shell script techniques to “just work” without figuring out the right way to do things AS style.

I’ve done AppleScripts with network home directories many times. A little snippet looks like this:


	try
		duplicate folder "SomeReallyImportantFolder" of folder "Management" of folder "Library" of startup disk to folder "Library" of home with replacing
	end try

Works like a champ, no matter where the home directory is.

Many of the fixed folders can be gotten by a path to their name:

path to library folder from user domain

path to preferences folder