Getting directory of input file (Applescript)

7.2k Views Asked by At

I'm confused - I've been googling for an hour and have tried probably ten different forms of set posixDirectory to POSIX path of (parent of (path to aFile) as string) but I can't seem to get it right.

I'm getting the full POSIX path (including the filename) by doing set posixFilePath to POSIX path of aFile

Now, how do I get the POSIX path of just the directory?? I'm getting various errors depending on what I do... can't make alias.. can't get parent of alias...

I would think this should work but it's not... set posixDirectory to POSIX path of ((parent of aFile))

4

There are 4 best solutions below

1
On BEST ANSWER

There are couple of ways to do this if you already have the initial path.

From Posix path format

set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"


set textNumber1 to characters 1 thru -((offset of "/" in (reverse of items of thePath as string)) + 1) of thePath as string

or using shell

set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"


set parentPath to do shell script "dirname " & quoted form of thePath

Result: "/Users/USERNAME/Documents/Test"


From Posix file format

set thePath to "Macintosh HD:Users:USERNAME:Documents:Test:selectedTextColour.css"


set textNumber1 to characters 1 thru -((offset of ":" in (reverse of items of thePath as string)) + 1) of thePath as string

Result: "Macintosh HD:Users:USERNAME:Documents:Test"

0
On

Use the container command of Finder:

set aFile to choose file

tell application "Finder" to set posixDirectory to POSIX path of ((container of aFile) as text)
0
On

Try the subroutine parentFolderOf(localPath). localPath can be an alias, alias as text or a posix path. It returns a reference that you can use with Finder, like this:

set thisItem to parentFolderOf(path to fonts folder)
tell application "Finder" to reveal thisItem

Here the script:

log (path to fonts folder)
log parentFolderOf(path to fonts folder)
log parentFolderOf(POSIX path of (path to fonts folder as text)) as text

log parentFolderOf("/System")
log parentFolderOf("/System") as text

# lets generate an error:
# parentFolderOf("ThisGeneratesAnError")

on parentFolderOf(localPath)

    if (class of localPath is text) and (localPath contains ":") then
        try
            set localPath to localPath as alias
        on error
            error "File missing!"
        end try
    end if

    # if its not an alias and not text containing ":" we assume its a posix path
    if not (class of localPath is alias) then
        try
            set localPath to (localPath as POSIX file) as alias
        on error
            error "File missing!"
        end try
    end if

    -- get the container:
    set localPathContainerPath to ""
    tell application "Finder"
        try
            set localPathContainerPath to (get container of localPath)
        end try
    end tell

    return localPathContainerPath

end parentFolderOf


Addition McUsr's approach seems to be a good strategy because there is no need to use Finder (which will throw an error if the item is missing). Here a version whose input works with alias, (alias as text) or posix path:

log (parentFolder for (path to me))
log (parentFolder for "/Root/Sub1/Sub2/Sub3/testFile.txt")
log (parentFolder for "/Root/Sub1/Sub2/Sub3////")
log (parentFolder for "///Root/Sub1/Sub2/Sub3////")

log (posixPath for (path to me))

to parentFolder for aPath
    set aPath to posixPath for aPath
    set {tids, text item delimiters, i} to {text item delimiters, "/", ((aPath ends with "/") as integer) + 1}
    set {pF, text item delimiters} to {text 1 thru text item -(i + 1) of aPath, tids}
    return pF
end parentFolder

to posixPath for aPath
    if class of aPath is not text then set aPath to aPath as text
    if aPath contains ":" then set aPath to POSIX path of aPath
    repeat while aPath starts with "//"
        set aPath to (characters 2 thru -1 of aPath) as text
    end repeat
    repeat while aPath ends with "//"
        set aPath to (characters 1 thru -2 of aPath) as text
    end repeat
    return aPath
end posixPath
0
On

This should be sufficient really, when you already have the posix path of a file.

to parentFolOfPxPath for pxPath
    -- Assumes no superfluous slashes
    set {tids, text item delimiters, i} to {text item delimiters, "/", ((pxPath ends with "/") as integer) + 1}
    set {parFol, text item delimiters} to {text 1 thru text item -(i + 1) of pxPath, tids}
    return parFol
end parentFolOfPxPath