Login

Retrieve WordPress View Count with AppleScript

I was looking around today for a way to get updated information on the daily view count on my blog. I was not able to locate one so I whipped up this little AppleScript to do the job.

The current settings will check every five minutes and compare the value from the last check with the current value.

How it works

The script uses a curl call to retrieve the latest stat information. When it is returned it looks something like this:

date,views
2009-01-9,1
2009-01-10,2
2009-01-11,3
2009-01-12,35  

The script breaks this apart into a list and extracts the last entry which is today. It compares the date and resets the high_count if it is a new day and then compares the high view count. If the current count is higher than the last check it pops up a dialog informing you of the date, old value and the new value.

How to Save the Script

Add your api_key and blog_id along with any other changes you want to make to the properties and then save this as a stay open application. The property values are persistent as long as the script is not opened and re-compiled.

property api_key : "your_api_key"
property blog_id : "your_blog_id"
property days_limit : 30
property records_limit : -1
property format : "cvs" -- this script relies on cvs format
property current_high : ""
property current_day : ""
property time_interval : (5 * minutes) -- change this to your desired time between checks


on idle

	set curlScript to "curl 'http://stats.wordpress.com/csv.php?" & ¬
		"api_key=" & api_key & ¬
		"&blog_id=" & blog_id & ¬
		"&days=" & days_limit & ¬
		"&limit=" & records_limit & ¬
		"&format='" & format

	set stats to paragraphs of (do shell script curlScript)
	set today to day of (current date)

	-- if today is a new day reset the count
	if today > current_day then
		set current_high to ""
		set current_day to today
	end if

	set {the_date, the_views} to {item 1, item 2} of tidStuff(",", item -1 of stats)

	if the_views > current_high then
                tell me to activate
		display dialog "New high for:  " & the_date & return & "Old count:     " & ¬
		current_high & return & "New count:     " & ¬
		the_views buttons {"Ok"} default button 1

		set current_high to the_views
	end if

	return time_interval
end idle

on tidStuff(paramHere, textHere)
	set OLDtid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to paramHere
	set theItems to text items of textHere
	set AppleScript's text item delimiters to OLDtid
	return theItems
end tidStuff

Comments  

 
0 # 2010-09-30 16:48
Thanks for the post. Just read Rosenthal's book on AS and excited to get started on AS scripting. I'll keep an eye on your site Craig.
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

 

Product Categories