global timeBeforeComputerIsNotInUse, computerIsInUse, previousIdleTime
on run
set timeBeforeComputerIsNotInUse to 60 -- 1 minute
set computerIsInUse to true
set previousIdleTime to 0
end run
on idle
set idleTimeStr to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'") as string
set {idleTime, flag} to my stringToNumber(idleTimeStr as text)
if not computerIsInUse then
if idleTime is less than previousIdleTime then
set computerIsInUse to true
do shell script "defaults write com.apple.touchbar.agent PresentationModeGlobal -string appWithControlStrip"
do shell script "pkill “Touch Bar Agent”" user name "yourUserName" password "yourPassword" with administrator privileges
end if
else if idleTime is greater than or equal to timeBeforeComputerIsNotInUse then
set computerIsInUse to false
do shell script "defaults write com.apple.touchbar.agent PresentationModeGlobal -string app"
do shell script "pkill “Touch Bar Agent”" user name "yourUserName" password "yourPassword" with administrator privileges
end if
set previousIdleTime to idleTime
return 1
end idle
on stringToNumber(s)
try
s as number
on error
if 5 > (system attribute "sys2") then
set |insécable| to ASCII character 202
else
set |insécable| to character id 160
end if
set |autorisés| to characters of "123456789,.+-'E" & space & |insécable|
set chars to characters of s
set est_un_nombre to true
repeat with c in chars
if c is not in |autorisés| then
set est_un_nombre to false
exit repeat
end if
end repeat
if not est_un_nombre then return {s, false}
set |déci_local| to character 2 of (0.5 as text)
if |déci_local| is "," then
set |déci_étranger| to "."
else
set |déci_étranger| to ","
end if
(*
remove possible thousands separators
*)
if (s contains |déci_local|) and (s contains |déci_étranger|) then
if (offset of |déci_étranger| in s) > (offset of |déci_local| in s) then
set s to my remplace(s, |déci_local|, "")
else
set s to my remplace(s, |déci_étranger|, "")
end if
end if
(*
Replace wrong separator by the local one
*)
if s contains space then set s to my supprime(s, space)
if s contains "'" then set s to my supprime(s, "'")
if s contains |insécable| then set s to my supprime(s, |insécable|)
if s contains |déci_étranger| then set s to my remplace(s, |déci_étranger|, |déci_local|)
if s starts with "+" then set s to text 2 thru -1 of s
end try
return {s, true}
end stringToNumber
--=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d1
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace