How to 'open a file' with an Applescript Application?


I’m new to Applescript, and I’m having a bit of trouble.

I’m writing a script that I want to be the default application to open a certain filetype.
To clarify, say I’ve got some xml files, but they’re all called something.myextension. I want to write a simple Applescript application, that I can set as the default application that opens .myextension files.
That’s fine, I know how to set the default handler for files.

The problem is, how do I retrieve that file in an applescript? In plain text, I want to do this:

get the file that’s just tried to be run with this applescript
do some stuff with it
end of script.

Could anyone enlighten me as to how I get that file?


James Frost


Use an open handler. Something like this:

on open {file_reference}
set the_text to read file_reference
– do something with the text, for instance
set the_text to the_text & return & “bye”
write the_text to file_reference
end open

If you’re doing more complicated stuff with the text like doing several writes, then you might want to use the ‘open for access’ which returns a reference number which helps in speading things up.


Oops. You need to use the ‘open for access’ command, otherwise the file remains open unless you restart. See ScriptingAddition.PDF if you don’t know how to use the read/write commands.