(POSIX path of (path to me)) as text
How do I get path
/users/[username]/Desktop/ and the file name
Foo.scpt as individual pieces?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
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
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("/") -- "/"
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.