2016-07-01   mac   book   pdf 

タイトルでPDFを検索して開くRubyスクリプト

背景

問題

解法

詳細

#!/usr/bin/env ruby

DIRECTORY = '/YOUR/PDF/DIR'
keyword = nil
number = nil

if ARGV.length != 1 and ARGV.length != 2
  puts "Usage (1): book keyword"
  puts "Example (1): book Galois"
  puts "Usage (2): book keyword number"
  puts "Example (2): book Galois 3"
  abort
end

keyword = ARGV[0]
number = ARGV[1].to_i if ARGV.length == 2

list = `ls #{DIRECTORY} | nkf --ic=UTF-8-MAC --oc=UTF-8 | grep '#{keyword}' | sort`.split(/\n/)
if number and number < list.size
  system("open '#{DIRECTORY}/#{list[number]}'")
else
  list.each_with_index do |path, index|
    puts "#{index}: #{path.sub(/#{DIRECTORY}\//, '')}"
  end
end

使用例

$ ruby book.rb Galois
0: Galois AAAAAAA
1: BBBB Galois BBBB
2: CCCC Galois CCCC
$ ruby book.rb Galois 2
(CCCC Galois CCCCを表示する)

参考

 2016-07-01   mac   book   pdf