Applescript for checking changes to Apple Refurbished Store (or any site)

Discussion in 'Mac Programming' started by silby101, Oct 12, 2010.

  1. macrumors member

    Joined:
    Jun 20, 2003
    #1
    Hello all,

    I thought that I'd share a script that helped me land my hexacore Mac Pro today. I developed it so that the instant it was added to the :apple: Refurbished Store, I would get an email. I tried using some websites that turn any page into an RSS feed, and tried other sites that monitored the refurbished stores, but they didn't act fast enough for what I wanted. This script (once you make it an application) runs in the background constantly but uses very little to no CPU power and very little RAM.

    This works on my Power Mac G5 [Late 2005] running 10.5.8 so I can't guarantee that it will work for you. Try it out, and let me know if it works. I won't know how to fix it on your setup if it doesn't but I can offer suggestions. I tried to fix some errors that occurred on my setup with Finder not updating paths or getting the current file size, which explains some of the extra repeat/try loops. I found that maybe once every few weeks, there would be a weird pop-up error that occurred from curl not being able to reach the site I wanted to monitor, but if you clicked Okay on the error, it would go back to normal checking. I found that if I ignored any errors in the code, it wouldn't work so errors are not suppressed.

    How to get it to work for you...
    1. Save the attachment
    2. Open the script
    3. Change the variables you need
      a. Website address to check [my_url]
      b. file path on your computer [the_filepath]
      c. email address [my_email_address]
    4. Choose File -> "Save As..."
    5. Choose File Format "Application"
    6. Choose Option "Stay Open"

    Here is the logic for the code...
    • Get the text from the source code for the website you want to check
    • Save it to the current.html file
    • Compare the current.html file size to the previous.html file size
    • If there is no change or a small change, try again later
    • If the change is significant then send an email with the current.html and previous.html as attachments
    • Then save the current.html as previous.html

    In this code, I ignore changes of 150 bytes since those changes are often just enabling or disabling links to other products in the refurbished store that I am not interested in. Usually when something is added or removed from the store, the change is between 1500 and 1700 bytes.

    Happy shopping!:)

    Here is the code...
    Code:
    global my_url, my_text, the_filename, the_filepath, the_file, the_new_file, the_old_file, old_fileSize, new_fileSize, my_email_address
    
    on run {}
    	check_site()
    end run
    
    on idle {}
    	tell application "Finder" to update the_filepath
    	check_site()
    	return 90 --seconds between checking
    end idle
    
    on check_site()
    	
    	--here are the variables you need to change
    	set my_url to "http://store.apple.com/ca/browse/home/specialdeals/mac/mac_pro" --this can be any site you want to monitor for changes
    	set the_filepath to "Your_HD:Users:your_name:Documents:Website Change:" --path to where your script is located along with the html files
    	set my_email_address to "your_name@email.com" --your email address
    	
    	set my_text to do shell script "curl " & quoted form of my_url
    	set the_filename to "current.html" --this file has to already exist at the_filepath
    	set the_file to the_filepath & the_filename
    	
    	try
    		set eof file the_file to 0
    		write my_text to file the_file
    	end try
    	tell application "Finder" to update the_filepath
    	set the_new_file to alias the_file
    	--needed to add this for the updates to work properly on my machine
    	repeat
    		try
    			tell application "Finder" to set new_fileSize to the size of the_new_file
    			if new_fileSize is not missing value then exit repeat
    		on error
    			delay 1
    			tell application "Finder" to update the_filepath
    			delay 5
    		end try
    	end repeat
    	
    	
    	set the_filename to "previous.html" --this file needs to exist at the_filepath
    	set the_file to the_filepath & the_filename
    	set the_old_file to alias the_file
    	
    	repeat
    		try
    			tell application "Finder" to set old_fileSize to the size of the_old_file
    			if old_fileSize is not missing value then exit repeat
    		on error
    			delay 1
    			tell application "Finder" to update the_filepath
    			delay 5
    		end try
    	end repeat
    	
    	
    	set size_change to new_fileSize - old_fileSize as integer
    	if size_change = 0 then
    		--display dialog "No change"
    		delay 0.1
    	else
    		if size_change > 150 or size_change < -150 then
    			
    			tell application "Finder"
    				if process "Mail" exists then
    					delay 0.5
    				else
    					tell application "Mail" to launch
    					tell application "Finder"
    						repeat until process "Mail" exists
    							delay 0.5
    						end repeat
    						delay 0.5
    					end tell
    				end if
    			end tell
    			delay 1
    			tell application "Finder" to set the visible of process "Mail" to false
    			
    			tell application "Mail"
    				set theMessage to make new outgoing message with properties {visible:false, subject:"Change to Refurbished Mac Store", content:"Change of " & size_change & " bytes" & return & my_url}
    				tell theMessage
    					make new to recipient at end of to recipients with properties {address:my_email_address}
    					make new attachment with properties {file name:the_new_file} at after the last word of the last paragraph
    					make new attachment with properties {file name:the_old_file} at after the last word of the last paragraph
    				end tell
    				delay 5
    				send theMessage
    				try
    					set eof file the_file to 0
    					write my_text to file the_file
    				end try
    			end tell
    		end if
    	end if
    end check_site
    
    
     

    Attached Files:

  2. macrumors P6

    DoFoT9

    Joined:
    Jun 11, 2007
    Location:
    Singapore
    #2
    absolutely wonderful script! i will install this when i get home!

    i have a request - i am in australia, is it customisable to each country?

    thanks a lot!

    edit: ahh i see you can choose the website - got it.
     
  3. macrumors newbie

    Joined:
    Nov 16, 2010
    #3
    Nice nice one silby101.
    I am also looking for a HEX from Canadian site.
     
  4. macrumors newbie

    Joined:
    Nov 16, 2010
    #4
    Thanks to silby101.

    I just scored a 6core. You have no idea how quickly the go.
    This script is a gem.
    :)
     
  5. thread starter macrumors member

    Joined:
    Jun 20, 2003
    #5
    In fact I do know how quickly they go, hence the creation of this script. :D

    Enjoy your hex, I love mine! I'm thinking of using this script to get a refub iPad or Macbook Air when they start selling them.
     
  6. tundramac, Mar 13, 2011
    Last edited: Mar 13, 2011

    macrumors newbie

    Joined:
    Mar 13, 2011
    #6
    DISREGARD - As I said, my fault. It is working now. Thanks so much!
     
  7. thread starter macrumors member

    Joined:
    Jun 20, 2003
    #7
    Anyone else had success with this?

    I just got a message (actually got it months ago but just now found it) of someone who was able to get a hexacore with this script, so I was wondering if anyone else has had success with it?

    So far, I have as well as this other person and one more in the thread. Anyone else?
     

Share This Page