I wrote an AppleScript, called with Automator (macOS 14.6, Sonoma, on an M1 Mac). I wrote a code for reminding myself to turn off Bluetooth after 120 seconds, if it is still on.
I need some help to understand what’s wrong. I think the problem is related to the use of shell script date +%s
as a number for doing algebraic operations.
I’m not that familiar with this coding language so probably there’s some other way to go in this same directions.
global Cnt
global Cnt2
global Cnt3
global diff
global curTime
global btOnTime
set Cnt to 0
set Cnt2 to 0
set Cnt3 to 0
set btOnTime to do shell script "date +%s"
set curTime to do shell script "date +%s"
set diff to (curTime as integer) - (btOnTime as integer)
set btStatus to 0
repeat
repeat until btStatus = 0
try
do shell script "system_Profiler SPBluetoothDataType | grep -i \"State: On\""
set btStatus to 1
set btOnTime to do shell script "date +%s"
on error
set btStatus to 0
end try
-- delay 110
set Cnt2 to (Cnt2 + 1)
end repeat
return display alert "BT ON. OUT OF CICLE 1. Cnt2: " & Cnt2
set curTime to do shell script "date +%s"
repeat while diff < 120
set curTime to do shell script "date +%s"
set diff to (curTime as integer) - (btOnTime as integer)
set Cnt3 to (Cnt3 + 1)
-- delay 110
end repeat
return display alert "BT ON. OUT OF CICLE 2. Cnt3: " & Cnt3
if diff > 120 and Cnt = 0 then
-- set recentRestart to (curTime - btOnTime) < 10
-- if not recentRestart then
return display notification "Stupendo, SPEGNI IL BLUETOOTH!!" with title "Bluetooth acceso da un BOTTO"
set Cnt to (Cnt + 1)
-- end if
end if
end repeat