Get path of sub-folder's Parent : Applescript

I want to get the path of a subfolder's parent folder when I drop a folder on a application coded in Applescript

So if I drop a folder called 'Test1' which is on the Desktop

I want the script to give me '/Users/username/Desktop/' as the parent path of 'Test1'. This is my code:

on open the_dropped_folder

tell application "Finder"

 set FolderPath to the_dropped_folder
 set ParentPath to container of FolderPath
 set thepath to POSIX path of ParentPath

end tell
end open

This throws and error saying that:

" Cant get class ctnr of {alias "Mac HD: Users:username:Desktop:Test1:"}

Any idea how I can achieve this?


Solution 1:

The open handler’s parameter gets a list of alias objects1. The curly braces ({}) in your error message indicate that the error happend while it was trying to operate on a list object.

Thus, you need to use something like set FolderPath to first item of the_dropped_folder to work with a single item instead of the list (and probably give the parameter a plural name while you are at it so it “reads better”). That should let your set ParentPath to container of FolderPath statement work properly.

The next statement will probably fail though. ParentPath will be a Finder folder object which does not have a POSIX path property. Usually the easiest way around this problem is to have Finder convert its item object (folder is a subclass of item) into an alias object and then extract its POSIX path (alias objects do have a POSIX path property).

If you put all this together, you might end up with something like this:

on open someDroppedAliases
    set theAlias to first item of someDroppedAliases
    tell application "Finder"
        set parentFolder to container of theAlias
        set parentFolderAlias to parentFolder as alias
    end tell
    set parentFolderPath to POSIX path of parentFolderAlias
    display dialog "Path of container:" default answer parentFolderPath
end open

Without all the intermediate variables:

on open someDroppedAliases
    tell application "Finder" to ¬
        set parentFolderPath to POSIX path of ¬
            (container of first item of someDroppedAliases as alias)
    display dialog "Path of container:" default answer parentFolderPath
end open

Or, with System Events (whose item objects actually have a POSIX path property):

on open someDroppedAliases
    tell application "System Events" to ¬
        set parentFolderPath to POSIX path of ¬
            container of first item of someDroppedAliases
    display dialog "Path of container:" default answer parentFolderPath
end open

Note: Nothing in my versions (or in your original formulation) is specific to processing folders. The same program will process a dropped file and yield its container.


1 Technically they are «class bmrk» objects in Snow Leopard, which seem to work mostly like proper alias objects (though there may be some differences from true alias objects).

Solution 2:

Here is another useful example (I just boiled down the above to illustrate what appears to be a common trap for young players like me; I did not realise that the parentheses are important).

Let's say tmpItem is a file dropped onto a droplet. You can tell application "Finder" to ...

set the destination to the POSIX path of tmpItem -- This will work

set the destination to the POSIX path of the container of tmpItem -- This will FAIL

set the destination to the POSIX path of the container of tmpItem as alias -- This will FAIL

set the destination to the POSIX path of (the container of tmpItem as alias) -- This will SUCCEED