Gettng the number of a certain type of item in a list

thank you for your help. I am new to applescript and I am trying to get the number of the same kind of file type from list. I started with something that works but I don’t know how to tell it to get all the “AGD6” files or tell me how many there may be in the dstfolder.

What I want to do is make a script that will separate all the files to one folder (i.e. freehad to freehand folder, photoshop to photoshop folder, etc.) , identify those files with missing icons by file type and then I can assign it the right application to open it.

here’s my code:

set chosenFolder to choose folder
set creatorList to {“AGD1”, “AGD3”, “AGD5”, “AGD6”, “TIFF”, “PDF”, “SIT5”, “”, “8BPS”, “PICT”, “JPEG”}
set appList to {“Freehand 8.0”, “Freehand 10.0”, “Freehand 11.0”, “Freehand MX”, “Adobe Photoshop TIFF”, “Adobe Acrobat .PDF”, " Missing Value", “Stuffit Archive 5”, “Adobe Photoshop PSD”, “PICT file”, “JPEG File”}

tell application “Finder”
set myFiles to every file of dstFolder
set myTypes to file type of every file of dstFolder
set myfileType to myTypes
repeat with myfileType in myTypes
end repeat
display dialog myfileType
end tell

thanks for any help.


You could do something like this:

set myJpg to every file of dstFolder whose file type is "JPEG"

Having said that, I was experimenting with this line and found that several jpegs that I have have no file type or creator type associated with them. It returns “missing value”. If you use the line:

get properties of whateverFileFolder

You’ll see a list of items you can use in making a script. You might try using “kind”

set myJpg to every file of dstFolder whose kind is "JPEG Image"

You could put files with missing value (from the file type line) somewhere else and sort through them using the “kind” statement and assign what program opens them based on their kind.

Hope this helps some.


Thanks for the help. This does work well! I was hoping for a solution that could assist me with getting the following:

set myFiles to every file of dstFolder —choosing the target folder
set myTypes to file type of every file of dstFolder — getting the file type of each file in dstfolder
set myCount to count myTypes —Get a count of how many files

if statement
set myCount to ?
(Something here that can tell me,"you have x amount of xxx type file in list myTypes)
end if

display dialog myCount ---display how many of each type of file in the list

example ( 6 files of type AGD6
16 files of type TIFF, etc.)

thanks for any help.

it’s not clear what do you want to use the “if statment” for.
to get the number of files of each kind in a folder try this:

set DestFolder to choose folder
set filetypes to {"jpeg image", "AVI movie", "MP3 audio file"} --or any other file kinds
set types_and_counts to {}
tell application "Finder"
	repeat with filetype in filetypes
		set thecount to count of (every file of DestFolder whose (kind is filetype)) --you can use type ¬
		--instead of kind if you prefer (seem to be less reliable)
		set type_and_count to filetype & ":" & thecount
		set types_and_counts to types_and_counts & type_and_count
	end repeat
	return types_and_counts -- or display dialog 
end tell

I’s sorry maybe this will clarify my request.

let’s say I have a list like this:

set a to {1, 1, 1, 2, 2, 3}— let’s say this is my list of file types.
get the selection whose value = “1” --Here is where i am stuck.

I want to get this result.

you have 3 file type “1”
you have 2 file type “2”
you have 1 file type “3”

I hope this helps


ok, now i really don’t understand what’re you looking for.
if you need a script to go through a folder and check how many files of each specific type are there, (for example you have 3 files of type avi, 2 files of type jpeg, etc.) then the script i’ve posted above should do just that.
if you need to determine which file types or kinds you have in your folder without having to specify them manualy, you can add the followin code at the top of the script (beneath the choose folder line) :

set DestFolder to choose folder
set filetypes to {}
tell application "Finder"
	set thefiles to (every item of DestFolder)
	repeat with afile in thefiles
		set thekind to ((kind of afile) as text)
		if (filetypes contains thekind) is false then set filetypes to filetypes & thekind
	end repeat
end tell

however, if you prefer to first have all the file types copied to a list and then count them, i guess one way to do it is this:

set a to {1, 1, 3, 2, 1, 3, 4, 4}
set thelist to {}
repeat with x in a
	set i to 0
	repeat with y in a
		if x as text is y as text then set i to i + 1 --you can use as integer or whatever, as long as x and y coerced the same.
	end repeat
	set theline to "you have " & (i as text) & " times " & (x as text) & return
	if ((thelist contains theline) is false) then set thelist to thelist & theline
end repeat
return thelist

there’s probably a better way, but tis work.

i’m curious though, why do you prefer to do it this way…

For being very confused about my request, You nailed it! it was just what I needed.

thank you so much!