Getting the parent of a file

Hello,

How can I get the parent of a file as a string? For example, I have a file at ~/Desktop/test.txt, and I want to get the parent of test.txt as a string. So I want to end up with two variables, one is the path to the file, and the other is the file’s path to its parent. In this case, /Users/Me/Desktop/. I will eventually use this file path in a shell script, so it needs to be gotten as a POSIX file path. Right now, this is what I have, but I can’t get it to work correctly:

set file1 to (choose file with prompt "Please Select A File")
set file1parent to the POSIX path of the parent of file1

Upon execution, I get the message “Can not get the parent of alias Macintosh HD:Users:Me:Desktop:test.txt”

If someone could point me in the right direction, I’d be very grateful. This is actually only a small part of a larger script, but, nonetheless, an extremely important part, so I can’t continue with the script until I have this working correctly.

Try something like this:

choose file with prompt "Please select a file:" without invisibles
set theFile to POSIX path of result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}

if last character of theFile is "/" then
	-- Handles 'files' there are actually bundles (e.g. Applications)
	get -3
else
	get -2
end if

set theFileParent to text 1 thru text item (result) of theFile & "/"
set AppleScript's text item delimiters to ASTID

return theFileParent

If you don’t mind using the Finder:

choose file with prompt "Please select a file:" without invisibles
set theFile to result

tell application "Finder" to set theFileParent to POSIX path of (container of theFile as alias)
set theFile to POSIX path of theFile

Thank you. That works perfectly.

One more question though: Is there any way to do the reverse and get the name of the file individually as a POSIX path. Like, going from /Users/Me/Desktop/test.txt to simply getting test.txt as a string without the full file path in front of it. The reason for this is because the shell script will be appending characters to the front of the file name using the “mv” command. So, I need to have the full file path (as a POSIX path), the parent file path (as POSIX), and then the name of the file so that I can rename it.

Something like this:

do shell script "mv" & space & file1 & space & file1parent & "whatever gets appended to the front here" & file1path

Where “file1path” is the name of the file, without the parent path in front. In this case, test.txt. The result would look something like this in Terminal:

mv ~/Desktop/test.txt ~/Desktop/'appended text'test.txt

If anyone can suggest a different or better way of doing this then getting separating the file path into parts, I’m open to it.

A few ideas.

choose file with prompt "Append text to this file name:" without invisibles
set theFile to quoted form of POSIX path of result

set appendedText to "appended_"

do shell script "cd \"`/usr/bin/dirname " & theFile & "`\"; myBaseName=`/usr/bin/basename " & theFile & "`; /bin/mv \"$myBaseName\" \"" & appendedText & "$myBaseName\""

choose file with prompt "Append text to this file name:" without invisibles
set theFile to result

set appendedText to "appended_"

tell application "Finder" to set name of theFile to (appendedText & name of theFile)

Well, the primary use of this would be to append a period to the beginning, thus making the file (or directory) invisible to the Finder. Sorry, I should have mentioned that. That’s why I sort of have to use a shell script.

It actually does work in Finder (it doesn’t give the “This is reserved for system files” error), but it is still visible. It’s odd that I can still see it.

And, no, I don’t have Finder set to show invisible files.

EDIT: It actually does hide the file, but you have to log out and back in. I suppose I could have it append a period and then just do “do shell script ‘killall Finder’” to have Finder refresh. Any better ideas are welcome though.

It works fine for me on OS X v10.4.

Did you notice my edit above?

Yes, I did. I decided to just use “killall Finder” to refresh it. I am using 10.4.6.