View Full Version : Run a script whenever an application opens

05-19-2010, 04:56 PM
Sorry for the stupid question, but I'm new to applescript.

How do you write a script that will run whenever an application is opened?

I want to open a specific file whenever one of certain applications are opened. It would be a different file for each specific application.

What I really want is a way to keep an editable file containing a few notes for every application I use. This is to remind me of new things I've learned about that app, any problems I've found a solution to, and anything I want to remember to do when I open the app, such as send a msg to Helmut, check for homophones, remember that it's set for IPA now, purge past events.

05-19-2010, 05:33 PM
You could simply use the System Preferences pane Do Something When (http://www.azarhi.com/Projects/DSW/index.php), along with a script like this:

tell application "AppName"
open file "Path:To:File"
end tell

Change this script as necessary to meet your needs (by setting the application name and the path to the desired file), and store the script wherever you'd like. Note, too, that the files which will be opened with these scripts can't be moved.

Do Something When allows you to run a script when an application launches. So, if you always want to open a certain file for each application, creating the scripts you want like this should work. Then, just go to System Preferences->DSW and set it to run the script you want for each application.

05-20-2010, 04:14 AM
What you need is an applescript that stays open all the time and performs its tasks periodically. So here's an example applescript for you. Save it as an application and check the "stay open" box to make it stay open after you launch it. The way a stay-open script works is there's 2 main handlers, the "on run" and the "on idle". "on run" runs once at application launch. "on idle" runs every so often. You set how often it runs by returning a number of seconds at the end of the handler. So here's the example... it sets up the application parameters in the "on run" handler and then in "on idle" it can check those parameters. Right now this script just displays the results but you can make it do whatever you want at this point.
global applicationsToWatch, idleTime

on run
set applicationsToWatch to {"Safari", "Mail"}
set idleTime to 10 -- number of seconds between checks
end run

on idle
-- get the currently running applications
tell application "System Events"
tell (processes whose background only is false) to set runningApplicationsNames to name
end tell

-- do something with the information
set resultString to ""
repeat with i from 1 to count of applicationsToWatch
if (item i of applicationsToWatch) is in runningApplicationsNames then
set resultString to resultString & item i of applicationsToWatch & " is running" & return
set resultString to resultString & item i of applicationsToWatch & " is not running" & return
end if
end repeat

-- display the results
tell me
set tempVar to display dialog "The Status:" & return & resultString buttons {"Quit", "Continue"} default button "Continue"
end tell

-- a mechanism to quit the application
if button returned of tempVar is "Quit" then
set idleTime to 1
tell me to quit
end if

return idleTime
end idle