├── .gitignore ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib ├── pbcopy.rb └── pbcopy │ └── version.rb ├── pbcopy.gemspec └── spec └── pbcopy_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in pbcopy.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Josh Cheek 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pbcopy 2 | 3 | Enables piping and redirecting into pbcopy from within Ruby. 4 | 5 | Mac OS X only. 6 | 7 | This lib is intended as a development aid, not a production utility. 8 | 9 | Note that this adds `String#|` 10 | 11 | 12 | ## Pipe strings into pbcopy 13 | 14 | On the command line you might do 15 | 16 | $ which ruby | pbcopy 17 | 18 | Which would copy the location of the ruby binary into your system's paste board. Later you could retrieve it with Command-V. 19 | 20 | In Ruby you can pipe any string into pbcopy to achieve the same result 21 | 22 | RUBY_VERSION | pbcopy 23 | User.last.login | pbcopy 24 | 25 | 26 | ## Redirect arbitrary objects into pbcopy 27 | 28 | On the command line you might do 29 | 30 | $ pbcopy < some_file 31 | 32 | In Ruby you can do 33 | 34 | pbcopy < 123 35 | pbcopy < [4, 5, 6] 36 | 37 | You can redirect any arbitrary object into pbcopy, it will be turned into a string (via `#to_s`) and placed in the system clipboard 38 | 39 | 40 | ## Shorter more Ruby-like version of redirect 41 | 42 | You can omit the chevron when redirecting into pbcopy 43 | 44 | pbcopy 123 45 | pbcopy [4, 5, 6] 46 | 47 | 48 | ## Make available in irb / pry 49 | 50 | $ echo 'require "pbcopy"' >> ~/.irbrc 51 | $ echo 'require "pbcopy"' >> ~/.pryrc 52 | 53 | Of course, if you're using Bundler, this won't work (b/c you're on a team and your 54 | team shouldn't have to know about and install your personal dev tools). So you can do 55 | this absurd monstrosity (you can figure out which paths to add via `$ gem which pbcopy pasteboard`) 56 | 57 | 58 | begin 59 | require "pbcopy" 60 | rescue LoadError 61 | $LOAD_PATH << '~/.rvm/gems/ruby-1.9.3-p125@global/gems/pbcopy-1.0.1/lib/' 62 | $LOAD_PATH << '~/.rvm/gems/ruby-1.9.3-p125/gems/pasteboard-1.0/lib/' 63 | require 'pbcopy' 64 | end 65 | 66 | 67 | 68 | ## Installation 69 | 70 | $ gem install pbcopy 71 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require "bundler/gem_tasks" 3 | -------------------------------------------------------------------------------- /lib/pbcopy.rb: -------------------------------------------------------------------------------- 1 | require "pbcopy/version" 2 | require 'pasteboard' 3 | 4 | 5 | module Pbcopy 6 | Pasteboard = ::Pasteboard.new 7 | 8 | def Pasteboard.<(other) 9 | other.to_s | self 10 | end 11 | end 12 | 13 | 14 | class String 15 | def |(pbcopy) 16 | pbcopy.put [[Pasteboard::Type::UTF_8, self]] 17 | self 18 | end 19 | end 20 | 21 | 22 | def pbcopy(obj = (no_obj=true)) 23 | return Pbcopy::Pasteboard if no_obj 24 | pbcopy < obj 25 | end 26 | -------------------------------------------------------------------------------- /lib/pbcopy/version.rb: -------------------------------------------------------------------------------- 1 | module Pbcopy 2 | VERSION = "1.0.1" 3 | end 4 | -------------------------------------------------------------------------------- /pbcopy.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/pbcopy/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.authors = ["Josh Cheek"] 6 | gem.email = ["josh.cheek@gmail.com"] 7 | gem.description = %q{Replicates OSX commandline utility where piping into pbcopy places things into the clipboard} 8 | gem.summary = %q{Use pbcopy in Ruby in the same way you would from the terminal} 9 | gem.homepage = "https://github.com/JoshCheek/pbcopy" 10 | 11 | gem.files = `git ls-files`.split($\) 12 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 13 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 14 | gem.name = "pbcopy" 15 | gem.require_paths = ["lib"] 16 | gem.add_dependency 'pasteboard', '~> 1.0' 17 | gem.version = Pbcopy::VERSION 18 | end 19 | -------------------------------------------------------------------------------- /spec/pbcopy_spec.rb: -------------------------------------------------------------------------------- 1 | require 'pbcopy' 2 | 3 | describe 'pbcopy' do 4 | 5 | # stolen from pasteboard's tests 6 | def assert_put *expecteds 7 | pb = Pasteboard.new 8 | expecteds.length.should == pb.get_item_count 9 | 10 | expecteds.each do |expected| 11 | expected.each_with_index do |(flavor, data), index| 12 | flavor.should == pb.copy_item_flavors(0)[index] 13 | data.should == pb.first(flavor) 14 | end 15 | end 16 | end 17 | 18 | it 'is available everywhere, but not visible in method lists' do 19 | expect { Object.new.pbcopy }.to raise_error NoMethodError 20 | expect { Object.new.instance_eval { pbcopy } }.to_not raise_error 21 | end 22 | 23 | 24 | context 'when piping a string into pbcopy' do 25 | it 'ends up in system clipboard' do 26 | "hello world" | pbcopy 27 | assert_put [[Pasteboard::Type::UTF_8, "hello world"]] 28 | end 29 | 30 | it 'is returned' do 31 | ("hello world" | pbcopy).should == "hello world" 32 | end 33 | end 34 | 35 | 36 | context 'when redirecting an object into pbcopy' do 37 | it 'is the same as `obj.to_s | pbcopy`' do 38 | (pbcopy < "hello world").should == "hello world" 39 | assert_put [[Pasteboard::Type::UTF_8, "hello world"]] 40 | 41 | (pbcopy < 123).should == "123" 42 | assert_put [[Pasteboard::Type::UTF_8, "123"]] 43 | end 44 | end 45 | 46 | 47 | context 'when treating it like a method' do 48 | specify 'pass any object to pbcopy as an alternative to pbcopy < obj' do 49 | (pbcopy "hello world").should == "hello world" 50 | assert_put [[Pasteboard::Type::UTF_8, "hello world"]] 51 | 52 | (pbcopy 123).should == "123" 53 | assert_put [[Pasteboard::Type::UTF_8, "123"]] 54 | 55 | (pbcopy false).should == "false" 56 | assert_put [[Pasteboard::Type::UTF_8, "false"]] 57 | 58 | (pbcopy nil).should == "" 59 | assert_put [[Pasteboard::Type::UTF_8, ""]] 60 | end 61 | end 62 | end 63 | --------------------------------------------------------------------------------