Last active
February 7, 2023 21:25
-
-
Save mcharo/98ffd253c4093da83eaef3e761dfbc22 to your computer and use it in GitHub Desktop.
Ping output to macOS CapsLock LED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env pwsh | |
| [CmdletBinding()] | |
| param( | |
| $IpAddress, | |
| $Count = 4 | |
| ) | |
| @' | |
| var capsState; | |
| function run(argv) { | |
| capsState = argv[0] == '1'?1:0; | |
| ObjC.import("IOKit"); | |
| ObjC.import("CoreServices"); | |
| (() => { | |
| var ioConnect = Ref(); | |
| var state = Ref(); | |
| $.IOServiceOpen( | |
| $.IOServiceGetMatchingService( | |
| $.kIOMasterPortDefault, | |
| $.IOServiceMatching( | |
| $.kIOHIDSystemClass | |
| ) | |
| ), | |
| $.mach_task_self_, | |
| $.kIOHIDParamConnectType, | |
| ioConnect | |
| ); | |
| // GetModifierLockState seems to be broke | |
| // $.IOHIDGetModifierLockState(ioConnect, $.kIOHIDCapsLockState, state); | |
| // this.console.log(state); | |
| $.IOHIDSetModifierLockState(ioConnect, $.kIOHIDCapsLockState, capsState); | |
| $.IOServiceClose(ioConnect); | |
| })(); | |
| } | |
| '@ | Out-File -Path /tmp/caps.jxa | |
| ping $IpAddress -c $Count | Where-Object { $PSItem -match 'from' } | ForEach-Object { | |
| ($PSItem -split '[=\ ]')[-2] | Tee-Object -Variable on | |
| $off = 1000 - $on | |
| osascript -l JavaScript /tmp/caps.jxa 1 | |
| Start-Sleep -Milliseconds $on | |
| osascript -l JavaScript /tmp/caps.jxa 0 | |
| Start-Sleep -Milliseconds $off | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| PING_TARGET="${1:-google.com}" | |
| cat << EOF > /tmp/toggle.jxa | |
| var capsState; | |
| function run(argv) { | |
| capsState = argv[0] == '1'?1:0; | |
| ObjC.import("IOKit"); | |
| ObjC.import("CoreServices"); | |
| (() => { | |
| var ioConnect = Ref(); | |
| var state = Ref(); | |
| $.IOServiceOpen( | |
| $.IOServiceGetMatchingService( | |
| $.kIOMasterPortDefault, | |
| $.IOServiceMatching( | |
| $.kIOHIDSystemClass | |
| ) | |
| ), | |
| $.mach_task_self_, | |
| $.kIOHIDParamConnectType, | |
| ioConnect | |
| ); | |
| // GetModifierLockState seems to be broke | |
| // $.IOHIDGetModifierLockState(ioConnect, $.kIOHIDCapsLockState, state); | |
| // this.console.log(state); | |
| $.IOHIDSetModifierLockState(ioConnect, $.kIOHIDCapsLockState, capsState); | |
| $.IOServiceClose(ioConnect); | |
| })(); | |
| } | |
| EOF | |
| ping $PING_TARGET -c 4 -W 1000 | grep from --line-buffered | while read line; do | |
| echo $line | |
| on=$(echo $line | awk -F'[ =.]' '{ print $13 }') | |
| off=$((1000-$on)) | |
| on_ms=$(printf "0.%03d\n" $on) | |
| off_ms=$(printf "0.%03d\n" $off) | |
| osascript -l JavaScript /tmp/toggle.jxa 1 | |
| sleep $on_ms | |
| osascript -l JavaScript /tmp/toggle.jxa 0 | |
| sleep $off_ms | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the value of
statethat's set byIOHIDGetModifierLockStateis always false for me. Any clue what's wrong?