"On Open" can't count files dropped if files have different extensions

This seems to have come up before, with no clear solution. I’ve been banging my head on the desk about it.

If I save this script as an applet, and drop multiple files on it, the applet thinks there’s only one dropped file. What obvious thing am I doing wrong???

EDIT: This occurs if I drop two files with different extensions. See my reply to Shane below.

on open theDrop
	set fileCount to count of theDrop
	display dialog fileCount
end open

If I select multiple files in the Execute menu of SD, then the count is correct. It’s wrong when saved as an applet.

1 Like

It sounds like you’re dropping quarantined files. This is a known issue, and you can find some (fairly complex) workarounds if you hunt around here or Macscripter. But sometimes the easiest solution is to drag the files somewhere that resolves the quarantine issue (ie, don’t drag files from Downloads).

Actually, I realized too late that the problem occurs when I drop two or more files with different extensions. My script tests dropped files to see if they are PostScript files, and, if they are, converts them to PDF, and if not, displays an error message.

This works perfectly if all the dropped files have a “.ps” extension or if all the dropped files have the same extension that is NOT “.ps”. But if I drop a “.ps” file and a “.txt” file, then the only file processed is the “.txt” file.

This is NOT a problem if I select files with different extensions in the on run block, using choose file.

Is this an AppleScript bug? And is there a workaround?

I’ve played with this a bit and found a few interesting things. Using Apple’s applet shell you are correct, files of the same type (or which share a common type identifier - e.g. text) are passed to the on open handler. If you have mixed file types, then you only receieve one type of file in the on open handler.

When I moved to the Enhanced applet shell, I get multiple invocations of the on open handler, one for each type of dropped file.

1 Like

Thank you!

Yes, exactly as you say: when I saved the script as an Enhanced Applet, the problem didn’t occur. I’ve been looking around for an example of an Enhanced Applet that will run without opening a window, but haven’t found one. Is there a collection of Enhanced Applets that I can look at?

And I wonder if there’s a workaround for this problem in pure AppleScript?

And to answer my own question: one solution is to go to the Bundle & Export dialog in SD and specfify a UTI in the Accept items conforming to these UTIs field. This will restrict the drop to files that conform that type. This solves the problem for me.