hey guys, im new here, and pretty new to applescripting in general. And while i have learned quite a lot in the past few days, mostly from google results on this site, so I thought Id ask this problem here for which I could not find an answer.
I have need to know the type of file being dropped in an on drop handler for reasons being, i need to not accept dropped folders when afolder is dropped in one place, and only accept jpg and pngs when they are dropped in another place. How can I tell what the of of the file is when it has been dropped?
Help is much appreciated. thanks in advance.
The simplest way is to use the info for command. Here is one way that may get you started:
on open the_Droppings
set OK_Files to {}
set dropped_Folders to {}
repeat with a_Drop in the_Droppings
if folder of (info for a_Drop) then
set end of dropped_Folders to a_Drop
else
if name extension of (info for a_Drop) is "jpg" or name extension of (info for a_Drop) is "png" then
set end of OK_Files to a_Drop
end if
end if
end repeat
display dialog "You dropped " & (OK_Files's length) & " JPG or PNG files, and " & (dropped_Folders's length) & " folders just now."
end open
You may also find this article helpful.
Good luck,
I dont know if I wasnt completely clear or not, but im not working with a droplet, just drop action in an applescript application. would the code below based on the info for do the trick?
on drop theObject drag info dragInfo
set dataTypes to types of pasteboard of dragInfo
set theFiles to {}
set theFiles to contents of pasteboard of dragInfo
repeat with thisFile in theFiles
if folder of (info for thisFile) then
display dialog "This is a Folder"
else if name extension of (info for thisFile) is "jpg" or name extension of (info for thisFile) is "png" then
display dialog "this is a valid image"
end if
end repeat
end drop
aparrantly not. the script cant find “thisFile” (of course the actual path is displayed, but it is the thisFile that cant be found)
Umm. Yes, I see. Sorry about that; I’ve no experience with that sort of thing. Have you studied the Drag and Drop example in your Developer/Examples/AppleScript Studio folder? Perhaps there are some clues there.
Thanks for the help jaques, Ill add in the code later today and let you know how it goes. Im sure it will work though. I had tried to set the file as posix already, but I was using “POSIX path of” instead of “POSIX file”