Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Applescript to notify when RAM on mac is using compression or swaps. This is useful when you have SSD installed
-- Split string by delimiter
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
-- Get current memory stats
set vmstat to do shell script "vm_stat"
set splitStat to splitText(vmstat, ".")
-- list of memory features to check if not zero
set memList to {"Compressions", "Decompressions", "Swapins", "Swapouts"}
set notif to ""
repeat with a from 1 to length of splitStat
set theCurrentListItem to item a of splitStat
considering case
repeat with b from 1 to length of memList
set memItemname to item b of memList
-- Check each memory feature with the list of features provided above
if theCurrentListItem contains memItemname then
-- Get count of the feature
set compressionsList to splitText(theCurrentListItem, ":")
set compressionsNum to item 2 of compressionsList
set compressionsNum to splitText(compressionsNum, space)
set compressionsNum to compressionsNum's last item
-- check if the count is not equal to zero
if compressionsNum is not equal to "0" then
set notif to notif & "
" & memItemname & " " & compressionsNum
end if
end if
end repeat
end considering
end repeat
-- Show notification if count of anything is not zero
if notif is not equal to "" then
display notification notif with title "RAM Overuse!"
end if

Steps to setup the applescript to run every hour

  1. Open Terminal

  2. Run crontab -e

  3. Add this line-

0 * * * * osascript <FULL_PATH_TO_SCRIPT>/RAM_Overuse_Notification.applescript
  1. save and exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment