Create a gist now

Instantly share code, notes, and snippets.

A simple test to load a series of web pages in Chrome on MacOS repeatedly until the battery runs out.
on run
set imessage_id to "" -- leave blank to skip sending periodic iMessage progress updates, put your iMessage ID here to get updates as the script runs
set delay_seconds to 5 -- Don't set to 0, it runs too fast. 0.75 is a good setting for "go fast".
set computer_name to do shell script "scutil --get ComputerName"
set os_version to do shell script "sw_vers -productVersion"
set os_build to do shell script "sw_vers -buildVersion"
set _uptime to do shell script "uptime"
set url_list to {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
-- Short list of URLs for testing, comment out for real testing:
#set url_list to {"", "", ""}
if not IsRunningOnBattery() then
display alert "Not running on battery power" message "This test is meant to measure battery life. Unplug from AC Power and double-check display brightness level before proceeding." as critical buttons {"Cancel", "Proceed"} default button "Proceed"
if button returned of the result is not "Proceed" then return -- stop script
end if
-- Logging Preamble
WriteToLog("") -- blank line to separate log entries
WriteToLog("Starting Chrome battery test on " & computer_name & linefeed & (current date))
WriteToLog("OS version: " & os_version & " (" & os_build & ")")
WriteToLog("Battery: " & my GetBatteryPercentage())
WriteToLog("Display brightness: " & my GetDisplayBrightness())
WriteToLog("Uptime: " & _uptime)
WriteToLog("Running apps: " & my GetRunningApps())
tell application "Google Chrome"
set _cycles to 0
set start_time to current date
close every window -- start each cycle by closing any open windows
set _cycles to _cycles + 1
repeat with _url in url_list
open location _url
delay delay_seconds
repeat 3 times
my PageDown()
delay delay_seconds
end repeat
end repeat
set elapsed_seconds to (current date) - start_time
set elapsed_str to my SecondsToHoursMinutes(elapsed_seconds)
set log_msg to (elapsed_str & ": " & "(" & _cycles & " cycles): " & my GetBatteryPercentage()) as string
my WriteToLog(log_msg)
-- Send text message update only every Nth cycle:
if (_cycles mod 3 is 0) and (imessage_id is not "") then my SendiMessage(computer_name & ": " & log_msg, imessage_id)
delay 0 -- How long to wait before starting all over again.
end repeat
end tell
end run
on PageDown()
tell application "System Events"
key code 121
-- 121: page down
-- 116: page up
end tell
end PageDown
on GetBatteryPercentage()
-- Returns battery percentage with the % symbol: "100%"
set _battery to do shell script "pmset -g batt | egrep -o '\\d+%'"
on error
set _battery to "(No battery)"
end try
return _battery
end GetBatteryPercentage
on GetDisplayBrightness()
set brightness_info to do shell script "ioreg -c AppleBacklightDisplay -r -w 0 | egrep -o '\"brightness\"={.+?}'"
-- Example for words of brightness_info:
-- `{"brightness", "=", "max", "=", "1024", "min", "=", "0", "value", "=", "704"}`
if word 3 of brightness_info is "max" then set b_max to word 5 of brightness_info
if word 9 of brightness_info is "value" then set b_val to word 11 of brightness_info
set current_brightness to (b_val as real) / b_max as real
on error
set current_brightness to "(indeterminate)"
end try
return current_brightness
end GetDisplayBrightness
on GetRunningApps()
tell application "System Events" to set running_apps to name of every application process whose background only is false
set old_TID to text item delimiters
set text item delimiters to ", "
set running_apps to running_apps as string
set text item delimiters to old_TID
return running_apps
end GetRunningApps
on IsRunningOnBattery()
set _result to do shell script "pmset -g batt | egrep 'Now drawing from'"
on error
display alert "Error determining if running on battery."
end try
if _result contains "Battery Power" then
return true
return false
end if
end IsRunningOnBattery
on WriteToLog(_msg as string)
do shell script "echo " & quoted form of _msg & " >> ~/Desktop/ChromeBatteryTest.log"
end WriteToLog
on SendiMessage(msg_text as string, msg_recipient as string)
tell application "Messages"
send msg_text to buddy msg_recipient of (service 1 whose service type is iMessage)
end tell
end SendiMessage
on SecondsToHoursMinutes(_secs)
-- Convert seconds to "h:mm" string
set _hours to _secs div (60 * 60)
set _minutes to (_secs div 60) mod 60
return (_hours & "h:" & AddLeadingZeros(_minutes, 1) & "m") as string
end SecondsToHoursMinutes
on AddLeadingZeros(this_number, maxleading_zeros)
-- Source:
set the threshold_number to (10 ^ maxleading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (maxleading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
return this_number as text
end if
end AddLeadingZeros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment