Safari - Keyboard Maestro Question

Is it possible to have Keyboard Maestro trigger a Notification and a Sound when a specified Safari Browser window changes or updates?
Thank You

Can't think of how that would work.

I think this could be done by getting the site's source, writing it in a variable or in a file and comparing the current source to this.

This seems to work, at least after the KM forum site updated to "See 1 new or updated topic". Didn't test in a macro but there's a diff in the HTML source.

Thanks Pete.... several concepts there that I'm not up to yet.
Still learning KB and not even started looking into Safari/Browsers.
I guess I'm in over my head. Much appreciated though.

I'm already trying if it would work in a macro :slight_smile: Do you know a site that periodically updates?

Yes... I know exactly the site I would give you.... It's a graphics order queue that updates quite often during the day. Unfortunately, I don't think it would be appreciated if it surfaced that I was looking into such things, utilizing this site. Even though this would benefit me / coworkers tremendously.
I won't be able to give you the site address. It's not any kind of top secret thing, I just don't feel comfortable giving it out.... I'm hesitant also because I don't know what I might disturb, if that's possible.
I realize that's about as far as you can go with this..... Sorry.... and again. thanks for your help.

No problem, I'm using a local html now. Should work …

This should get you started. Note that the macro reloads the page.

I'm using custom AppleScript colors so here's the script as text

tell application "Safari"
	set theTab to current tab of window 1 -- might make more sense to use the site's name
	set theURL to URL of theTab
	set URL of theTab to theURL
	set theSource to source of theTab
end tell

Using the current tab of window 1 doesn't make much sense (only for testing), it might make more sense to use the site's name:

tell application "Safari"		
		set theTab to first tab of windows whose name = "Latest topics - Keyboard Maestro Discourse"
end tell

I will take it slow and recreate the variables and actions you describe..... this is new to me. I'll have fun trying to work it out. Thank you for the work you've put into this... I'll reply back if I have any problems or (hopefully) success!

1 Like

No trouble setting up everything -. save for the AppleScript entry, I am just not clear as to what goes where or what replaces what.... could you explain that section once again?
I'll understand if you'd rather not. Thanks again.

That actually saved you from trying something that won't work … forget that part. I forgot to test the macro with the second script (only tested in Script Debugger and didn't pay enough attention).

Here's a new script.

  • Make sure to copy the URL from Safari after the site is fully loaded
    (as URLs might be redirected - if that happens and you've set a non-redirected "version" of the URL in the script it would never find the tab. It would be possible to check whether a URL is redirected, but doing that would be overkill as this would be done each time the macro runs).

  • Make sure to set a Timeout (e.g. 5 minutes) in the Execute AppleScript action
    (as in case the site fails to load at some point the script would run till the default Timeout of 99 hours is reached)

This scripts should work - "should" because I didn't test it in the macro, that's your part

-- Find tab via URL (to get its source)

set theURL to ""

tell application "Safari"
		set theTab to missing value
		repeat with thisWindow in windows
			set theTabs to (tabs of thisWindow whose URL = theURL)
			if theTabs ≠ {} then
				set theTab to item 1 of theTabs
				exit repeat
			end if
		end repeat
		if theTab = missing value then error "Found no tab with this URL"
		set URL of theTab to theURL -- reload site
		delay 1
		set theSource to ""
		repeat while theSource = "" -- set a Timeout in the Keyboard Maestro AppleScript action as this would run "forever" if loading of the site fails
			delay 0.2
			set theSource to source of theTab -- get current source
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "Safari" message error_message as warning
	end try
end tell

Hi @ChilliDog32, out of interest did you specifically want to detect changes in an open Safari window or would you prefer to detect changes to a web page (without opening it in Safari) and get a notification of some sort? I ask because I’ve created something in KM to do the latter and which runs, for example, every so often.

That could be useful sometime, where can I find this?

Pete31.... I will try out your latest script and directions...thanks.

tiffle... The Safari window would have to be open.... Thanks

1 Like

pete31.... This works Great....The last script you sent worked. Thanks for all your efforts with this question. I learned a few things with your help.

1 Like