Class: Terminitor::MacCore

Inherits:
AbstractCore show all
Includes:
Appscript
Defined in:
lib/terminitor/cores/mac_core.rb

Overview

Mac OS X Core for Terminitor This Core manages all the interaction with Appscript and the Terminal

Constant Summary collapse

ALLOWED_OPTIONS =
{
  :window => [:bounds, :visible, :miniaturized],
  :tab => [:settings, :selected]
}

Instance Attribute Summary

Attributes inherited from AbstractCore

#termfile, #terminal, #windows, #working_dir

Instance Method Summary collapse

Methods inherited from AbstractCore

#load_termfile, #process!, #run_in_window, #setup!

Constructor Details

#initialize(path) ⇒ MacCore

Initialize @terminal with Terminal.app, Load the Windows, store the Termfile Terminitor::MacCore.new(‘/path’)



14
15
16
17
18
19
# File 'lib/terminitor/cores/mac_core.rb', line 14

def initialize(path)
  super
  @terminal = app('Terminal')
  @windows  = @terminal.windows
  @delayed_options = []
end

Instance Method Details

#active_windowObject

returns the active window by checking if its the :frontmost



61
62
63
64
65
66
# File 'lib/terminitor/cores/mac_core.rb', line 61

def active_window
  windows = @terminal.windows.get
  windows.detect do |window|
    window.properties_.get[:frontmost] rescue false
  end
end

#execute_command(cmd, options = {}) ⇒ Object

executes the given command via appscript execute_command ‘cd /path/to’, :in => #<tab>



23
24
25
# File 'lib/terminitor/cores/mac_core.rb', line 23

def execute_command(cmd, options = {})
  active_window.do_script(cmd, options)
end

#open_tab(options = nil) ⇒ Object

Opens a new tab and returns itself.



28
29
30
31
32
# File 'lib/terminitor/cores/mac_core.rb', line 28

def open_tab(options = nil)
  terminal_process.keystroke("t", :using => :command_down)
  set_options(return_last_tab, options) if options
  return_last_tab
end

#open_window(options = nil) ⇒ Object

Opens A New Window, applies settings to the first tab and returns the tab object.



35
36
37
38
39
40
41
42
43
44
# File 'lib/terminitor/cores/mac_core.rb', line 35

def open_window(options = nil)
  terminal_process.keystroke("n", :using => :command_down)
  # Options of the first tab must be set before window options, 
  # because change of the first tab options causes change of window size
  if options    
    set_options(return_last_tab, allowed_options(:tab, options))
    set_options(active_window, allowed_options(:window, options))
  end
  return_last_tab
end

#return_last_tabObject

Returns the last instantiated tab from active window



54
55
56
57
58
# File 'lib/terminitor/cores/mac_core.rb', line 54

def return_last_tab
  local_window = active_window
  local_tabs = local_window.tabs if local_window
  local_tabs.last.get if local_tabs
end

#set_delayed_optionsObject

Apply delayed options and remove them from the queue



100
101
102
103
104
105
# File 'lib/terminitor/cores/mac_core.rb', line 100

def set_delayed_options
  @delayed_options.length.times do 
    option = @delayed_options.shift
    option[:object].instance_eval(option[:option]).set(option[:value])
  end
end

#set_options(object, options = {}) ⇒ Object

Sets options of the given object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/terminitor/cores/mac_core.rb', line 69

def set_options(object, options = {})
  options.each_pair do |option, value| 
    case option
    when :settings   # works for windows and tabs, for example :settings => "Grass"
      begin
        object.current_settings.set(@terminal.settings_sets[value])
      rescue Appscript::CommandError => e
        puts "Error: invalid settings set '#{value}'"
      end
    when :bounds # works only for windows, for example :bounds => [10,20,300,200]
      # the only working sequence to restore window size and position! 
      object.bounds.set(value)
      object.frame.set(value)
      object.position.set(value)
    when :selected # works for tabs, for example tab :active => true
      delayed_option(option, value, object)
    when :miniaturized # works for windows only
      delayed_option(option, value, object)
    when :name
      # ignore it.
    else # trying to apply any other option
      begin
        object.instance_eval(option.to_s).set(value)
      rescue
        puts "Error setting #{option} = #{value} on #{object.inspect}"
      end
    end
  end
end

#terminal_processObject

Returns the Terminal Process We need this method to workaround appscript so that we can instantiate new tabs and windows. otherwise it would have looked something like window.make(:new => :tab) but that doesn’t work.



49
50
51
# File 'lib/terminitor/cores/mac_core.rb', line 49

def terminal_process
  app("System Events").application_processes["Terminal.app"]
end