~/Desktop/Foo.scpt contains:

(POSIX path of (path to me)) as text

Which returns:


How do I get path /users/[username]/Desktop/ and the file name Foo.scpt as individual pieces?

4 Answers 4


Just ask Finder he knows how to do it :)

tell application "Finder"
     set parentpath to POSIX path of (parent of (path to me) as string)
     set filename to name of (path to me)

     display dialog parentpath
     display dialog filename
 end tell

Another option is to use text item delimiters:

set text item delimiters to "/"
POSIX path of (path to me)
text item -1 of result -- "Untitled.scpt"

If the path can end with a slash, you can use a handler like this:

on basename(x)
    if x is "/" then return "/"
    if item -1 of x is "/" then set x to text 1 thru -2 of x
    set text item delimiters to "/"
    text item -1 of x
end basename

basename("/dir1/dir2/file.txt") -- "file.txt"
basename("/dir1/") -- "dir1"
basename("/dir1/dir2/") -- "dir2"
basename("/dir1/dir2") -- "dir2"
basename("/") -- "/"

Note that text item delimiters is a property of the AppleScript object (not local to the function), but as far as I know, you don't have to restore the text item delimiters property if you don't rely on it later in the script.


This should work:

-- This script returns the full path to the directory that this script is running in

-- get the full path to be split
set pathToMe to POSIX path of (path to me as text)

-- get the path to the directory
set script1 to "dirname '" & pathToMe & "'"
set dirPath to do shell script script1

-- get the file name
set script2 to "basename '" & pathToMe & "'"
set fileName to do shell script script2

-- display the results
display dialog "Directory Path: " & dirPath & return & return & "File Name: " & fileName
set path_ to (POSIX path of (path to me)) as text
set name_ to name of (info for path_)
set a_ to count path_
set b_ to count name_
set minus_ to a_ - b_
set path_name_ to text 1 thru (minus_) of path_

variable path_ is the path
variable name_ is the name

You must log in to answer this question.

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