multiple Script questions

Hello everyone,

I’ve been learning applescript by jumping right into the flames of it. I’m working on the whole “Project Jarvis” idea.

I’ve found scripts online and changing parts here and there to see how it works and performs. Its gone pretty well so far. It will wake me up, tell me the forecast, & play an itunes playlist. Here it what i have completed:

tell application "iTunes"
	activate
	set the sound volume to 60
	play the playlist ("Good Morning")
end tell

property oldrandnum : missing value
set greetings to {"Good Morning, it's time to wake up", "Rise and Shine", "This is the day that the Lord has made", "Get up, it's going to be a great day!"}
set countgreetings to length of greetings
repeat
	set randnum to random number from 1 to countgreetings
	if oldrandnum is missing value or (randnum ≠ oldrandnum) or (countgreetings = 1) then
		set oldrandnum to randnum
		exit repeat
	end if
end repeat
set greeting to item randnum of greetings
say greeting using "Serena"
-- Tells me the date
set mydate to date string of (current date)
say "Today is " & mydate using "Serena"
set mydate to month of (current date)
set myday to day of (current date)
set mymonth to month of (current date)
set myhour to get the (hours of (current date)) as string
set myminutes to get the (minutes of (current date)) as string
set pre to "AM"
set myhour to hours of (current date)
set myminutes to minutes of (current date)
if (myhour > 12) then
	set myhour to (myhour - 12)
	set pre to "PM"
end if
say "it is now" & (myhour as string) using "Serena"
say (myminutes as string) using "Serena"
say pre

tell application "Safari"
	activate
	delay 2
	tell application "Safari" to open location "http://www.weather.com/weather/today/Oklahoma+City+OK+73119:4:US"
	--this is the city code. Search the code for your city on http://weather.yahoo.com/
	set CityCode to 2464592
	--temperature format
	set t_format to "F"
	--voiceover format
	set v_format to "S"
	--say present condition
	set a_format to "Y"
	
	set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
	
	--downloading the file using curl
	set file_content to (do shell script "curl " & IURL)
	--looking for the line with actual condition
	set theText to text ((offset of "yweather:condition" in file_content) + 1) thru -1 of file_content
	set sub_1 to text ((offset of "\"" in theText) + 1) thru -1 of theText
	
	--today conditions found
	set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
	
	--looking for actual temperature temperature
	set sub_1a to text ((offset of "temp=" in sub_1)) thru -1 of sub_1
	set sub_1b to text ((offset of "\"" in sub_1a) + 1) thru -1 of sub_1a
	set actual_temp to text 1 thru ((offset of "\"" in sub_1b) - 1) of sub_1b
	
	if t_format is equal to "C" then
		set actual_temp to (5 / 9) * (actual_temp - 32) as integer
	end if
	
	--looking for today forecast
	set theText to text ((offset of "yweather:forecast" in file_content) + 1) thru -1 of file_content
	set sub_2 to text ((offset of "\"" in theText) + 1) thru -1 of theText
	
	--maximum and minimum temperatures found
	set today_min_temp to word 9 of sub_2
	set today_max_temp to word 12 of sub_2
	if t_format is equal to "C" then
		set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
		set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
	end if
	
	--looking for today forecast condition (a bit tricky)
	set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
	set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
	set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
	
	--looking for tomorrow forecast
	set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
	set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
	
	--maximum and minimum temperatures found
	set tomorrow_min_temp to word 9 of sub_6
	set tomorrow_max_temp to word 12 of sub_6
	if t_format is equal to "C" then
		set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
		set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
	end if
	
	--looking for tomorrow forecast condition (a bit tricky)
	set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
	set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
	set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8
	
	say "Outside it is, " & actual_condition & ", and " & actual_temp & " degrees " using "Serena"
	
	if v_format is equal to "L" then
		say "Today: " & today_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees. 
	Tomorrow: " & tomorrow_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees" using "Serena"
	else
		say "The Weather today in Oklahoma City will be: " & today_forecast & ", between " & today_min_temp & " , and " & today_max_temp & " degrees. 
	 Tomorrow: " & tomorrow_forecast & ", between " & tomorrow_min_temp & " ,and " & tomorrow_max_temp & " degrees" using "Serena"
	end if
	tell application "iTunes"
		set the sound volume to 70
		delay 1
		set the sound volume to 80
		delay 1
		set the sound volume to 90
		delay 1
		set the sound volume to 100
	end tell
	tell application "System Events"
		keystroke "f" using {control down, command down}
	end tell
	delay 2
	
