Change file association

I am trying to change a files kind/application association (the “open with” settings in the Get Info panel).

  1. How would I create a folder action to do this when I drop a file into a specific folder?
  2. How would I do this with selected files (multiple files) manually?

I would really appreciate any help, thanks.

The best way to do this would be to set the creator type (“code”) property of the files. The Finder can do this. To find the creator code for a specific app, use this:

tell application "Finder" to set theCreator to the creator type of item (path to application "APP NAME")

It will be four characters long. You can then set this for any files and it should open that file with the desired app. This won’t change the default settings for a file type, just for files which are specifically changed.

This droplet will make all files dropped on it open with TextEdit:

on open theseItems
	tell application "Finder"
		repeat with thisItem in theseItems
			set creator type of thisItem to "ttxt"
		end repeat
	end tell
end open

To get the selected files in the Finder, you get “the selection”. It will return a list of selected files so you’ll need to work with each item in the list, even if there’s only one. So something like:

tell application "Finder"
	set theseItems to the selection
	repeat with thisItem in theseItems
		set the creator type of thisItem to "ttxt"
	end repeat
end tell

Hope this helps.

Thank you so much! That looks wonderful, but since I am initially going to be using this with a folder action, how would I implement it? Is there a “ThisFolder” selection instead of “TheseItems”?

I would think I could just use the command “set the creator type of thisItem to “ttxt”” as the action script, but will it know that thisItem is the item that get’s added to the folder?

tell application "Finder"
	set creator type of thisItem to "ttxt"
end tell

Does that sound OK to you?

Folder action scripts take the form:

on adding folder items to thisFolder after receiving theseItems

Here, thisFolder is the folder you’re adding files to and theseItems are the files being added. So if you’re going to be adding files to the folder, the script should work basically as is except “on open theseItems” would be replaced by the line from above and “end open” would be replaced by “end adding folder items to”.

You could run into problems, however, if you’re adding folders to your “action folder” and want their contents to have the script run on them. To get around that, you’d have to implement some sort of folder check and work with it from there. Something like:

on adding folder items to thisFolder after receiving theseItems
	tell application "Finder"
		repeat with thisItem in theseItems
			if kind of thisItem is not "Folder" then
				set creator type of thisItem to "ttxt"
			else
				my folderHandle(thisItem)
			end if
		end repeat
	end tell
end adding folder items to

on folderHandle(theFolder)
	tell application "Finder"
		set folderFiles to (every item of theFolder)
		repeat with theFile in folderFiles
			if kind of theFile is not "Folder" then
				set creator type of theFile to "ttxt"
			else
				my folderHandle(theFile)
			end if
		end repeat
	end tell
end folderHandle

This should do the job of recursively scanning folders you add to your “action folder” for files and changing their creator type as well. Hope this helps.

On a side not, anyone have any idea why I couldn’t get:

if not folder of thisItem

to work properly? I resorted to using “type is not “Folder”” but shouldn’t the other way work as well? I’m stumped.

Hi,

There is no folder property of Finder item if you look in the dictionary. There is a folder label for the record that is returned from the standard additions scripting addition command ‘info for’.

set the_folder to choose folder
set item_info to (info for the_folder)
folder of item_info
--> true

Thanks for clearing that up.