Applescript报警在特定时间运行并说出温度

时间:2013-12-05 14:31:02

标签: html applescript

我已经写了一些AppleScript来唤醒我早上(here),它工作正常,但是,我想添加一些功能。我希望脚本访问this网页,并告诉我当前的温度。现在,下面是上述网站温度的元素。

 <span id="rapidtemp" class="pwsrt" pwsid="IWESTERN183" pwsunit="metric" pwsvariable="tempf" english="°F" metric="°C" value="71.8">
   <span class="nobr"> <span class="b">22.1</span>&nbsp;°C</span>
 </span>

我想知道如何在AppleScript上定义一个变量,如本网站所述的温度。

另一件我想知道怎么做的事情,就是我怎么可能,比如当我上床睡觉时运行这个脚本,所以它等到06:00,然后运行。

非常感谢任何帮助。感谢。

- 迈克尔

2 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

set myUrl to "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=-31.953005,115.857468"
set myTemp to text 2 thru -2 of (do shell script "curl " & quoted form of myUrl & " | grep 'span id=\"rapidtemp\"' | grep -Eo '\"[0-9]+\\.?[0-9]*\"'")

您可以使用launchd来安排这些事件。 Lingon会帮助你。

答案 1 :(得分:1)

如果这仍然相关,我就是这样做的:

on run {input, parameters}
    --this is the city code. Search the code for your city on http://weather.yahoo.com/
    set CityCode to 12765838
    --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 "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%2012765838&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"

    --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 sub_1a to text ((offset of "text" in sub_1) + 1) thru -1 of sub_1
    set sub_1b to text ((offset of "\"" in sub_1a) + 1) thru -1 of sub_1a
    set actual_condition to text 1 thru ((offset of "\"" in sub_1b) - 1) of sub_1b

    --looking for actual temperature temperature
    set sub_1c to text ((offset of "temp=" in sub_1)) thru -1 of sub_1
    set sub_1d to text ((offset of "\"" in sub_1c) + 1) thru -1 of sub_1c
    set actual_temp to text 1 thru ((offset of "\"" in sub_1d) - 1) of sub_1d

    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 19 of sub_2
    set today_max_temp to word 22 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 19 of sub_6
    set tomorrow_max_temp to word 22 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

    --VoiceOver Section
    set myTime to time string of (current date)
    set myParts to words of myTime
    set mySpeak to (item 1 of myParts) & " " & (item 2 of myParts) & " "

    if a_format is equal to "Y" then
        say "Outside it is, " & actual_condition & ", and " & actual_temp & " degrees "
    end if
    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"
    else
        say "Today: " & today_forecast & ", between " & today_min_temp & " , and " & today_max_temp & " degrees. 
     Tomorrow: " & tomorrow_forecast & ", between " & tomorrow_min_temp & " ,and " & tomorrow_max_temp & " degrees"
    end if
end run
相关问题