set p to "/Users/myAccount/Desktop/testfilename.txt"
set a to POSIX file p

I would like to extract "testfilename.txt" from the posix path above? Anyone know how to go about to do that?

  • If you want just the file name you can use: set a to do shell script "basename $HOME/Desktop/testfilename.txt" as string Mar 21, 2016 at 18:27

1 Answer 1


Try this:

set yourpath to ("/YOUR/PATH/HERE/file.ext") as POSIX file
do shell script "basename " & quoted form of POSIX path of yourpath

This should work for files and folders as well.
I used "basename " & quoted form of... because AppleScript and shell script handle spaces in directories quite in a different way, that should allow you to use also paths with spaces in it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .