How to add "on quit" to idle handler

Discussion in 'Mac Programming' started by pierat, Apr 30, 2014.

  1. pierat macrumors member

    Joined:
    Dec 28, 2010
    #1
    Admittedly, I know almost nothing about applescript. I borrowed this script from a user on a forum and applied it to my own needs.

    Code:
    on idle
    	tell application "Safari"
    		if name of documents as string does not contain "Pandora" then -- check window name
    			activate -- comment this out if you want it in the background
    			open location "http://pandora.com" -- open new Pandora window if there is none
    		else -- refresh existing Pandora window
    			set _documents to documents
    			repeat with _document in _documents
    				if name of _document contains "Pandora" then
    					activate -- comment this out if you want it in the background
    					do JavaScript "
    window.location = 'http://pandora.com';
    " in document 1
    				end if
    			end repeat
    		end if
    	end tell
    	
    	return 3600 -- every 60 mins
    end idle
    All I am trying to do is make a script that will open Pandora in a Safari tab and refresh it every hour because I like to turn my display off while letting Pandora run. I've saved this script as a stay open application. Right now, the script works perfectly for what I want. But when I quit it, I must also go to the Safari tab and close Pandora, or it will continue to run. What I would like to add, is something like this.

    Code:
    on quit
    tell application "Safari"
        repeat with t in tabs of windows
            tell t
                if name of documents as string contains "Pandora" then close
            end tell
        end repeat
    end tell
    I tried adding it several different ways, but no matter what I do, the application won't quit. Am I just missing some simple thing here? Any help would be appreciated.
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    I've not really used an "on quit" handler myself before. However, looking at the documentation on the Apple website might be a good place to start. Check out the bit about "quit Handlers" here:

    Documentation

    I suspect you'll need a "continue" or "continue quit" in there somewhere.

    Good luck!
     
  3. pierat thread starter macrumors member

    Joined:
    Dec 28, 2010
    #3
    I got some help from a friend who was kind enough to fix this for me. He didn't really fix anything, he basically threw mine out and started over!

    Here is the finished script that actually works.

    Code:
    on idle
    	set pandoraTab to getPandoraTab()
    	
    	-- No Pandora tab found
    	if pandoraTab is null then
    		tell application "Safari"
    			activate
    			open location "http://pandora.com"
    		end tell
    		
    		return
    	end if
    	
    	-- Pandora tab found: refresh
    	tell application "Safari"
    		activate
    		do JavaScript "window.location = 'http://pandora.com';" in pandoraTab
    	end tell
    	
    	return 3600 -- delay!
    end idle
    
    on quit
    	set pandoraTab to getPandoraTab()
    	
    	if pandoraTab is not null then
    		tell application "Safari" to close pandoraTab
    	end if
    	
    	continue quit
    end quit
    
    on getPandoraTab()
    	tell application "System Events" to set procs to name of every process
    	if procs does not contain "Safari" then
    		return null
    	end if
    	
    	tell application "Safari"
    		set allWindows to every window
    		repeat with aWindow in allWindows
    			try
    				set allTabs to tabs of aWindow
    				repeat with aTab in allTabs
    					set tabName to name of aTab
    					if tabName contains "Pandora" then
    						return aTab
    					end if
    				end repeat
    			end try
    		end repeat
    	end tell
    	
    	return null
    end getPandoraTab
    
    
    
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Other than the refactoring to produce a common subroutine, the principal difference is this line in the on quit handler:
    Code:
    	continue quit
    
    So the solution is exactly as superscape suspected. It also answers your original question, "Am I just missing some simple thing here?". Yes, you're missing a continue quit.
     
  5. pierat thread starter macrumors member

    Joined:
    Dec 28, 2010
    #5
    Yup, correct. Thanks for the replies here guys. And thank you superscape for the answer!
     

Share This Page