Run script on login on program launch

How would i set up a script to run when an application runs or when i log in?
say if i open up IE my google search script pops up

or when i log in a dialog says how are you etc…

In OS X 10.2 and later, you can add the script (saved as an application) to the Login Items preference pane. I’m not sure when this feature was introduced - it might or might not be available in all versions of OS X.

Regarding a script which activates when you launch an app, there’s no built-in way to do it. You could create a script (saved as a stay-open application), and have it watch the process list for a target application. When it sees that the target application is running, it could run the desired code. Something like this (saved as a stay-open application):

on idle
	set idleInterval to 30 -- seconds between checks
	set targetApp to "Internet Explorer"
	tell application "System Events"
		set processExists to exists process targetApp
	end tell
	if processExists is true then
		tell application targetApp
			-- your code here
		end tell
		-- Then what?...
		-- quit this script?
		-- set idleInterval to a large number so your code doesn't execute every few seconds while the app is in use?

	end if
	return idleInterval
end idle

It isn’t the perfect solution but it might work if your needs are modest. :slight_smile:

so does need to be open? like a login app as explained above?

You can add it to the login items and/or launch it by clicking the script’s icon.

Folks- I figured out another way to get a script to run on program launch, though imperfect.

I wanted a script that would run when InDesign is launched. After launching InDesign, I did a search on the computer for ‘files created today’. Eventually, I located a temp file that InDesign creates when it launches. By making the folder this file is contained in a ‘watched folder’ and attaching the appropriate script, I got the script to launch when the app was launched.

Only problem is, the file seems to be regenerated multiple times during a session, and the script runs whenever this file is meddled with. Maybe other applications behave differently?

hope this helps someone…