I have my app running just fine. The basic app is this, 2 buttons, Start and Stop. Click on Start and the app starts checking for the current state of the power source. In theory, click on Stop and the app stops. The problem is, you can’t click on Stop. My coding is such that it is running so fast continually calling the PowerStatus handler that the buttons are never checked and can’t even be clicked on.
I am new to coding in Applescript ObjC. What I want is to have a routine that continues to run until the condition of “Stop button = Enabled”
This is my code as is now and will show how new I am to this.
script Power_DetectorAppDelegate
property parent : class "NSObject"
property startButton : missing value
property stopButton : missing value
property checkTime : integer
property currTime : integer
on awakeFromNib()
set checkTime to 0
end awakeFromNib
on clickStartButton_(sender)
set statStartButton to my startButton's |state|() as integer
repeat while (statStartButton = 1)
set the title of startButton to "Running"
set the title of stopButton to "Stop"
my startButton's setEnabled_(false)
my stopButton's setState_(false)
set statStopButton to 0
set currTime to time of (current date) as integer
delay 5
getPowerStatus_()
end repeat
end clickStartButton_
on getPowerStatus_()
set PowerSource to (do shell script "pmset -g ps")
if currTime < 300 then
set checkTime to 0 --resets for midnight
end if
if PowerSource does not contain "AC Power" then
if (currTime > checkTime + 300) then --check every 5 minutes (300 seconds)
--send Ryan alerts
set recipientRyanEmail to "myname@mac.com"
set recipientRyanSMS to "myphonenum@txt.att.net"
set recipientRyanName to "Ryan"
set theSubject to "POWER ALERT!"
set theContent to return & "Power issues at the FIC!"
try
tell application "Mail"
--Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:false}
--Set a recipient
tell theMessage
--messages to Ryan
make new to recipient with properties {name:recipientRyanName, address:recipientRyanEmail}
send
end tell
end tell
end try
set checkTime to time of (current date) as integer
end if --(currTime > checkTime + 300)
end if --PowerSource does not contain "AC Power"
set currTime to time of (current date) as integer
end getPowerStatus_
on clickStopButton_(sender)
set statStopButton to my stopButton's |state|() as integer
set the title of startButton to "Start"
set the title of stopButton to "Stopped"
my startButton's setState_(false)
my startButton's setEnabled_(true)
set statStartButton to 0
end clickStopButton_
on idle
display dialog "Hello"
end idle
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script