Macos – Get parent of folder in AppleScript

applescriptmacos

I am writing some AppleScript in an Automator workflow, and I need to get the parent folder of a file (or folder) object. I have searched all over for this, and gotten the same answers, which are to use the "container", "parent", or "folder" properties. However, none of these work for me, whether or not they are wrapped in a 'tell "Finder"' block.

Thus, if I try something like:

set a to POSIX file "/usr/local"

tell application "Finder"
    set b to container of a
end tell

display alert a

I get the error message 'Can’t get container of file "Macintosh HD:usr:local".' The same thing happens with parent and folder.

Furthermore, if I do "display alert class of a", I get "furl". This is in AppleScript Editor. On the other hand, an Automator workflow with a Run Applescript module with the text

on run {input, parameters}
    repeat with i from 1 to length of input
        set the_file to item i of input
        display alert class of the_file
    end repeat
    return input
end run

displays "1634494835" in an alert box. But if I use "return class of the_file" instead, and use the View Results module, it shows the result as {alias}. Despite these strange class designations, I can use regular alias and file properties such as "POSIX path", with the file object.

Does anybody have any clue what's going on here? Is my AppleScript library broken or something?

Best Answer

From the Library help of Standard Additions:

POSIX file n : A file object specified with a POSIX (slash)-style pathname.

  • POSIX path (text, r/o) : the POSIX (slash)-style path of a file or alias object

There is no container property.


You need what Finder considers an item to be able to do this. One way to get it:

set b to container of (a as alias)
Related Question