Set File Type and Creator based on file extension

As you can probably tell from my current script, I am somewhat of a novice.

I am trying to create a droplet application script such that when a file types with specific named file extensions are dropped onto the script icon, their respective (previously missing) file type and creator codes are added automatically.

The current script I have compiled returns the following error when a file is dropped;

Can’t get <> of application “Finder”

Any help greatly appreciated. Note that the script was working to set the file type and creator code until I became ambitious in making the set Type actions dependent on the file extension name. I suspect the current script is over-simplistic, and there is much more to this.

Ultimately, I’d like to make the script work in Leopard, Snow Leopard and Lion (hopefully the same code can be used for all three O.S versions, but may need to be saved natively to each).

property FileType : ""
property CreatorType : ""

on open theFiles
	tell application "Finder"
		activate
		
		if name extension is "paperlibrary" then
			set FileType to "LBPR"
			set CreatorType to "FSPC"
			
		else if name extension is "patternlibrary" then
			set FileType to "LBPT"
			set CreatorType to "FSPC"
			
			repeat with eachFile in theFiles
				set the file type of eachFile to FileType
				set the creator type of eachFile to CreatorType
			end repeat
			
		end if
	end tell
end open

David

I didn’t test this but it should work, and should work for any of the OS versions you specified.

on open theFiles
	set CreatorType to "FSPC"
	
	tell application "Finder"
		activate
		
		repeat with eachFile in theFiles
			set FileType to missing value
			
			-- get the name extension
			set thisFileExtension to name extension of eachFile
			
			-- figure out the FileType
			if thisFileExtension is "paperlibrary" then
				set FileType to "LBPR"
				
			else if thisFileExtension is "patternlibrary" then
				set FileType to "LBPT"
				
			end if
			
			-- set the FileType if eachFile was one of the given types
			if FileType is not missing value then
				set the file type of eachFile to FileType
				set the creator type of eachFile to CreatorType
			end if
		end repeat
	end tell
end open

Many thanks, Hank,

I was initially testing your code on an old PowerPC running Leopard, and it would not work for some reason. As soon as I switched to an Intel Mac (Leopard), it worked like a charm.

Just curious, I was wondering why set CreatorType to “FSPC” came before tell application “Finder”
activate
in the script. I imagined that the ‘activate’ part would have to come first, but obviously not as it is working as is.

David