LoginSignup
16
10

More than 5 years have passed since last update.

Macの暇具合を調べる方法

Posted at

Macが暇そうな時にだけスクリプトを動かしたかった。
こんな感じで、画面の明るさ、アイドル時間、通信量を調べられる。


require "open3"

module IsMacIdle
  module_function
  def idle_time
    `ioreg -c IOHIDSystem`.scan(/HIDIdleTime" = (\d+)/)[0][0].to_f / 1000_000_000
  end

  def brightness
    `ioreg -c AppleBacklightDisplay`.scan(/"brightness"[^}]+=(\d+)}/)[0][0].to_f
  end

  def net_traffic(sec = 1)
    Open3.popen3("netstat -i -w #{sec}") do |i, o, e, th|
      o.gets
      o.gets
      a = o.gets.split.map(&:to_i)
      th.kill
      {in: a[2], out: a[5]}
    end
  end
end

if __FILE__ == $0
  p IsMacIdle::brightness
  p IsMacIdle::idle_time
  p IsMacIdle::net_traffic
end

16
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
10