Eventlistener if Safari loaded a new page?

Hi all,

I know next to nothing about AppleScript, so is it possible to make a kind of eventlistener that reports if safari loaded a new page? I know that you can do handlers via the “on …”-command, maybe that’s a way to solve this problem? Thanks in advance.

Hi Greenie,

You can use an idle handler to watch for chnages in Safari fron page. e.g.


global the_url
--
on run
	tell application "Finder" to set Safari_exists to (exists process "Safari")
	if Safari_exists then
		tell application "Safari" to set the_url to URL of front document
	else
		quit
	end if
end run
--
on idle
	tell application "Finder" to set Safari_exists to (exists process "Safari")
	if Safari_exists then
		tell application "Safari" to set cur_url to URL of front document
		if cur_url is not the_url then
			set the_url to cur_url
			-- do whatever
			tell application "Safari" to display dialog ¬
				("The new url is:" & return & the_url) buttons "OK" default button "OK"
		end if
	else
		quit
	end if
	return 2
end idle

You save this as a stay open application. When you run it it checks every two seconds if the web page changed.

gl,

Hi kel,

thank you so much! :slight_smile: I will try that out asap

I posted the wrong script.


global the_url
--
on run
	tell application "Finder" to set Safari_exists to (exists process "Safari")
	if Safari_exists then
		tell application "Safari"
			if exists front document then
				set the_url to URL of front document
			end if
		end tell
		try
			the_url
		on error
			set the_url to ""
		end try
	else
		quit
	end if
end run
--
on idle
	tell application "Finder" to set Safari_exists to (exists process "Safari")
	if Safari_exists then
		set cur_url to ""
		tell application "Safari"
			if exists front document then
				set cur_url to URL of front document
			end if
		end tell
		try
			cur_url
		on error
			set cur_url to ""
		end try
		if cur_url is not the_url then
			set the_url to cur_url
			-- do whatever
			tell application "Safari" to display dialog ¬
				("The new url is:" & return & the_url) buttons "OK" default button "OK"
		end if
	else
		quit
	end if
	return 2
end idle

This one checks if a page is loaded and if there is a window.

gl,

Hi,

and thanks again for your efforts. Seems as if AppleScript can do more and doesn’t look as ugly as I thought. Nevertheless, I don’t think I will get used to this kind of programming-language :slight_smile: