Created
December 2, 2010 12:50
-
-
Save RobertAudi/725228 to your computer and use it in GitHub Desktop.
`pmset schedule` wrapper to easily schedule OS X to sleep, shutdown, etc
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 ruby | |
# NOTES: | |
# - This script only works on OS X. | |
# - This script was only tested with ruby 1.9.2 using | |
# the aaronh-chronic gem. If the chronic gem is used | |
# instead, this script will not work (unless the chronic | |
# gem was updated to work with ruby 1.9.2). | |
require 'rubygems' | |
require 'chronic' | |
require 'rainbow' | |
# ------------------------------------------------------------------------------ | |
actions = ["sleep","wake","poweron","shutdown","wakeorpoweron"] | |
this_is_a_test = false # Set this to true if you just want to test the script | |
add_colors_to_output = true | |
action_color = :green | |
time_color = :red | |
# ------------------------------------------------------------------------------ | |
# ------------------------------------------------------------------------------ | |
def list_actions(actions = ["sleep","wake","poweron","shutdown","wakeorpoweron"]) | |
puts "List of valid actions:" | |
actions[0] = "\t#{actions[0]}" | |
puts actions.join("\n\t") | |
end | |
def show_help | |
puts "Usage: please [action] [time]" | |
puts "Example: please sleep in 3 hours" | |
puts "\n" | |
list_actions | |
puts "\nList of valid times:\n\tAnything that chronic can parse\n\t(See https://github.com/mojombo/chronic)" | |
end | |
# ------------------------------------------------------------------------------ | |
if ARGV.empty? | |
show_help | |
exit | |
end | |
action = ARGV[0] | |
if action == "test" && actions.include?(ARGV[1]) | |
this_is_a_test = true | |
ARGV.shift | |
action = ARGV[0] | |
elsif action == "help" | |
# TODO: show help for each valid action | |
show_help | |
exit | |
elsif !actions.include?(action) | |
puts "Unknow action: #{action}" | |
list_actions | |
exit | |
end | |
time_pieces = ARGV[1..-1] | |
if time_pieces[0] == "at" | |
time_pieces.shift | |
time_pieces.unshift("tomorrow at") if Chronic.parse(time_pieces.join(" ")) < Time.now | |
end | |
time_string = time_pieces.join(" ") | |
chronic_time = Chronic.parse(time_string) | |
time = chronic_time.strftime("%m/%d/%Y %H:%M:%S") | |
if this_is_a_test | |
if add_colors_to_output | |
puts "OS X would #{action.color(action_color)} #{time_string.color(time_color)} (#{time.color(time_color)})" | |
else | |
puts "OS X would #{action} #{time_string} (#{time})" | |
end | |
exit | |
else | |
if add_colors_to_output | |
exec("sudo pmset schedule #{action} \"#{time}\" && echo 'OS X will #{action.color(action_color)} #{time_string.color(time_color)} (#{time.color(time_color)})'") | |
else | |
exec("sudo pmset schedule #{action} \"#{time}\" && echo 'OS X will #{action} #{time_string} (#{time})'") | |
end | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment