question regarding list

I am trying to write a script which gets “kind” of a file and then i want to create a folder named with the “kind” of the file.

set myList to {“xyz”, “pqr”}

tell app “finder”
get kind of every file in
set x to the result

repeat with aItem in x

	make new folder with properties {name:aItem} at desktop
	
end repeat

end tell

Problem: i dont know how to deal with “kind” repetitions i.e. i get an error when the “kind” is repeated (because more than one file have the same “kind”)
For solving this problem:
i wish to add “kind” to myList after a folder is created for that “kind” and then avoid making new folder if “kind” matches contents of myList

Thanks in advance.

Hi Lance,

If I got you right, the following code should suit your needs:


tell application "Finder"
	-- desktop used just as an example
	set desktopfiles to every file in desktop
	repeat with desktopfile in desktopfiles
		set filekind to kind of desktopfile
		if not (exists folder filekind in desktop) then
			make new folder in desktop with properties {name:filekind}
		end if
	end repeat
end tell

It creates a folder named after every found file kind, but does not create the folder, if it already exists.

Thanks for the prompt help.
yes, you got me right. works perfectly.
however i still want to know a few questions about list
couple of them i asked earlier:

i wish to add “kind” to myList after a folder is created for that “kind” and then avoid
making new folder if “kind” matches contents of myList

Is it possible to do such a thing with lists?----if not in this case, then i may need it somewhere else.

i had tried this:

repeat with aItem in x

make new folder with properties {name:aItem} at desktop
set y to the result
set end of myList to y

try this, but it is probably not faster than Martin’s solution


property kindList : {}

tell application "Finder"
	-- desktop used just as an example
	set desktopfiles to every file in desktop
	repeat with desktopfile in desktopfiles
		set filekind to kind of desktopfile
		if filekind is not in kindList then
			make new folder in desktop with properties {name:filekind}
			set end of kindList to filekind
		end if
	end repeat
end tell

thank you very much Martin Michel and StefanK

i did not want a faster solution, i just wanted to learn more about how to use lists.
After you gave me your script, I could write the next script where i could place files with same extensions into the folders created earlier.
(By the way, i realised one mistake----i should have used “name extension” instead of “kind”)

set docfolder to alias "Mac HD:Users:lance:Documents:"

tell application "Finder"
	get name of every folder in docfolder
	
	
	set docfolderfiles to every file in docfolder
	repeat with docfolderfile in docfolderfiles
		set fileext to name extension of docfolderfile
		if (exists folder fileext in docfolder) then
			move docfolderfile to folder fileext of folder docfolder
		end if
	end repeat
end tell