Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pierat

macrumors member
Original poster
Dec 28, 2010
36
0
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.
 
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!
 
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
 
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.
 
Yup, correct. Thanks for the replies here guys. And thank you superscape for the answer!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.