move files based on file type

I was thinking of having this hotfolder set up with action assigned to it,

I copy files into the folder and based on file type the script copies them to a location predetermined based on what kind of file it is , so quark files go there, tiffs somewhere else, and so on

here’s another thing, after all the coping is done, ALL files would be MOVED to yet another folder

can applescript distinguish what type of file it’s dealing with and does it know that a file is still copying or has finished coping and is now available to be moved?

I’m going to break this into pieces

*can applescript distinguish what type of file it’s dealing with

set File2scan to choose file
file type of (info for File2scan)

*what kind of file it is , so quark files go there, tiffs somewhere else

“Quark files” are by creator type, “tiff” files are by file type. Note the difference that a tiff file can be opened by different apps. Quark is not a file type because file types are four character values “TEXT”, "MP3 ",“JPEG”, “TIFF”. Choosing which to sort by is important. In OS X you could also sort by name extension “.jpg”, “.mpg”, “.tiff”. All these values can be obtained from the “info for” command. Just substitute which one you want for “file type” in the code above.

*does it know that a file is still copying or has finished coping and is now available to be moved?

From “ADD - NEW ITEM ALERT”
Sal Soghoian ©1998 Apple Computer
Folder Action handler is triggered when items are added to the attached folder.


-- THIS PROPERTY IS USED TO INDICATE WHETHER THE SCRIPT CHECKS THE INCOMING FILES FOR COMPLETED TRANSFER
-- SET THIS PROPERTY TO TRUE FOR FOLDERS SHARED VIA FILE SHARING
-- NOTE THAT ITEMS DROPPED IN SHARED FOLDERS WILL HAVE THEIR LABEL SET TO 7

property copy_checks_indicator : false

--The handler is (assuming you set all the variables)

if copy_checks_indicator is true then
			-- CHECK THE FILES TO MAKE SURE THEY"RE COMPLETELY AVAILABLE
			set the added_items to my check_added_items(the added_items)
			if the added_items is {} then return "no vaild items"
		end if

But if I were you instead of a folder action, I would do this with a droplet because

*after all the coping is done, ALL files would be MOVED to yet another folder *

Kind of makes it pointless to drop them into the original folder in the first place. Move them here, copy them to, move them again… you sure about that?

With a droplet, you could select your files, drop them, and they would go wherever you direct them via the script. Multiple copies to different folders, whatever.
Post back with comments
SC

thanks for all this info, I’ll test it to see what I get

as to the move, copy, move part… yeah you’re right it’s kind of more than needed, I was thinking of overcoming this issue by having the hotfolder on the same volume as the final destionation would be, that way no actual phisical moving of files takes place (except copying part but I do need two duplicates so that does not change anything)

there’s one thing that might be an adventage there too, eventually I may have multiple users acccesing this script, some of them almost simulatniously, with a folder i can have the script write temporary subdirecotires for each of those user actions, I don’t know if having multiple droplets wouldn’t be too messy (I’m assuming the droplet thing can only be occupied by one action at a time)

still, it may go different ways so when you get a chance let me know how to create those

thanks

Hey thar - I successfully got a droplet to do the following:
-Receive a batch of different file type dropped files
-If a file type was not known, a prompt came up asking where to put files of that type. This property only has to be set once for each file type. The script then put the files to the assigned folder;
-When the file type is known, the files are ported to thier respective destinations according to file type. Copy could easily be added at this point since your touching the file (the way I structured the script the file type is known by the time it gets here).

The registering of filetypes to the destination files into properties and then referencing them took a bit of variable trickery. There was no hardwiring of paths so the script works on any volume. Even if you go with a hotfolder you’ll probably want this snippet:
Droplet “DropMove”, ‘Sitcom 2005’


property TypeFiles : {}
property TypeFileFolders : {}

