How can I fix my Apple Script to prevent it from timing out
I am a new to apple script and am not sure how to fix this problem. Basically my script is a countdown timer that sends a message to my phone after the timer has ended. After the timer ends, the message is sent, and a dialog box waits for the user to enter the amount of time to set for the next timer.
I have noticed that when I am not connected to a power source the script times out if I don't enter something into the dialog box. I would like to fix this behavior so that it doesn't time out unless it has been 7 or more hours.
a) how can i do this?
b) is it safe to do this?
-------------------------------------------------------------------------------- ----------------------------------------------------
#Simple Times
#Contdown timer that can be used as a pomodoro timer
#Also able to send break notifications as a text to your phone
on get_txt_preference()
display alert "Send session information to phone?" buttons {"No", "Yes"}
if button returned of result = "yes" then
return true
else
return false
end if
end get_txt_preference
##########################################
#Globals
##########################################
set thePhoneNumber to "+X(XXX)XXX-XXXX" #change to your phone number
set shortBreak to 240 #4 m
set medBreak to 900 #15 m
set longBreak to 3600 #1 hour
set pomodoroVal to 1500 #25 m
set pomoSymbol to "
"
set sendTxtIsOn to get_txt_preference()
set dialogText to ((current date) as string) & "
_________________________________________
_________________________________________
[time] [m || s || h] 25 m, 3 s, 1 h
[minutes] 25
Minutes ||||| Seconds
25 1500
15 900
4 240
_________________________________________
_________________________________________
q to quit
"
global sendTxtIsOn
global shortBreak
global medBreak
global longBreak
global pomodoroVal
global currentPomo
global totalPomo
global pomoSymbol
global dialogText
global thePhoneNumber
###############################
#Functions
##########################################
on quit
display alert "Exit Simple Times?" buttons {"No", "Yes"}
if button returned of result = "yes" then
try
send_txt("Nice work. Pomodoros: " & totalPomo & "
" & "min: " & sec / 60 & "
" & "hour: " & (sec / 60) / 60)
end try
continue quit
end if
end quit
on increment_pomo(theString)
return theString & pomoSymbol
end increment_pomo
on make_pomo_summary(sec, mark)
set summary to (mark & "
" & "min: " & sec / 60) & "
" & "hour: " & ((sec / 60 / 60))
return summary
end make_pomo_summary
on make_break_summary()
set summary to "End of Break" & "
total: " & totalPomo & "
current: " & currentPomo
return summary
end make_break_summary
on send_txt(txt)
if sendTxtIsOn then
tell application "Messages" to activate
tell application "Messages"
sendtxttobuddythePhoneNumber of service "SMS"
end tell
end if
end send_txt
on is_number(x)
try
x as number
return true
on error
return false
end try
end is_number
on is_break(theTime)
return theTime = shortBreak or theTime = medBreak or theTime = longBreak
end is_break
on get_input()
with timeout of (60 * 420) seconds# 7 hour timeout
repeat while true
tell me
activate
set input to text returned of (display dialogdialogTextdefault answer "q")
set n to word 1 of input
end tell
set wordCount to count of (words of input)
if 1 ≤ wordCount and wordCount ≤ 2 and is_number(n) then
if wordCount = 1 then
set modifier to "m"
else
set modifier to character 1 of (word 2 of input)
end if
if modifier = "m" then
return n * 60 as number
else if modifier = "s" then
return n as number
else if modifier = "h" then
return n * 60 * 60 as number
end if
else if n = "q" then
quit
end if
end repeat
end timeout
end get_input
on main()
set totalPomo to ""
set currentPomo to ""
set sec to 0.0
repeat while true
set input to get_input()
repeat input times
delay 1
end repeat
say "times up"
if input = pomodoroVal then
set sec to sec + input
set currentPomo to increment_pomo(currentPomo)
set totalPomo to increment_pomo(totalPomo)
set dialogText to increment_pomo(dialogText)
send_txt(make_pomo_summary(sec, currentPomo))
if length of currentPomo = 4 then
set currentPomo to ""
set totalPomo to totalPomo & " "
set dialogText to dialogText & " "
end if
else if is_break(input) then
send_txt(make_break_summary())
end if
end repeat
end main
main()
--> Result: {button returned:"OK"}
MacBook (Retina, 12-inch, Early 2015), macOS Sierra (10.12.3), null