Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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).

share|improve this answer
    
That was informative. I'm new to AppleScript and was wondering what I was doing wrong. Thanks!!! –  smokinguns May 20 '11 at 4:58
    
You can also convert it via text: tell app "Finder" to POSIX path of (some item of desktop as text). Converting back to an HFS path: POSIX file "/Applications" as text -- "HD:Applications". POSIX file and alias can refer to just about anything - folders, files, aliases, symlinks, volumes,,. Finder's folder and file file classes can't: tell application "Finder" to folder "HD:etc" -- Finder got an error: Can’t get folder "HD:etc". But "HD:etc" as alias -- alias "HD:private:etc:". –  ؘؘؘؘ May 20 '11 at 20:44

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.