PDA

View Full Version : Aplescript that checks URL




Gizzy
Feb 17, 2012, 10:10 AM
I want to create a script that will make a noise or say something whenever a user goes to facebook.com. Can someone help me with this? I have searched but cannot find an example.

Thanks



Mark FX
Feb 18, 2012, 01:29 PM
Try this Applescript code below.


property runningApps : {} as list
property SafariRunning : false as boolean

on run
tell application "System Events"
set runningApps to name of every application process whose visible is equal to true
end tell
if runningApps contains "Safari" then
set SafariRunning to true
my checkForFacebook() --Call the checkForFacebook Handler
else
set SafariRunning to false
return
end if
end run

to checkForFacebook()
if SafariRunning is equal to true then
tell application "Safari"
set currentTab to current tab of window 1
set tabName to name of currentTab
set tabURL to URL of currentTab
end tell
if tabName contains "facebook" or tabURL contains "facebook" then
set volume (3.5) --set the Volume level between 0 and 7
beep --Alert Beep
delay 0.5 --Delay between Beep and Speech
--Speak the Alert Text you want with one of the installed voices
say "Facebook page open in the Safari application." using "Alex"
else
return
end if
else
return
end if
end checkForFacebook

on idle
tell application "System Events"
set runningApps to name of every application process whose visible is equal to true
if runningApps contains "Safari" then
set SafariRunning to true
my checkForFacebook() --Call the checkForFacebook Handler
else
set SafariRunning to false
return
end if
end tell
return 5 --Set the number of seconds between facebook page checks
end idle

on quit
set runningApps to {}
set SafariRunning to false
continue quit
end quit


Copy and paste this code into the Applescript Editor, then very important, save
the file as an application and check the stay open option box, it will only work
as a stay open applet, as all of the work is done in the on Idle handler.

You could change the return time in seconds from the on idle handler, I have
set it to check every 5 seconds.
Also you can change the speech text to what you want, and also the output
volume too, there is more work you could do on this script but as it stands
it works fine.

You will have to double click the app to make it run, and you will also have to
quit it as well, but once it is running it will check that Safari is running, and
check every 5 seconds that a facebook page is opened.

Hope this helps.

Regards Mark

Mark FX
Feb 24, 2012, 03:32 PM
Your welcome Gizzy, no thanks required, it only took me an hour.

Regards Mark