Module: Osascript
- Defined in:
- lib/osascript/Key.rb,
lib/osascript.rb,
lib/osascript/Safari.rb,
lib/osascript/Preview.rb,
lib/osascript/any_app.rb,
lib/osascript/version.rb,
lib/osascript/Osascript.rb,
lib/osascript/constants.rb
Overview
Work with any application.
Defined Under Namespace
Classes: Error, Key, Preview, Safari
Constant Summary collapse
- VERSION =
"0.4.5"
- LIB_FOLDER =
File.dirname(__dir__)
- GEM_FOLDER =
File.dirname(LIB_FOLDER)
- TEST_FOLDER =
File.join(GEM_FOLDER,'test')
Class Method Summary collapse
-
.__asrun(code, application = nil) ⇒ Object
main =.
-
.get_window_properties(app_name, options = nil) ⇒ Object
Return the front window (or other window defined in options) properties.
-
.on?(app_name) ⇒ Boolean
True if application
app_name
is running. -
.quit(app_name) ⇒ Object
Quit application
app_name
if it’s running. -
.set_window(app_name, properties, options = nil) ⇒ Object
Set any properties of front window of
app_name
app (or other window defined in options) toproperties
. -
.set_window_bounds(app_name, bounds, options = nil) ⇒ Object
Set bounds of front window of
app_name
app (or other window defined in options) tobounds
. -
.set_window_dimension(app_name, dim, options = nil) ⇒ Object
Set dimension of front window of app
app_name
(or other window defined with options) todim
. -
.set_window_position(app_name, pos, options = nil) ⇒ Object
Set position of front window of
app_name
application (or other window defined with options) topos
.
Class Method Details
.__asrun(code, application = nil) ⇒ Object
main =
Méthode principale qui exécute le code code
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/osascript/Osascript.rb', line 8 def self.__asrun(code, application = nil) if application code = <<~TEXT tell application "#{application}" #{code} end tell TEXT end resultat = `osascript <<'TEXT' #{code.strip} TEXT ` return resultat.strip end |
.get_window_properties(app_name, options = nil) ⇒ Object
Return the front window (or other window defined in options) properties.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/osascript/any_app.rb', line 27 def self.get_window_properties(app_name, = nil) if on?(app_name) window = ( && [:window]) || 'front window' code = "return (properties of #{window})" ret = __asrun(code, app_name) # On se retrouve avec un texte du type : # "document:Mon doc, floating:false, bounds:4, 12, 23, 45 etc." # ret = ret.split(/(?:, )?([a-z]+)\:/) ret.shift if ret[0].empty? || ret[0].nil? # puts "ret = #{ret.inspect}" table = {} while ret.count > 0 prop = ret.shift.to_sym value = ret.shift # puts "prop = #{prop.inspect} / value: #{value}::#{value.class}" value = case prop when :bounds, :position eval("[#{value}]") else begin eval(value) rescue NameError value rescue Exception => e if value.match?(',') value.split(',').map {|n|n.strip} else raise e end end end table.merge!(prop => value) end return table end end |
.on?(app_name) ⇒ Boolean
Returns true if application app_name
is running.
25 26 27 28 29 |
# File 'lib/osascript/Osascript.rb', line 25 def self.on?(app_name) retour = __asrun("tell application \"System Events\" to return (name of processes) contains \"#{app_name}\"") # puts "retour = #{retour.inspect}" return retour == "true" end |
.quit(app_name) ⇒ Object
Quit application app_name
if it’s running
32 33 34 35 36 |
# File 'lib/osascript/Osascript.rb', line 32 def self.quit(app_name) if on?(app_name) __asrun("tell application \"#{app_name}\" to quit") end end |
.set_window(app_name, properties, options = nil) ⇒ Object
Set any properties of front window of app_name
app (or other window defined in options) to properties
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/osascript/any_app.rb', line 77 def self.set_window(app_name, properties, = nil) if on?(app_name) window = ( && [:window]) || 'front window' table = properties.map do |k, v| "{\"#{k}\", #{v.inspect}}" end.join(', ') code = <<~CODE set table to {#{table}} tell #{window} repeat with dprop in table set propName to item 1 of dprop set propValue to item 2 of dprop if propName is "bounds" then set bounds of it to propValue else if propName is "name" then set name of it to propValue else if propName is "index" then set index of it to propValue else if propName is "zoomed" then set zoomed of it to propValue else if propName is "miniaturized" then set miniaturized of it to provalue else if propName is "visible" then set visible of it to propValue end if end repeat end tell CODE __asrun(code, app_name) end end |
.set_window_bounds(app_name, bounds, options = nil) ⇒ Object
Set bounds of front window of app_name
app (or other window defined in options) to bounds
114 115 116 117 118 119 120 121 122 |
# File 'lib/osascript/any_app.rb', line 114 def self.set_window_bounds(app_name, bounds, = nil) if on?(app_name) window = ( && [:window]) || 'front window' code = <<~CODE set bounds of #{window} to {#{bounds.join(', ')}} CODE __asrun(code, app_name) end end |
.set_window_dimension(app_name, dim, options = nil) ⇒ Object
Set dimension of front window of app app_name
(or other window defined with options) to dim
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/osascript/any_app.rb', line 129 def self.set_window_dimension(app_name, dim, = nil) if on?(app_name) window = ( && [:window]) || 'front window' code = <<~CODE tell #{window} set theBounds to bounds of it set topRight to (item 1 of theBounds) + #{dim[:width]} set bottomRight to (item 2 of theBounds) + #{dim[:height]} set bounds of it to {item 1 of theBounds, item 2 of theBounds, topRight, bottomRight} end CODE __asrun(code, app_name) end end |
.set_window_position(app_name, pos, options = nil) ⇒ Object
Set position of front window of app_name
application (or other window defined with options) to pos
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/osascript/any_app.rb', line 149 def self.set_window_position(app_name, pos, = nil) if on?(app_name) window = ( && [:window]) || 'front window' code = <<~CODE tell #{window} set theBounds to bounds of it set width to (item 3 of theBounds) - (item 1 of theBounds) set topRight to #{pos[:left]} + width set height to (item 4 of theBounds) - (item 2 of theBounds) set bottomRight to #{pos[:top]} + height set bounds of it to {#{pos[:left]}, #{pos[:top]}, topRight, bottomRight} end CODE __asrun(code, app_name) end end |