Resize an OSX display with RubyCocoa.
This file contains 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
require 'osx/cocoa' | |
class Screen | |
class << self | |
def resize(width, height, &block) | |
new.resize(width, height, &block) | |
end | |
end | |
def initialize(screen = OSX::CGMainDisplayID()) | |
@screen = screen | |
@mode = OSX::CGDisplayCopyDisplayMode(@screen) | |
end | |
def resize(width, height, &block) | |
configure(best_mode(width, height)) | |
block.call | |
ensure | |
configure(@mode) | |
end | |
private | |
def best_mode(width, height) | |
# TODO Try to figure out color depth from CGDisplayModeCopyPixelEncoding? | |
OSX::CGDisplayCopyAllDisplayModes(@screen, nil).detect do |mode| | |
OSX::CGDisplayModeGetWidth(mode) == width && | |
OSX::CGDisplayModeGetHeight(mode) == height && | |
OSX::CGDisplayModeIsUsableForDesktopGUI(mode) | |
end | |
end | |
def configure(mode) | |
result, config = OSX::CGBeginDisplayConfiguration() | |
if result == OSX::KCGErrorSuccess | |
OSX::CGConfigureDisplayWithDisplayMode(config, @screen, mode, nil) | |
OSX::CGCompleteDisplayConfiguration(config, OSX::KCGConfigureForSession) | |
else | |
raise "I have no clue what just happened: #{result}" | |
end | |
end | |
end |
Hi! I'm pretty new to this stuff, too. (See line 39.)
You see how OSX::CGMainDisplayID()
is the default screen
parameter in line 10. I think you'd need to use one of the other Quartz Display Services to look up a different display id to pass in, perhaps CGGetOnlineDisplayList
?
Cool, thanks... I'll see what I can figure out using CGGetOnlineDisplayList
Hi
How's the usage? How can I use this in the Terminal?
Thanks a lot
Fabio
Hi, Fabio, sorry for my late reply. I've been traveling for some time now.
You'll be best off getting more comfortable with Ruby in particular and shell-scripting in general. I hope you'll kindly understand that I'm not able to help you with that here.
I would love to see usage in ruby, i'm unsure of the syntax expected.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to modify this to set the resolution of a secondary display? I'm pretty new to this, and unsure how I'd modify it...