Sha256: 3a957bf6a3ed9df7f2ff940876f4f558730465fe560fc8eb4d4ce09d15109fdb

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 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.
#---

require 'applescript'
require 'note'

class TextNote < Note
  include AppleScript

  @@app = TextNote
  
  def initialize(name = 'Untitled', with_options = {})
    tell.application('TextEdit').activate!
  end

  DontSave = 2

  def exit!
    menu 'TextEdit', 'Quit TextEdit'
    
    tell.
      application('System Events').
      process('TextEdit').
      window('Untitled').
      sheet(1).
      click_button!(DontSave) #(1)
  end

  def running?
    tell.
      application('System Events').
      process!('TextEdit') == 'TextEdit' #(2)
  end
end




class TextNote  
  def text
    tell.
      application('System Events').
      process('TextEdit').
      window('Untitled').
      scroll_area(1).
      text_area(1).
      get_value!
  end
  
  def text=(new_text)
    select_all
    
    tell.application('System Events').
      process('TextEdit').
      window('Untitled') do
        new_text.split(//).each {|k| keystroke! k}
      end
  end
end




class TextNote
  def menu(name, item, wait = false)
    tell.application('System Events').
      process('TextEdit').
      menu_bar(1).
      menu_bar_item(name).
      menu(name).
      click_menu_item! item
  end
  
  def undo; menu('Edit', 1) end #(3)

  def select_all; menu('Edit', 'Select All') end
  def cut; menu('Edit', 'Cut') end
  def copy; menu('Edit', 'Copy') end
  def paste; menu('Edit', 'Paste') end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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