Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lynkynpark86

macrumors 6502
Original poster
I am making a small utility that creates a file in the user's directory, and creates subdirectories in that folder, named Documents, Audio, Video, Photos, etc. I have that part done. Then, however, I want it to go through ALL the files on the desktop, one by one, and move them to the appropriate folder, ie:
Code:
if file "fileA" is of type "pdf" or "doc" or "pages"
   Move file "fileA" to "stuff"
end if
Is there any way to make a script chose a file in a folder, and to tell the extention? PLEASE HELP ME!!! :apple
 
Here you go.

Code:
tell application "Finder"
	activate

	--All of these folders must already exist.
	
	--A folder with a bunch of files in it
	set sourceFolder to folder "Source_Folder" of desktop
	
	--A folder that will have the sub-folders in it
	set destFolder to folder "Dest_Folder" of desktop
	
	--Sub-folders
	set destPNG to folder "png" of destFolder
	set destPDF to folder "pdf" of destFolder
	set destDOC to folder "doc" of destFolder
	
	
	--Grab all of the PDFs
	set everyPDF to every file of sourceFolder whose name extension is "pdf"
	repeat with aPDF in everyPDF
		move aPDF to destPDF
	end repeat
	
	
	--Grab all of the PNGs
	set everyPNG to every file of sourceFolder whose name extension is "png"
	repeat with aPNG in everyPNG
		move aPNG to destPNG
	end repeat
	
	
	--Fancy. Multiple file types at a time.
	set everyDocFile to every file of sourceFolder whose name extension is in {"doc", "pages"}
	repeat with aFile in everyDocFile
		move aFile to destDOC
	end repeat
	
	
	--You see the pattern, now do your own thing here.
	
	
	
	
end tell

-numero
 
Here's a more compact version that also creates the folders as it goes:

Code:
tell application "Finder"	
    set sourceFolder to folder "Source_Folder" of desktop
    set destFolder to folder "Dest_Folder" of desktop

    repeat with itemRef in {¬
            {fileExts:{"png", "jpg", "jpeg", "tif", "tiff"}, folderName:"Images"}, ¬
            {fileExts:{"txt", "pages", "doc"}, folderName:"Documents"}} -- Add more entries here

        set subFolderName to folderName of itemRef
        if not (exists folder subFolderName of destFolder) then
            make new folder at destFolder with properties {name:subFolderName}
        end if

        move (every file of sourceFolder whose name extension is in fileExts of itemRef)¬
                to (folder subFolderName of destFolder)
    end repeat
end tell

Incidentally, it'd be really nice if you could use Universal Type Identifiers ("public.image", "public.movie", etc.) rather than listing every last file name extension yourself. Unfortunately, Finder doesn't do UTIs, and I'm skeptical that System Events would handle them intelligently, so extensions probably are the way to go.

HTH
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.