Aplescript that checks URL

Discussion in 'Mac Programming' started by Gizzy, Feb 17, 2012.

  1. macrumors newbie

    #1
    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
     
  2. macrumors member

    Mark FX

    #2
    Try this Applescript code below.

    Code:
    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
     
  3. macrumors member

    Mark FX

    #3
    Your welcome Gizzy, no thanks required, it only took me an hour.

    Regards Mark
     

Share This Page