end tell

All of this works well for me, but there are some things Im trying to add and Im hoping to get some help.

Im wanting to have it tell me how many new messages I have in my outlook inbox. I can get it to return the number, but when I add conditions to it (if unread count is greater than 0 then say “You have # of messages”) it returns with an error or ignores it completely.

tell application "Microsoft Outlook"
	get unread count of mail folder "inbox"
	say "Bryon you have (unread count) new messages this morning" using "Serena"
end tell

I also would like to set something up to ping the static ip address of my iPhone so it will tell when my phone shows up on the network, or when I come home. I know how to get it to ping, but where my mind is going numb is on figuring out how to set it up to ping until it finds it, tells me Welcome home, you sexy beast (or something to that effect :slight_smile: ) and then ping until the phone leaves and then starts over. Obviously I need it to continue pinging the ip, but i dont want it to continue to tell me welcome home.

third issue is the saying the RSS feed. I know there has been a lot written on this, but I just cant seem to get anywhere with the parsing and reading of the text. The feed Im trying to get it to read is http://www.biblegateway.com/usage/votd/rss/votd.rdf

another issue is getting it to tell meif I have any Facebook notifications.

Finally, I’m wondering if there is anyway to send a text using applescript? Our home alarm system can be set or disarmed by using text messaging. i would like to add it to my script to set the alarm system to home at night and when running the Good morning Script, it will disarm it automatically.

I know I have thrown a lot out there and I’m going to continue to work through these issues on my own, but if some of you experts could jump in and give me some help, I would greatly appreciate it!!

Bryon <><

Hi,

the Outlook part could be


tell application "Microsoft Outlook"
	set unreadCount to unread count of mail folder "inbox"
end tell
if unreadCount = 0 then
	set countMessage to "no new messages"
else if unreadCount = 1 then
	set countMessage to "one new message"
else
	set countMessage to (unreadCount as text) & " new messages"
end if
say "Bryon you have " & countMessage & " this morning" using "Serena"



Awesome! Much Thanks!

I also got the verse of the day feed to work this morning. i found a RSS script and changed some things up to get it to work.

set theSource to do shell script "curl [url=http://www.biblegateway.com/usage/votd/rss/votd.rdf]http://www.biblegateway.com/usage/votd/rss/votd.rdf"[/url]
set theQuote to (extractBetween(theSource, "<![CDATA[“", "”")) as text

set theTitle to (extractBetween(theSource, "<title>", "</title>")) as text

say "Your verse for today is found in" & theTitle
say theQuote

on replaceThings(txt, srch, repl)
	set AppleScript's text item delimiters to the srch
	set the item_list to every text item of txt
	set AppleScript's text item delimiters to the repl
	set txt to the item_list as string
	set AppleScript's text item delimiters to ""
	return txt
end replaceThings

to extractBetween(SearchText, startText, endText)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to startText
	set endItems to text of text item -1 of SearchText
	set AppleScript's text item delimiters to endText
	set beginningToEnd to text of text item 1 of endItems
	set AppleScript's text item delimiters to tid
	return beginningToEnd
end extractBetween

Anybody know a quick way to get it to say “first” when its written 1? For example First Peter when it sees 1 Peter?

Thanks
Bryon <><

Hi Bryon,

I don’t know if this example is quick, but it just concatenates the ordinal suffix to the verse number:


display dialog "Enter an integer:" default answer ""
set the_number to text returned of result
if the_number ends with "1" then
	set ord_suff to "st"
else if the_number ends with "2" then
	set ord_suff to "nd"
else if the_number ends with "3" then
	set ord_suff to "rd"
else
	set ord_suff to "th"
end if
say (the_number & ord_suff)

gl,

Just found out the computer automatically does it:


display dialog "Enter an integer:" default answer ""
set the_number to text returned of result
say (the_number & "th")

gl,

Hello.
I believe I snagged this handler from either the old applescript page of apple, or MacOsXAutomation.com

on add_numeric_suffix(this_num)
		set the list_index to (this_num mod 10) + 1
		set the num_suffix to item list_index of ¬
			{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
		return ((this_num as text) & the num_suffix)
	end add_numeric_suffix