Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Screen Shot - Ruby

Hiroto,


Awhile back you posted this script, but no longer works with Yosemite, can you post the needed edit?


#!/bin/bash


# screenshot area
x=0
y=0
w=$((1365-$x))
h=$((767-$y))


OSXver=$(sw_vers -productVersion)
if [[ $OSXver < "10.9" ]]; then 
    ruby="/usr/bin/ruby"
else
    ruby="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
fi


$ruby <<-'END' - "$x" "$y" "$w" "$h"
require 'osx/cocoa'
include OSX
raise ArgumentError, "Usage: #{File.basename($0)} x, y, w, h" unless ARGV.length == 4
x, y, w, h = ARGV.map {|a| a.to_f}
outfile = File.expand_path(%x[date +'%F %H.%M.%S.png'].chomp, '~/Desktop')
img = CGDisplayCreateImageForRect(CGMainDisplayID(), CGRectMake(x, y, w, h))
brep = NSBitmapImageRep.alloc.initWithCGImage(img)
data = brep.objc_send(
    :representationUsingType, NSPNGFileType,
    :properties, {})
data.objc_send(
    :writeToFile, outfile,
    :atomically, false)
END

OS X Yosemite (10.10)

Posted on May 5, 2015 4:22 PM

Reply
Question marked as Best reply

Posted on May 5, 2015 7:21 PM

Tony,


Apple dropped the Ruby/Cocoa Scripting Bridge in Mavericks and Yosemite. If you install the OpenSource Ruby Cocoa v1.2.0, this script will then resume working, unless there is a Ruby version issue.

5 replies

May 5, 2015 7:22 PM in response to Tony T1

Hi,



On Maverick, the current version of ruby is 2.0, when i run the script --> `require': cannot load such file -- osx/cocoa (LoadError)

I can run your script with ruby version 1.8


On Yosemite, open the '/System/Library/Frameworks/Ruby.framework/Versions/' folder.

if the "1.8" folder exists, you can use this script :


#!/bin/bash
# screenshot area
x=0
y=0
w=$((1365-$x))
h=$((767-$y))

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby <<-'END' - "$x" "$y" "$w" "$h"
require 'osx/cocoa'
include OSX
raise ArgumentError, "Usage: #{File.basename($0)} x, y, w, h" unless ARGV.length == 4
x, y, w, h = ARGV.map {|a| a.to_f}
outfile = File.expand_path(%x[date +'%F %H.%M.%S.png'].chomp, '~/Desktop')
img = CGDisplayCreateImageForRect(CGMainDisplayID(), CGRectMake(x, y, w, h))
brep = NSBitmapImageRep.alloc.initWithCGImage(img)
data = brep.objc_send(
    :representationUsingType, NSPNGFileType,
    :properties, {})
data.objc_send(
    :writeToFile, outfile,
    :atomically, false)
END

May 5, 2015 8:56 PM in response to Tony T1

Hello Tony T1,


Under OS X 10.10, you need to manually install RubyCocoa 1.2.0 which supports Ruby 2.0 or later.


http://rubycocoa.sourceforge.net/

http://sourceforge.net/projects/rubycocoa/files/RubyCocoa/1.2.0/


and change ruby interpreter in script to:


/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby



Regards,

Hiroto



PS. [[ "10.10" < "10.9" ]] is evaluated as true in shell. So the version test code, if necessary, needs to be changed... Perhaps using BuildVersion instead of ProductVersion?

May 6, 2015 2:05 AM in response to Hiroto

Hiroto,


Just for clarity, RubyCocoa 1.2.0 does not install its own Ruby interpreter. The following Ruby binaries have matching identical checksums, and versions (2.0.0p481, release 45883) on OS X 10.9, and 10.10.3 respectively. They are the same binaries. RubyCocoa v1.2.0 enables the default System Ruby to again use the Ruby/Cocoa frameworks with its re-added scripting bridge support.

  • /usr/bin/ruby
  • /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby


Mavericks disabled the Ruby/Cocoa bridge unless one used the following alternative. Yosemite removed this version entirely.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby


The collating sequence makes the sw_vers -buildVersion a good choice for string comparison. The addition of RubyCocoa 1.2.0 in Mavericks, or Yosemite allows one to simply use the default system Ruby 2.0, while Mountain Lion and earlier require the Ruby 1.8 release per the previous bullet.

Screen Shot - Ruby

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.