Image View file type restrictions

Hopefully this is a easy one…

I’ve got a Image view which I’ve setup for drag and drop, but what I want to do is to restrict the file types that can be dropped onto it to “jpg, png, psd”…

Does anybody have any ideas? I’ve searched this forum but can’t find anything related to this.

Thanks in advance.

Hi,

you have to filter the extensions in the drop event handler
something like this



property extensionList : {"jpg", "png", "psd"}

on drop theObject drag info dragInfo
	set dropped to false
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		set preferred type of pasteboard of dragInfo to "file names"
		set theFiles to contents of pasteboard of dragInfo
		set Ex to name extension of (info for POSIX file (item 1 of theFiles) as alias)
		if Ex is in extensionList then
			set theImage to load image (item 1 of theFiles)
			set oldImage to image of theObject
			set image of theObject to theImage
			try
				delete oldImage
			end try
			set dropped to true
		end if
		set preferred type of pasteboard of dragInfo to ""
	end if
	return dropped
end drop

Thanks StefanK, works like a treat…

And what about only allowing folders ?

Filtering folders for dragging onto an image view is quite useless, but you can use this


.
set theFiles to contents of pasteboard of dragInfo
set {folder:Fo, package folder:Pa} to (info for POSIX file (item 1 of theFiles) as alias)
if Fo and not Pa then
.

the following code won’t work because the folder itself couldn’t be processed

Thanks for your quick reply…

As you said it doesn’t work, but nevermind, I only needed it to only allow applications and folder being dropped… (“app” works but folders don’t have an extension so I couldn’t find a way to add folders to the extensionlist.)