global checkInterval, alertDelta, oldCounter on run set listOfIntervals to {1, 2, 5, 10, 15, 30, 60} set listOfDeltas to {100, 300, 500, 1000, 5000, 10000, 20000} set checkInterval to (choose from list listOfIntervals with prompt "Time interval between iTMS counter checks (in minutes):") * 60 set alertDelta to (choose from list listOfDeltas with prompt "Alert me when difference from next full 100,000 drops below:") set oldCounter to 0 return checkInterval end run on reopen set oldCounter to 0 tell me to idle -- check now end reopen on idle set theCounter to pollCounter() --say theCounter as string --say "The next reminder will be at " & alertDelta if theCounter = 0 then -- error return 0 else set delta to 100000 - (theCounter mod 100000) if delta ≤ alertDelta then beep display dialog "The iTMS Contest Counter is only " & delta & " songs away from the next 100,000 mark." & timeApprox(oldCounter, theCounter, delta) buttons {"Remind me later", "Open Music Store"} default button 2 if button returned of result is "Open Music Store" then open location "itms://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/viewAlbum?" else --remind later set alertDelta to alertDelta div 2 if alertDelta is 0 then quit return 0 end if end if end if set oldCounter to theCounter return checkInterval end if end idle on timeApprox(oldCounter, newCounter, delta) set resultString to "" if (oldCounter ≠ 0) and (newCounter > oldCounter) then set secondsTo100K to delta / (newCounter - oldCounter) * checkInterval set minutesTo100K to secondsTo100K div 60 set resultString to return & "At the current rate, this mark will be reached in approximately " & minutesTo100K & " minutes." end if return resultString end timeApprox on pollCounter() try set theCounter to do shell script "curl -m 5 http://phobos.apple.com/external_counter.js | perl -e 'if (<> =~ m/num\\s*=\\s*(\\d+)\\D/i) {print $1} else {print 0};'" set theCounter to theCounter as integer if theCounter is 0 then error "Parse error" return theCounter on error display dialog "Could not retrieve the iTMS counter." buttons {"Quit", "Keep Running"} default button 2 with icon 2 giving up after 40 if button returned of result is "Quit" then quit return 0 end if return 0 end try end pollCounter on quit continue quit end quit