Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active August 24, 2019 15:33
Show Gist options
  • Save snipsnipsnip/14327 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/14327 to your computer and use it in GitHub Desktop.
ramen timer using windows schtasks command
class SchTasks
def initialize(password = nil)
@password = password
end
def include?(taskname)
schtasks('/query', '/nh') do |f|
f.read =~ /^#{taskname}/
end
end
def create(taskname, time, cmd, freq)
hms = time.strftime('%H:%M:%S')
ymd = time.strftime('%Y/%m/%d')
schtasks('/create',
'/tn', taskname,
'/sc', freq,
'/sd', ymd,
'/st', hms,
'/tr', %_"#{cmd}"_) do |f|
f.puts @password if @password
end
end
private
def schtasks(*args, &blk)
open("|schtasks #{args.join ' '}", 'r+', &blk)
end
end
if __FILE__ == $0
require 'optparse'
autoload :Win32API, 'Win32API'
def main
opts = parse_opts
if opts[:messagebox]
msg = opts[:message]
api = Win32API.new('user32', 'MessageBoxA', 'pppi', 'i')
api.call(nil, msg, 'moo', 0)
return
end
name = opts[:name] || 'moo'
time = make_time(opts[:minute])
cmd = make_cmd(opts[:message] || "moo")
tasks = SchTasks.new(opts[:pass])
name = name.succ while tasks.include?(name)
tasks.create(name, time, cmd, :once)
end
def make_cmd(msg)
$:.grep(/site_ruby\z/).min {|a,b| a.size <=> b.size } =~ /lib/
ruby = "#{$`}bin/rubyw.exe".tr('/', '\\')
script = File.expand_path($0).tr('/', '\\')
%_"""#{ruby}""" -x """#{script}""" --messagebox -m """#{msg}"""_
end
def make_time(minute)
Time.now + minute.to_i * 60
end
def parse_opts
opts = {}
ARGV.options do |o|
o.on '--messagebox' do |opts[:messagebox]| end
o.on '-p', '--pass=PASSWORD' do |opts[:pass]| end
o.on '-t', '--time=MINUTE' do |opts[:minute]| end
o.on '-n', '--name', 'task name' do |opts[:name]| end
o.on '-m', '--message=MSG' do |opts[:message]| end
ARGV.parse!
unless opts[:pass] && opts[:minute] || opts[:messagebox] && opts[:message]
puts o
exit 1
end
end
opts
end
main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment