Dropping an open folder and its contents on a droplet

I am trying to determine if an item of one or more items dropped onto a droplet is the folder that contains all the items. (sounds like a work puzzle, sorry)

This script below works but only for single items dropped on the droplet.

If more than one item (even if they are all files) the script errors.

I only want the warning to occur based on two instances:

  1. if an open folder and its contents are all dragged over the droplet.
  2. Or if a single closed folder is dragged over the droplet. (This part works with the script below)
on open files_
	set infoRec to get info for files_
	if folder of infoRec then
		display dialog "You are trying to archive a folder, or you have selected a folder that is open along with all its contents. Press Cancel and try archiving your files again." buttons {"Cancel"} default button "Cancel" with icon 2
	end if

files_ is a list of everything dropped onto the droplet. So to access each item of a list you need to use a repeat loop and cycle through each. I didn’t test this but your script should look something like this…

on open files_
	repeat with i from 1 to count of files_
		set thisFile to item i of files_
		set thisInfo to (get info for thisFile)
		if kind of thisInfo is folder then
			display dialog "whatever"
		end if
	end repeat
end open

Thank you Hank,
I actually tried doing a loop similar to this, but as in this one I am getting an error that it can’t get the class of the item or items dropped on this droplet?

Quoting the word “folder” will fix it.

if kind of thisInfo is "folder" then

Here’s 4 ways to find out what each item is when you loop through the list…

set anItem to choose folder
set theInfo to (get info for anItem)

if theInfo is folder then
	return true
else
	return false
end if
set anItem to choose folder
set theInfo to (get info for anItem)

if kind of theInfo is "Folder" then
	return true
else
	return false
end if
set anItem to choose folder
tell application "Finder"
	set theClass to class of (get properties of anItem)
	
	if theClass is folder then
		return true
	else
		return false
	end if
end tell
set anItem to choose folder
tell application "System Events"
	set theKind to kind of (get properties of anItem)
end tell

if theKind is "Folder" then
	return true
else
	return false
end if

I almost have it, but still not there.
The problem comes when trying to determine if the files dragged upon the droplet were the combination of an an open folder as well as its contents.

Therefore, I am only concerned in somehow determining if all the contents belong to the folder being dragged above the droplet, along with their open folder being dragged upon the droplet also.

What I want to warn against: (all these items dragged on the droplet)
Folder (open)
File x
File xy
File xyz

However, I am not concerned if an operator was to grab a bunch of files along with folders.
This would be acceptable and should not throw a warning.
Folder (closed)
File x
Folder x (notice that I don’t care if a folder is in the mix, as long as it is not the parent folder that houses all the other selected files in the list.

So what I came up with is this. but it’s not quite there.

on open files_
	set x to count of files_
	--display dialog x
	if x is greater than 1 then
		tell application "Finder"
			repeat with i from 1 to count of files_
				set thisFile to item 1 of files_
				set thisInfo to (get info for thisFile)
				if kind of thisInfo is "folder" then
					display dialog "whatever"
				end if
			end repeat
		end tell
		
	else if x is equal to 1 then
		set thisFile to files_
		set thisInfo to (get info for thisFile)
		if kind of thisInfo is "folder" then
			display dialog "whatever"
		end if
	end if
end open

if Folder (open) and the files have been dropped onto the droplet, you can save the reference to the folder in a variable and compare the container of the files with this variable before processing the items.

But this is impossible, if only one of the files File x(yz) has been dropped.

Thank you all for your great help with this. I realized I was able to modify my script to test for the last item in a path string that would always be the root path of the files being dropped from. I realize that it’s slightly different than what I asked for originally, but the script incorporates all the info you kindly provided. So many thanks.

on open files_
	--Test for trying to archive an open sub folder that resides in the Image: folder.
	tell application "Finder"
		repeat with i from 1 to count of files_
			set thisFile to item i of files_
			set thisInfo to (get info for thisFile)
			if kind of thisInfo is "folder" then
				set theContainer to container of item i of files_ as string
				set theText to characters -2 thru -6 of theContainer as string
				if theText is equal to "Image" then display dialog "Do Not archive the DSA.zip folder." buttons {"Cancel"} default button "Cancel" with icon 2
			end if
		end repeat
	end tell
end open 

A few comments about your script. It’s nothing serious but I thought I’d point out a few tips to help you become a better scripter.

  1. You have all of your code inside of a “Finder” tell block. This is highly inefficient and as of 10.6 it may even cause problems. The rule of thumb is to only tell an application to do something if that application is needed. Since the “info for” command isn’t a Finder command you shouldn’t tell the Finder to run it.

  2. In this line set theText to characters -2 thru -6 of theContainer as string you use the word “characters” which returns a list and then coerce that into a string. You can get the string directly using the word “text” instead.

  3. The Finder is not needed for the display dialog command either.

With these in mind I re-wrote your script. Notice we only tell the Finder to do the one thing is it needed for eg. get the container.

on open files_
	--Test for trying to archive an open sub folder that resides in the Image: folder.
	repeat with i from 1 to count of files_
		set thisFile to item i of files_
		set thisInfo to (get info for thisFile)
		if kind of thisInfo is "folder" then
			tell application "Finder" to set theContainer to container of thisFile
			set theText to text -2 thru -6 of (theContainer as text)
			if theText is equal to "Image" then display dialog "Do Not archive the DSA.zip folder." buttons {"Cancel"} default button "Cancel" with icon 2
		end if
	end repeat
end open

Thank you Hank. the info helps a lot, but whether or not I retain it is another thing”I only dabble in scripting a handful of times a year. Which sucks because this is a language that needs to be practiced.
I have to ask a simple question. how did you know to put the parenthesis around (theContainer as text)?

Me and messed up logic, I end up writting code like these 2 lines

set theContainer to theContainer as string
--(“string” vs. “text”. I need to look that up tomorrow)
set theText to text items -2 thru -6 of theContainer --(Which wouldn’t have worked because “items” doesn’t belong, but I would have guessed it was :frowning:

I am not expecting you to respond to this, you have already helped me tremendously. I now know a bit where to focus some reading.