How do you make a script automatic?

i am definitely new at this, but i do have some very basic experience in programming. i found a very basic manual online, but i feel like i need something more.

my main question is, how can you make it so that a script is automatic, so that you are not even aware that it is running? now i’m figuring that i’m going to have to make it an application and put it in that StartupItems folder. but after that i’m not sure. specifically i would like the script to do an action whenever Safari loads a new URL, and for this capability to always be on whether or not Safari has been activated or has quit. i’m having trouble finding material that addresses this, and if someone can point me in the right direction that would be great.

okay i got my script to work. but instead of being executed at start up, i’d rather have it run automatically when there is an open document in Safari, or if that doesn’t work, when safari has been activated. but i don’t know how to do that.

You can’t have a script that is launched automatically when Safari does something. You’ll need to create a stay open application that watches Safari and then does something based on what it sees. Modify this to suit your needs and then save it as a stay open application. To debug the script during development, comment the first and last lines (add – to the beginning of the lines)

on idle
	tell application "System Events" to set safari_running to exists process "Safari"
	
	if safari_running is true then
		-- do stuff
		tell application "System Events"
			set fm to process "Safari" is frontmost
		end tell
		
		if fm is true then
			-- Safari is active/frontmost
			-- do stuff
		else
			-- Safari is not active/frontmost
			-- do other stuff
		end if
		
	else
		-- Safari isn't running, do other stuff
	end if
	return 30 -- seconds to wait between checks
end idle

– Rob

this is very helpful. i’ve been looking at some different scripts and i figured that “System Events” would probably have something to do with it. now to finalize this thing, how do you call a seperate script from inside one like this, and is there a way to get the script to kinda run in the background, i.e. not on the dock? it’s going to be running constantly anyway. if i ever wanted to quit it, i’d just remove it from startupitems and reboot i suppose.

To run another script, use: run script file “path:to:script”. To quit a script (app) without rebooting, use another script.

tell application "script name" to quit

There are various ways to keep an app/script from showing in the Dock. Drop Script Backgrounder is one such utility.

– Rob