Launch Script when opening specific filetype

Hi,

I am an applescript novice who has occasionally used the language since OS 9. I’ve always been able to figure out how to achieve my desired results but I am running into a wall now and would like to see if anyone would be able to help.

I am trying to write a script that will display a dialog box when I open a file on my computer that is a certain type (be it .xlx, .ppt, .doc, etc.)

Any guidance here would be much appreciated, thanks in advance!

I would use the reversed scheme.

Drag and drop the file’s icon upon an applet’s icon which will do the wanted job.

Here is the applet’s code :

on open (sel)
	local thefile, theExt
	set thefile to item 1 of sel
	tell application "Finder" to set theExt to name extension of thefile
	if theExt is in {"xls", "ppt", "doc"} then
		display dialog "got one of the selected kind of files"
	end if
	tell application "Finder" to open thefile
end open

Yvan KOENIG (VALLAURIS, France) jeudi 6 février 2014 19:31:05

Thank you for the quick reply. Is there any way to “catch” the file open command and then act upon the command?

I’m not aware of such feature and it’s why I posted a script fitting the need by an alternate road.

Yvan KOENIG (VALLAURIS, France) vendredi 7 février 2014 18:01:06

Again, thanks for the quick reply. I’ll let you know what I come up with if I’m able to pull this off. I’ve looked into a watch script that checks in periodically to see if certain files are open. I’ll have to implement this and see if it causes performance issues and is responsive.

You could try writing an Applescript application that does two things:

  1. displays the dialog box

  2. opens the document in the usual application (Word, Excel, etc.)

and then set your script as the default application for the file type (.doc, etc.) by selecting a .doc file, using Get Info, Open With, Change All, and navigating to your script.

I haven’t tried this specific route, but it should probably work. You may need to modify the info.plist file of the AppleScript application so that the application is recognized as a handler for the file type. Look into existing info.plist files to see how it’s done.

Great idea, I’ll try this route as this meets all my requirements. Appreciate the help.

Please post the script if you get it working. It could be useful to others.