Change "file Uti's" in subtree

Hello.

This snippet lets you change the default application or uti ( uniform type identifier) for every file with a given name extension in a subtree.

You start out with changing one of the files from the file-info window in finder, to the app you want it to open with.
Then you select another file, which is of older date of course, and is opened by default by the app you don’t want it to open with.
Then you run the script. The script will then ask you for which subtree* you want to change all the files to open with an other default app than the current one in.
When you have done so; all the files will from thereafter be having your wanted default app, as the default app.
You may loot and build upon this code and pretty much use it as a blueprint, should you need something to work upon a folder structure.

*I call it a subtree, but I mean really just a folder with some folders within.

I really could have written the handler my self, but i looted Copy folder tree by Jeff Fischer jeff@5fischers.org who gave credit to Dan Decker and Robert Metzker for ideas from their scripts

The usual guarantee about nothing what so ever applies.

You are not granted any rights to reproduce this code in order to publish a tip or hint in print or on a webpage (blog), or pdf books or similar media without my written consent… Posts with as much of the code as you need is of course allowed here at macscripter.net You may naturally refer to the code by a link to this post at macscripter.net. Besides those limitiations i declare the code to be put in public domain for general use and abuse.
Enough said; here you are, enjoy!


” © McUsr 2010 and put in Public domain with the limitation written in the post macscripter.net/viewtopic.php?pid=129400#p129400
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 

You must also state what you eventually have done with the original source. This obviously doesn't matter if you  distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
set fileExt to ""
set resRec to divergingProperties(a reference to fileExt)
if resRec is not null then
	set theRootOfTheTree to choose folder with prompt "Choose a tree to change \"uti's\" within"
	changeAllUtisOLD(theRootOfTheTree, resRec, fileExt)
end if
on divergingProperties(theFileExt)
	local theSel, propCount, fileNmA, fileNmB, fPropsA, fPropsB, dateA, dateB, newFProps, oldFProps, changeRec
	set propCount to 0
	tell application "Finder"
		set theSel to (get its selection) as alias list
		if (count of theSel) is not 2 then
			display dialog "Needs two files with same extension, where one of them is configured to be opened with another app"
			error number -128
		end if
	end tell
	set fileNmA to item 1 of theSel as text
	set fileNmB to item 2 of theSel as text
	tell application "System Events"
		set fPropsA to (get properties of item fileNmA)
		set contents of theFileExt to name extension of fPropsA
		set fPropsB to (get properties of item fileNmB)
		if not (name extension of fPropsA is equal to name extension of fPropsB) then
			display dialog "Needs two files with same extension, where one of them is configured to be opened with another app"
			error number -128
		else
			set dateA to modification date of fPropsA
			set dateB to modification date of fPropsB
			if dateB > dateA then
				set newFProps to fPropsB
				set oldFProps to fPropsA
			else
				set newFProps to fPropsA
				set oldFProps to fPropsB
			end if
			-- eventually changing attributes in order of appearance 
			if not (file type of newFProps is equal to file type of oldFProps) then
				set propCount to propCount + 1
				set changeRec to {file type:(file type of newFProps)}
			end if
			
			-- so we are basically clear about what from what properties the uti data will be set.
			if not (kind of newFProps is equal to kind of oldFProps) then
				set changeRec to {kind:(kind of newFProps)}
				if propCount > 0 then
					set changeRec to changeRec & {kind:(kind of newFProps)}
				else
					set changeRec to {kind:(kind of newFProps)}
				end if
				set propCount to propCount + 1
			end if
			if not (creator type of newFProps is equal to creator type of oldFProps) then
				if propCount > 0 then
					set changeRec to changeRec & {creator type:(creator type of newFProps)}
				else
					set changeRec to {creator type:(creator type of newFProps)}
				end if
				set propCount to propCount + 1
			end if
			if not (type identifier of newFProps is equal to type identifier of oldFProps) then
				if propCount > 0 then
					set changeRec to changeRec & {type identifier:(type identifier of newFProps)}
				else
					set changeRec to {type identifier:(type identifier of newFProps)}
				end if
				set propCount to propCount + 1
			end if
			if not (default application of newFProps is equal to default application of oldFProps) then
				if propCount > 0 then
					set changeRec to changeRec & {default application:(default application of newFProps)}
				else
					set changeRec to {default application:(default application of newFProps)}
				end if
				set propCount to propCount + 1
			end if
		end if
	end tell
	if propCount > 1 then -- let the show begin
		return changeRec
	else
		return null
	end if
end divergingProperties

on changeUtiForFile(theFile, theChanges)
	local changedProperty
	tell application "System Events"
		try
			set changedProperty to file type of theChanges
			set properties of file theFile to {file type:changedProperty}
		end try
		try
			set changedProperty to kind of theChanges
			set properties of file theFile to {kind:changedProperty}
		end try
		try
			set changedProperty to creator type of theChanges
			set properties of file theFile to {creator type:changedProperty}
		end try
		try
			set changedProperty to type identifier of theChanges
			set properties of file theFile to {type identifier:changedProperty}
		end try
		try
			set changedProperty to default application of theChanges
			set properties of file theFile to {default application:changedProperty}
		end try
	end tell
end changeUtiForFile


on changeAllUtis(theRootOfTheTree, theChangeRec, theFileExt)
	local fileText
	tell application "Finder"
		set tSelectedFiles to (every file of folder theRootOfTheTree whose name extension is equal to theFileExt) as alias list
		if tSelectedFiles ≠ {} then
			repeat with aFile in tSelectedFiles
				set fileText to aFile as text
				my changeUtiForFile(fileText, theChangeRec)
			end repeat
		end if
		set tFolders to (every folder of theRootOfTheTree)
		repeat with tThisFolder in tFolders
			my changeAllUtis(tThisFolder as alias, theChangeRec, theFileExt)
		end repeat
	end tell
end changeAllUtis

Best Regards

McUsr

http://github.com/AlanQuatermain/SetAppAffinity

Thanks

I’ll check it out and incorporate it if it works which I’m pretty sure it does.

You have also SLopen which provides a graphical interface for doing it with one or more files at a time by drag and drop.

My was to have a method that did it with a whole subtree, I made up the way to do it by ensuring that every property which may change when changing a default application in one file would be changed in the rest.

Thanks again. I will update to a new version using the command line tool as soon as possible.

Edit: I think I’ll wait until the developer states that the code is tested.
Best Regards

McUsr