Sha256: a96dc8000d5f2159f70ca02e5a22abc3bb06af9be38335f0421ade572244d3ec

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

#---
# Excerpted from "Scripted GUI Testing With Ruby",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/idgtr for more book information.
#---

module AppleScript
  class Command
    def initialize #(1)
      @lines = []
      @tells = 0
    end

    def method_missing(name, *args, &block)
      immediate = name.to_s.include? '!' #(2)
      param = args.shift
      script = name.to_s.chomp('!').gsub('_', ' ')
      script += %Q( #{param.inspect}) if param
      
      unless immediate #(3)
        script = 'tell ' + script
        @tells += 1
      end
      
      @lines << script
      
      if block_given? #(4)
        @has_block = true
        instance_eval &block
        go!
      elsif immediate && !@has_block
        go!
      else
        self
      end
    end
  end
end




module AppleScript
  class Command
    def go!
      clauses = @lines.map do |line|
        '-e "' + line.gsub('"', '\"') + '"'
      end.join(' ') + ' '
      
      clauses += '-e "end tell" ' * @tells
      
      `osascript #{clauses}`.chomp("\n")
    end
  end
end




module AppleScript
  def tell
    Command.new
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
win_gui-0.1.6 book_code/one_more_thing/applescript.rb
win_gui-0.1.4 book_code/one_more_thing/applescript.rb
win_gui-0.1.3 book_code/one_more_thing/applescript.rb
win_gui-0.1.2 book_code/one_more_thing/applescript.rb
win_gui-0.1.1 book_code/one_more_thing/applescript.rb
win_gui-0.1.0 book_code/one_more_thing/applescript.rb