Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 124 lines (86 sloc) 3.21 KB
#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
require 'yaml'
DOLLA_ZERO = File.basename $0
DOC_RUN_CONTROL = "~/.#{DOLLA_ZERO}rc"
RUN_CONTROL = File.expand_path DOC_RUN_CONTROL
USAGE = """
`#{DOLLA_ZERO}(1)` -- mac os x window layout utility
===============================================
## synopsis
#{DOLLA_ZERO} [-hg] [-a <app1>,<app2>,...] [<profile>]
## description
nerd alert! `#{DOLLA_ZERO}(1)` repositions the windows of your applications to
your preferred layout.
## getting the positions and sizes of windows:
executing the following:
#{DOLLA_ZERO} -g -a Safari,Terminal,Twitter,MacVim
will get the current positions and sizes of the listed applications, and write
them to your `#{DOC_RUN_CONTROL}` file.
## setting the positions and sizes of windows:
executing the following:
#{DOLLA_ZERO}
will read the positions of the files in `#{DOC_RUN_CONTROL}` and position and
size the apps accordingly.
## installation
gem install repozish
## other notes
`#{DOLLA_ZERO}(1)` only operates on the front window of applications. it may
not work as desired with applications that have more than one window.
`#{DOLLA_ZERO}(1)` will first use the application list passed in with the '-a'
flag. if the '-a' flag isn't given, it will use the apps listed in
`#{DOC_RUN_CONTROL}`. otherwise it'll just like crash or something.
you may optionally specify a profile name. for instance if you want a different
window layout when you're doing a preso or some crap:
#{DOLLA_ZERO} -ga Safari,Terminal,Twitter,MacVim alt_layout
#{DOLLA_ZERO} alt_layout
when you omit the profile, `#{DOLLA_ZERO}(1)` picks a default profile name
that's specific to the monitor configuration. so as long as you've set up
profiles for the monitors that are currently plugged in, you can just run:
#{DOLLA_ZERO}
and it will choose the appropriate layout for the attached screens.
## options
"""
def set_dims(app, dims)
`osascript -e 'tell application "#{app}" to set the bounds of the front window to {#{dims.join ', '}}'`
end
def get_dims(app)
`osascript -e 'tell application "#{app}" to get the bounds of the front window'`
end
def displays()
display_data = YAML.load(`system_profiler SPDisplaysDataType`)
display_data['Graphics/Displays'].map do |d|
monitors = d[1]['Displays']
if monitors && monitors.length
monitors.map{|m| m[1]['Resolution']}.join('').gsub(' ', '')
end
end.find_all{|d| d}.join "_"
end
mode = 'set'
apps = nil
o = OptionParser.new do |o|
o.on('-a', '--apps app1,app2', 'apps to size and reposition') do |apps|
apps = apps.split ','
end
o.on('-g', '--get', "'get' mode -- just write the apps' positions") do
mode = 'get'
end
o.on('-h', '--help', 'print this help message') do
puts USAGE + o.summarize.join
exit
end
o.parse! # oh, parse!
end
profile = ARGV[0] || "_default" + displays
rc = File.exists?(RUN_CONTROL)? YAML::load_file(RUN_CONTROL) : {}
rc[profile] = {} unless rc[profile]
apps = rc[profile].keys unless apps
apps.each do |app|
if mode == 'get'
rc[profile][app] = get_dims(app).split(',').map {|s| s.to_i}
else
set_dims app, rc[profile][app] if rc[profile][app]
end
end
File.open(RUN_CONTROL, 'w') { |f| f.write(rc.to_yaml) }
Jump to Line
Something went wrong with that request. Please try again.