Class: MyScripts::Wake

Inherits:
Script
  • Object
show all
Defined in:
lib/my_scripts/scripts/wake.rb

Overview

This script prevents screen saver from locking down Windows by randomly moving mouse pointer a bit every 4 minutes or so. Why would you need this? Well, if your XP box is in a domain with security-crazy admins who have immutable policy of forced screen saver after 5 minutes of inactivity you’d feel the pain…

Constant Summary collapse

VERSION =
'0.1.0'
SLEEP_TIME =

seconds

4 * 60

Instance Method Summary collapse

Methods inherited from Script

#error, #gets, #puts, #system, #to_s, #usage, #version

Constructor Details

#initialize(name, cli, argv, argf) ⇒ Wake

Returns a new instance of Wake.



12
13
14
15
16
17
# File 'lib/my_scripts/scripts/wake.rb', line 12

def initialize( name, cli, argv, argf )
  require 'win/gui/input'
  require 'win/error'
  self.class.send(:include, Win::Gui::Input)
  super
end

Instance Method Details

#move_mouse_randomlyObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/my_scripts/scripts/wake.rb', line 19

def move_mouse_randomly
  x, y = get_cursor_pos

  # For some reason, x or y returns as nil sometimes
  if x && y
    x1, y1 = x + rand(3) - 1, y + rand(3) - 1
    mouse_event(MOUSEEVENTF_ABSOLUTE, x1, y1, 0, 0)
    puts "Cursor positon set to #{x1}, #{y1}"
  else
    puts "X: #{x}, Y: #{y}, last error: #{Win::Error::get_last_error}"
  end
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/my_scripts/scripts/wake.rb', line 32

def run
  case @argv.size
    when 0
      sleep_time = SLEEP_TIME
    when 1
      sleep_time = @argv.first.to_f * 60
    else
      usage "[minutes] - prevents screen auto lock-up by moving mouse pointer every [(4) minutes]"
  end

  loop do
    move_mouse_randomly
    sleep sleep_time
  end
end