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?
Best Answer
The
open
handler’s parameter gets a list ofalias
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 yourset ParentPath to container of FolderPath
statement work properly.The next statement will probably fail though.
ParentPath
will be a Finderfolder
object which does not have aPOSIX path
property. Usually the easiest way around this problem is to have Finder convert itsitem
object (folder
is a subclass ofitem
) into analias
object and then extract itsPOSIX path
(alias
objects do have aPOSIX path
property).If you put all this together, you might end up with something like this:
Without all the intermediate variables:
Or, with System Events (whose
item
objects actually have aPOSIX path
property):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 properalias
objects (though there may be some differences from truealias
objects).