Scripting Safari

sladuuch

Registered
I need a script that can perform a specific action when a specific URL is accessed in Safari. How might I go about doing this?
 
Do you want a constantly running AppleScript applet, to monitor the front most (document) window?, checking the window's url field for a specific URL?
If so, yes.

I would check for the 'url of document 1' within a 'tell app "Safari"' line, and then compare the resultant url to the one of interest, inside an 'on idle' handler.

Naturally, the above can apply to any document window of 'Safari', if you loop through all of them.
 
barhar said:
Do you want a constantly running AppleScript applet, to monitor the front most (document) window?, checking the window's url field for a specific URL?

Exactly. Is there any way to get an applescript to constantly run in the background, ideally launching when Safari launches?
 
'Is there any way to get an applescript to constantly run in the background, ideally launching when Safari launches?' - no, there is not a way to have an AppleScript applet running in the background, and then be launched (since it is already launched) when a process (such as 'Safari') starts. You cannot launch something that is already launched - unless, you are referring to a daemon or thread, which AppleScript does not do.

However, you can save an AppleScript as an application (thus the name - AppleScript 'applet'). To have the applet run all the time (when opened [launched, run]), in 'Script Editor's 'Save' / 'Save As...' menu item's sheet make sure to click on the 'Stay Open' check box (to add a check mark).

To have the code in the 'applet' check, for 'Safari', every x seconds - use ...

return x -- where x is the number of seconds to check again

... as the last line in the 'on idle' handler.

For example, ...

on run
idle
end

on idle
-- your code here

return 30 -- the 'idle ()' handler executes infinitely every 30 seconds.
end idle
 
Back
Top