on open filelist
	set YourFile to (items of filelist) as list --process files to match file type to destination folder
	repeat with YourFile in filelist
		
		set ThisFilesName to name of (info for YourFile)
		set ThisFilesType to file type of (info for YourFile) as string
		if ThisFilesType is not in TypeFiles then
			display dialog "The file:" & return & ThisFilesName & return & "whose file type is '" & ThisFilesType & "'" & return & "needs to be assigned to a folder-"
			choose folder --You are prompted for a  destination folder when a new file type occurs
			set Fold to the result as string
			copy ThisFilesType to the end of TypeFiles --Adds new file type to "TypeFile" list
			copy Fold to the end of TypeFileFolders --Adds the file type and the folder its associated with to TypeFileFolders
		end if
		
		if ThisFilesType is in TypeFiles then
			set FolderNum to list_position(ThisFilesType, TypeFiles) as number
			set FolderName to alias ((item FolderNum) of TypeFileFolders) as string
			
			tell application "Finder"
				move file YourFile to folder FolderName
			end tell
		end if
	end repeat
end open

on list_position(ThisFilesType, TypeFiles)
	repeat with i from 1 to the count of TypeFiles
		if item i of TypeFiles is ThisFilesType then return i
	end repeat
	return 0
end list_position

I like this because after surfing my download folder looks like a mixed bean soup of file types. Now I can just drop the whole selection on the script and it sends them all home. Thanks for the idea!
I think multiple users each having there own script could work out for you; When a command is sent, the script waits on a timed reply, I think the default is 30 seconds. So if two files were coming into a folder at once, the scripts would just get in line,and wait thier turn. I think :rolleyes:
Besides, that happens at lightning speed.
SC

yeah… due to what appears to be more problems than benefits using the hotfolder option, I just might go with your droplet.

In case I revise the script substantialy, could you just post a raw format for a droplet?
what I mean is without any tasks actually assinged but just what makes a script into a droplet

thanks

btw, can it somehow ignore folder structure and dig into directories to find files to examine and redirect? I had few files and then some more inside a folder, when I dropped that package onto the droplet, it did what it was supposed to to the files but it couldn’t make out what to do with a folder (files inside were ignored therefore)

The part of the script that makes it a droplet is


On open filelist--Creates a droplet
--script
end open

As the beginning of the script. Any other on() handlers need to after the closing of the “open filelist” statement. Yes, droplets are that simple!

The folder is getting caught up at

tell application “Finder”
move file YourFile to folder FolderName
end tell

Because a folder isn’t a file. I spent time developing this into a full-blown app that takes on files and folders from a droplet. You can either drop the selection of files or one folder with mulitple files in it. If it encounters a folder in the enclosed filelist, the enclosed folder gets ignored. That’s how I knew where the snag was, I had spent some “intimate time” with that part of the code. I didn’t go there, but I’d say that you could set it up to do a folder in a folder. But folders in a folder would get sticky. That’s why I only allow one folder to be processed at a time - errors started coming up to the point of me asking, “what’s the goal here?”. So I made it to suit my needs- it sends files home from a selection list or a folder, and leaves the folders inside alone. You might even want a (choose folder) approach instead of a drop; whatever suits your goal because a lot of options start coming up. For example, with the droplet you can add the feature of resetting the assigned folders to an empty list “on run”, or clicking it.
SC

is the variable filelist how I refer to the files dropped on the droplet?

in other words if I need to lets say copy those files somewhere how do I tell applescript what I am refering to as far as the objects to be copied

filelist is the varible that refers to the dropped items as a list, this is your selected list of files.
YourFile is the variable that is each file within the list:


on open filelist
set YourFile to (items of filelist) as list --turns your dropped items into a list 
repeat with YourFile in filelist-- grabs YourFile, one at a time
      --this is the place to execute code on each item, as the loop repeats until it reaches         the last file. This is how you achieve  copy to location 1, move to location 2, and so on with the next file.
end repeat 
end open

“droppedItems” is often used for the variable ‘filelist’. But look how that would turn out here:
on open droppedItems
set theItems to (items of droppeItems)

SC