Current Position:Home > Can anybody fix this apple script for me so all the responses work

Can anybody fix this apple script for me so all the responses work

Update:10-11Source: network consolidation
Advertisement
iv been working on a jarvis wake up script and iv continued to add on commands to if theResponce parts of the script but now most of them wont work and i get a syntax if i dont have like 8 end if's at the end of the script could somebody please overview it, fix the script and resubmit it to me in the comments. will be so grateful if somebody fixes this pleasee.!!!!
set theHours to hours of the (current date)
if theHours > 18 then
          say "good evening sir"
else if theHours > 12 then
          say "good afternoon sir"
else if theHours > 6 then
          say "good Morning sir"
else if theHours > 0 then
          say "get out of bed sir!"
end if
say "It is " & getTimeInHoursAndMinutes() using "Tom"
on getTimeInHoursAndMinutes()
          set timeStr to time string of (current date)
          set Pos to offset of ":" in timeStr
          set theHour to characters 1 thru (Pos - 1) of timeStr as string
          set timeStr to characters (Pos + 1) through end of timeStr as string
  -- Get the "minute"
          set Pos to offset of ":" in timeStr
          set theMin to characters 1 thru (Pos - 1) of timeStr as string
          set timeStr to characters (Pos + 1) through end of timeStr as string
  --Get "AM or PM"
          set Pos to offset of " " in timeStr
          set theSfx to characters (Pos + 1) through end of timeStr as string
          return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes
set CityCode to 1098081
set t_format to "C"
set v_format to "S"
set a_format to "Y"
set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
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
set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
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
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
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
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
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
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
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
if a_format is equal to "Y" then
          say "The current conditions in Perth are " & actual_condition & " ,and the current temperture is " & actual_temp & " degrees"
end if
if v_format is equal to "L" then
          say "Today it will be : " & today_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees .
          Tomorrow we are expecting it to be: " & tomorrow_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees " using "Tom"
else
          say "Today it will be : " & today_forecast & ", between " & today_min_temp & " , and " & today_max_temp & " degrees .
           Tomorrow we are expecting it to be: " & tomorrow_forecast & ", between " & tomorrow_min_temp & " ,and " & tomorrow_max_temp & " degrees " using "Tom"
          say "will that be all sir?"
          tell application "SpeechRecognitionServer" to set theResponse to listen for {"get my mail ", "yes", "notes", "repeat", "Music", "Sleep in"}
          if theResponse is "Sleep in" then
  delay 6
                    say "Time to get up sir,or you will be late"
                    tell application "SpeechRecognitionServer" to set theResponse2 to listen for {"Ok pat im getting up"}
                    if theResponse2 is "Ok pat im getting up" then
  set volume 35
                              tell application "iTunes"
                                        set the sound volume to 0
  play user playlist "Wake up"
                                        repeat 10 times
                                                  if sound volume is less than 40 then
                                                            set sound volume to (sound volume + 10)
                                                            delay 2
                                                  end if
                                        end repeat
                              end tell
                              if theResponse is "Music" then
  set volume 20
                                        tell application "iTunes"
                                                  set the sound volume to 0
  play user playlist "Wake up"
                                                  repeat 10 times
                                                            if sound volume is less than 60 then
                                                                      set sound volume to (sound volume + 10)
                                                                      delay 2
                                                            end if
                                                  end repeat
                                        end tell
                                        if theResponse is "yes" then
                                                  delay 1
                                                  set theOptions to {"very good, sir, have a nice day"}
                                                  set theChoice to some item of theOptions
  say theChoice displaying theChoice with waiting until completion
                                        end if
                                        if theResponse is "get my mail " then
                                                  tell application "Mail" to launch
                                        end if
                                        if theResponse is "notes" then
                                                  tell application "Stickies" to launch
                                        end if
                                        if theResponse is "repeat" then
                                                  set CityCode to 1098081
                                                  set t_format to "C"
                                                  set v_format to "S"
                                                  set a_format to "Y"
                                                  set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
                                                  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
                                                  set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
                                                  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
                                                  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
                                                  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
                                                  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
                                                  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
                                                  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
                                                  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
                                                  if a_format is equal to "Y" then
                                                            say "The current conditions in Perth are " & actual_condition & " ,and the current temperture is " & actual_temp & " degrees"
                                                  end if
                                                  if v_format is equal to "L" then
                                                            say "Today it will be : " & today_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees .
          Tomorrow we are expecting it to be: " & tomorrow_forecast & ". Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees " using "Tom"
                                                  else
                                                            say "will that be all sir?"
                                                            tell application "SpeechRecognitionServer" to set theResponse3 to listen for {"get my mail ", "yes", "notes", "Music"}
                                                            if theResponse3 is "Music" then
                                                                      set volume 20
                                                                      tell application "iTunes"
                                                                                set the sound volume to 0
                                                                                play user playlist "Wake up"
                                                                                repeat 10 times
                                                                                          if sound volume is less than 60 then
                                                                                                    set sound volume to (sound volume + 10)
                                                                                                    delay 2
                                                                                          end if
                                                                                end repeat
                                                                      end tell
                                                            else
                                                                      if theResponse3 is "get my mail " then
                                                                                tell application "Mail" to launch
                                                                      end if
                                                                      if theResponse3 is "yes" then
                                                                                delay 1
                                                                                set theOptions to {"very good, sir, have a nice day"}
                                                                                set theChoice to some item of theOptions

The Best Answer

Advertisement
What you do in this situation, is split the file into parts. 
Start with:
set theHours to hours of the (current date)
if theHours > 18 then
          say "good evening sir"
else if theHours > 12 then
          say "good afternoon sir"
else if theHours > 6 then
          say "good Morning sir"
else if theHours > 0 then
          say "get out of bed sir!"
end if
Add in a few lines of code to this file and see what happens. 
add the on run, this would be clearer.  It is the default on unit to run.
on run
    set theHours to hours of the (current date)
    if theHours > 18 then
        say "good evening sir"
    else if theHours > 12 then
        say "good afternoon sir"
    else if theHours > 6 then
        say "good Morning sir"
    else if theHours > 0 then
        say "get out of bed sir!"
    end if
end run
Now, it is time for debugging.
It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
    Author: rccharles
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
end run