Class: Pixab::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/Utilities.rb

Class Method Summary collapse

Class Method Details

.check_shell_result(error_msg = nil, success = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/Utilities.rb', line 19

def check_shell_result(error_msg = nil, success = nil)
  is_success = is_shell_execute_success(success)
  if is_success 
    return
  end
  if !error_msg.nil?
    puts error_msg.red
  end
  exit(1)
end

.dir_exist(path) ⇒ Object

判断目录是否存在,已适配2.4以后的版本



86
87
88
89
90
91
92
# File 'lib/Utilities.rb', line 86

def dir_exist(path)
  if Dir.respond_to?(:exist?)
    return Dir.exist?(path)
  else
    return Dir.exists?(path)
  end
end

.display_choose_list(items, default_items = nil, title = nil, prompt = nil, ok_button_name = nil, cancel_button_name = nil, is_multiple_selections = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/Utilities.rb', line 52

def display_choose_list(items, default_items = nil, title = nil, prompt = nil, ok_button_name = nil, cancel_button_name = nil, is_multiple_selections = false)
 shell = "osascript -e 'choose from list #{items}"
 if !title.nil?
  shell += " with title \"#{title}\""
 end
 if !prompt.nil?
  shell += " with prompt \"#{prompt}\""
 end
 if !ok_button_name.nil?
  shell += " OK button name \"#{ok_button_name}\""
 end
 if !cancel_button_name.nil?
  shell += " cancel button name \"#{cancel_button_name}\""
 end
 if !default_items.nil?
  shell += " default items #{default_items}"
 end
 if is_multiple_selections
  shell += " with multiple selections allowed"
 end
 shell += "'"
 selected_item_string = `#{shell}`.chomp
 if selected_item_string == "false"
  return []
 else 
  return selected_item_string.split(",")
 end
end

.display_default_dialog(default_text) ⇒ Object



38
39
40
41
42
43
# File 'lib/Utilities.rb', line 38

def display_default_dialog(default_text)
  input_msg = `osascript -e 'display dialog "#{default_text}"'`.chomp
  reg = /button returned:(.+)/
  input_msg.match(reg)
  !$1.nil?
end

.display_dialog(default_text, default_answer = "") ⇒ Object



45
46
47
48
49
50
# File 'lib/Utilities.rb', line 45

def display_dialog(default_text, default_answer = "")
  input_msg = `osascript -e 'display dialog "#{default_text}" default answer "#{default_answer}"'`.chomp
  reg = /text returned:(.+)/
  input_msg.match(reg)
  $1.nil? ? "" : $1
end

.execute_shell(commad) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/Utilities.rb', line 30

def execute_shell(commad)
  if commad.nil?
    return false
  end
  `#{commad}`
  return is_shell_execute_success
end

.is_shell_execute_success(success = nil) ⇒ Object



14
15
16
17
# File 'lib/Utilities.rb', line 14

def is_shell_execute_success(success = nil)
  is_success = success.nil? ? $?.to_i == 0 : success 
  return is_success
end