Display dialog if swap is used

Recently got a refurbished M1 MacBook Pro with 16 GB RAM.

My Mac Studio often uses more than 16 GB which is fine, but I don’t want the MacBook to use swap (see Tracking swap space: is it wearing out your SSD? – The Eclectic Light Company).

This AppleScript displays a dialog if swap is used. I use it in a periodic Keyboard Maestro macro. It shows the currently used swap and a button that opens Activity Monitor.app .

Set property testing to true if you want to check whether the macro works as expected.

-- Display dialog if swap is used

property testing : false
property theDialog_IconPath : (((path to application "Activity Monitor") & "Contents:Resources:AppIcon.icns:") as string)

if testing = false then
	set theSwapouts to do shell script "sysctl vm.swapusage | cut -d' ' -f8"
else
	set theSwapouts to "155.00M" -- testing 
end if

if theSwapouts ≠ "0.00M" then
	tell application "System Events"
		set theDialog_Buttons to {"Cancel", "Open Activity Monitor"}
		set theDialog_Text to linefeed & "Swap usage: " & theSwapouts & linefeed
		set theDialog_ButtonReturned to button returned of (display dialog theDialog_Text with title "Activity Monitor" buttons theDialog_Buttons default button 2 cancel button 1 with icon (theDialog_IconPath as alias) giving up after 30)
		if theDialog_ButtonReturned = (item 2 of theDialog_Buttons) then
			tell application "Activity Monitor"
				activate
			end tell
		end if
	end tell
end if

Swap usage.kmmacros (3.0 KB)