I want to do more w/ the email my IP address script

Hi Everyone,
I have been trying to take the email my ip address to new heights. I didn’t write this script, but I have modified it a little. I would like to get my ip address, check if its new, by comparing it to text in a file. If it’s new, send the email then save it to the text file. I know I’m not opening the file for access, or pointing to the file correctly, or something. I have searched, and I have tried many things, but it doesn’t do the check, and it doesn’t do the save. I am still not understanding a lot of concepts, as I am new at this. I am at the point where I am confusing myself the more I research. Any help will be appreciated. Thanks, Adown

PS: Even though I’ve butchered it, it still checks the ip and sends the email.

set response to do shell script "/usr/bin/curl [url=http://checkip.dyndns.org/]http://checkip.dyndns.org/"[/url]
set New_ip_address to extract_ip(response)
set Current_ip_address to "~:Current IP Address.rtf"
set mailto_addr to "myemail@att.net"
set email_address to mailto_addr

if (New_ip_address = Current_ip_address) then
	quit
else
	tell application "Mail"
		activate
		set composedMessage to (a reference to (make new outgoing message ¬
			at the beginning of outgoing messages))
		tell composedMessage
			make new to recipient at beginning of to recipients ¬
				with properties {address:email_address}
			set the subject to "New IP Address"
			set the content to New_ip_address
		end tell
		send composedMessage
	end tell
	save New_ip_address as "Current_ip_address.rtf" in "~/"
end if

-- Function to extract ip from HTML returned by dydns.com 
on extract_ip(this_text)
	set clean_ip to ""
	set this_char to ""
	repeat with this_char in this_text
		set this_char to the contents of this_char
		if the this_char is in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."} then
			set the clean_ip to the clean_ip & this_char as string
		end if
	end repeat
	return the clean_ip
end extract_ip

I don’t recall where I got this (I didn’t write it) but this works and extracts the IP address directly:


property theName : "John Doe"
property theAddress : "john@doe.com"

set ipAddress to do shell script "/usr/bin/curl http://checkip.dyndns.org | /usr/bin/awk '/: / {print $6}' | /usr/bin/cut -f 1 -d '<' "

tell application "Mail"
	set newMessage to make new outgoing message with properties {visible:true, subject:ipAddress}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
	end tell
	activate
	send
end tell

This is the script fragment I use which extracts it in AppleScript instead of the shell:

set Ext_IP to word 25 of (do shell script "curl checkip.dyndns.org")

With respect to writing to a file this is the technique:


set tFile to (path to desktop folder as text) & "Trial.txt" -- define it wherever.
set f to open for access tFile with write permission -- open it (or create it if it doesn't exist)
try
	set eof of f to 0 -- this erases the file
	write "Now is the time" to f -- write to it. Instead of a quote, it can be a variable from elsewhere.
	close access f -- release it.
on error Err
	display dialog Err -- what went wrong?
	close access f -- don't leave it hanging open if it's screwed up.
end try
-- This just tests the result and shows how to read.
display dialog (read file tFile) --> Now is the time
--- Some time later, perhaps, to add text instead of erasing.
set g to open for access tFile with write permission
try
	write return & "for all good men" to g starting at eof -- still at the end of the previous entry.
	close access g
on error
	close access g
end try
display dialog (read file tFile)
--> Now is the time 
--> for all good men

Hi Adam, Thanks for your reply. I came across that script in my searches, and even have an iteration saved with open for access Current_ip_address, but it I have not able to figure out comparing the “new ip address” to the “current ip address” on file.
I only want it to send an email if the ip address has changed. I also thought there would be a command to save the new ip address as "Current ip address, over writing the existing file, for future comparison. Thanks again for your reply. I’ll see what I can do with it, adown

I would love to hear new ideas on this. Thanks, adown

Hello Adown
I am also fairly new to Applescript. The script that I compiled is pieces from other scripts on this site. I am not sure if some of it is right, but it works for me on Lion 10.7.3 If anybody wants to improve on this they are welcomed to do so. Hope this will help you figure out what you want to do. I have some comments to help me remember what some of the lines do.



(*
Works with Lion version 10.7.3
This script will email to Property "theAddress" only if the IPAddress was changed.
Replace "theName" and "theAddress" with your information.
Script checks the current IPAddress and compares it to the old IPAddress that is saved in "ScriptCheckIPAddress.Plist file" that is in "Users:Library:Preferences"
*)

property theName : "JOHN"
property theAddress : "john@doe.com"

try
	--See what the Current IpAddress is
	set Current_ipAddress to do shell script "/usr/bin/curl http://checkip.dyndns.org | /usr/bin/awk '/: / {print $6}' | /usr/bin/cut -f 1 -d '<' "
	--set Current_ipAddress to "192.168.1.256" as text --FOR TESTING PURPOSE ONLY
	
	set ipAddress to Current_ipAddress
	
	--See what the Old IPAddress is in the "ScriptCheckIPAddress.Plist file"
	-- ScriptCheckIPAddress is the name of the variable
	-- ipAddress is the value as text
	set Old_IPAddress to (do shell script "defaults read ScriptCheckIPAddress ipAddress")
	
	if Old_IPAddress = Current_ipAddress then
		-- Don't do anything because nothing changed.
	else
		
		do shell script ("defaults write ScriptCheckIPAddress ipAddress " & ipAddress as text)
		
		--If IPAddress is changed then send an Email
		tell application "Mail"
			set newMessage to make new outgoing message with properties {visible:true, subject:Current_ipAddress, content:"Your Current IPAddress is: " & Current_ipAddress}
			
			tell newMessage
				make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
			end tell
			send newMessage
		end tell
	end if
on error --> no such file yet so write it to plist file
	do shell script ("defaults write ScriptCheckIPAddress ipAddress " & ipAddress as text)
	
end try


Model: Mac Pro
Browser: Safari 534.48.3
Operating System: Mac OS X (10.7)

Thanks a Million,

That is exactly what I was trying to do. I am going to keep going with it to add a timer to run the script every morning. I saw some info on doing that, so I will see what I can do before I cry for help again. Thanks again Prince and Adam for you help. Now I can study the script to understand it better. Thanks again, adown