Hello,
Here’s the apple script:
using terms from application "iChat"
on received text invitation theText from theBuddy for theChat
accept theChat
end received text invitation
on message received theMessage from theBuddy for theChat
if theMessage is "Hello" then
send "Hi" to theBuddy
else
send "???" to theBuddy
end if
end message received
end using terms from
I’m trying to make is respond to ichat ims. But no luck… When I press run, it runs for about a second then stops running so I think I need a repeat somewhere in the code.
Any ideas?
Thanks,
Elijah
Model: MacBookPro
AppleScript: 2.3
Browser: Firefox 3.6
Operating System: Mac OS X (10.6)
You’ll have to connect the code to iChat in it’s preferences. You don’t click “Run” in AppleScript Editor.
Thanks! Here is updated code:
set CityCode to 11050
--temperature format
set t_format to "F"
--voiceover format
set v_format to "A"
--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
using terms from application "iChat"
on message received theMessage from theBuddy for theChat
if theMessage is "current weather" then
send today_forecast to theBuddy
end if
if theMessage is "hello" then
send "hey" to theBuddy
end if
end message received
end using terms from
When I send the command current weather over ichat I get this error:
Event: Message Received
File: ICHAT.scpt
Error: The variable today_forecast is not defined.
But, when I send the command hello, it responds correctly with hey.
Any ideas?
Elijah
Model: MacBookPro
AppleScript: 2.3
Browser: Firefox 3.6
Operating System: Mac OS X (10.6)
That’s because you put the weather retreval part of your script outside of the “on message received theMessage from theBuddy for theChat” handler. Put ALL of your code into the “on message received theMessage from theBuddy for theChat.”
Try this.
on weather(CityCode)
--temperature format
set t_format to "F"
--voiceover format
set v_format to "A"
--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
return text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
end weather
using terms from application "iChat"
on message received theMessage from theBuddy for theChat
if theMessage starts with "current weather for " then
send "Loading..." to theBuddy
send weather(text (offset of " for " in theMessage) thru -1 of theMessage) to theBuddy
else if theMessage is "hello" then
send "Hey!" to theBuddy
else
send "I don't understand you." to theBuddy
end if
end message received
end using terms from
IT WORKS! Thanks!
Now how do I have the CityCode change based on my google latitude information?
Model: MacBookPro
AppleScript: 2.3
Browser: Firefox 3.6
Operating System: Mac OS X (10.6)
Sorry, I did something wrong. Try it again. Now it can be any area code you want.