Changing the "Open With" of a file

Hi, I’ve been at this for about an hour, searching and experimenting, and having no luck.

I’d like to write a script that takes the current Finder Selection and sets the file’s “open with…” setting to a preset application.

I’ve tried this (and variations):

tell application "Finder"
	set fileList to selection
	repeat with theFile in fileList
		try
			tell application "Finder"
				set creator type of file this_file to "R*ch"
			end tell
		on error errorMessage
			display dialog errorMessage
		end try
	end repeat
end tell

but a little digging around revealed that apparently “creator type” is not a common thing anymore, true? Anyway, I usually get the error firing, and it either has to do with not being able to reference a file or some error with the creator type.

Thanks for any help…I’m one of those guys who understands programming but doesn’t know much about AppleScript, so it’s frustrating when I know you can do something and have a good idea of how you might do it, but just fall short on the syntax.

Model: Power Mac G5 Dual 2.5 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi drukepple,

What app do you want to set the rtf file open with? Do you want an explanation of what determines which app opens a file? I have a goo document that explains bindings, but can’t remember where I got it. You’re using the file type and not the creator type in your script.

gl,

Hi Kel, thanks for the reply
It’s actually a plain text XML file, that I want to set to open up in TextMate. I just hadn’t gotten far enough to figure out what the codes were. I thought “R*ch” looked kinda like a RTF, but I wasn’t sure. It was a code snippet I found elsewhere.

The text files get created outside of any application, so it doesn’t have the “open with” set to anything but the default of TextEdit. I’d just like a slightly more automated way of turning this files into TextMate files (you know what I mean).

Try using “TxMt” for the creator type.

I found that on the internet.

Thanks kel, I finally got it. The problem I was having was accessing the files in the selection. I finally got it right with:

tell application "Finder"
	set theFiles to selection as list
	repeat with theFile in theFiles
		set creator type of theFile to "TxMt"
	end repeat
end tell

The main hurdle I had to get over were get the selection as a list

I also tried this once I had the selection as list:

set creator type of every item in theFiles to “TxMt”

but that didn’t work. Probably just me not being all that familiar with the ins and outs of AppleScript, but in the end I just had to repeat of the items in the list explicitly. Was I doing something wrong with my “every” thought? Should something like that have worked? Or not?

Anyway, I finally got what I wanted…not sure if the time spent on this will really save me that much time in the long run, but we’ll see. I suppose that part of the learning process, and in the end it’ll be experience to make the next script go quicker.

Hi Drukepple,

In the past, you could use ‘selection’ as a multi-threaded list, but in OSX you can only get the selection as a list. So you’re doing it right with the repeat loop, because the ‘every element’ reference form on a list just returns the list.

However, if the selected items where the only items in a folder, you could use the reference form to get a mutil-threaded list and set the property of every item:

tell app “Finder”
set creator type of every item of somefolderreference to “ABCD”
end tell

This would work because when used in the same statement, you’re working with a multi-threaded list.

gl,

OK, thanks for the explanation, kel!

For reference, and handy snippet for determine File and Creator Type:


on open parseMe
	set parseMeInfo to info for parseMe
	logMe("FILE NAME: " & name of parseMeInfo)
	logMe("FILE TYPE: " & file type of parseMeInfo)
	logMe("FILE CREATOR: " & file creator of parseMeInfo)
	logMe("--------------------------------------------------")
end open

logMe is a generic log handler I always use which writes to a text file. Can be anything you want it to be really. I have to mess with File and Creator Types alot in my job and generally I save a file known to be from a certain program (simple blank document), run the above on it to establish a baseline for a particular File/Creator pair.

In your case you can find out the “now” of a sample XML file and the “to what” by saving something from the destination program and getting it’s File/Creator pair.

Hi Kevin,

You can also get the creator type of an app directly from the app if it has its own creator type. In this case I don’t have Text Mate so had to use the internet.

Script make things so easy huh?

gl,

Ah did not know that. Neat trick I’ll have to log in the back of my brain. :smiley:

Still need an actual file to get the pair (Type & Creator), so still easier in alot of cases. I like to set both when I’m mucking around. Not having the actual application around makes things trickier. Unless you can get the info directly from the software company, I’ve found internet lists for File Type, Creator Type, and Extension to be a combination of: correct, wrong, incomplete, or dated*. I dislike ambiguity when I do programming. :stuck_out_tongue:


*Based on my experiences writing an AppleScript that restores lost File Type and Creator Type from Mac graphics files with missing resource forks. I learned far too much about these things doing that project, yeesh.