Module: TotalSpaces2

Defined in:
lib/totalspaces2.rb

Defined Under Namespace

Classes: Display, Displays, Window, Windows

Constant Summary collapse

MAX_DESKTOPS =
16

Class Method Summary collapse

Class Method Details

.add_desktops(number_to_add) ⇒ Object

Add desktops There can be at most 16 desktops unless the display has collected some when a secondary display has been unplugged. Returns true on success, false if number_to_add was zero, or would result in more than 16 desktops. The on_layout_change notification will be sent if a changed was made.

TotalSpaces2.add_desktops(1)


524
525
526
# File 'lib/totalspaces2.rb', line 524

def add_desktops(number_to_add)
  TSApi.tsapi_addDesktopsOnDisplay(number_to_add, 0)
end

.add_desktops_on_display(number_to_add, display_id) ⇒ Object

Add desktops There can be at most 16 desktops unless the display has collected some when a secondary display has been unplugged. Returns true on success, false if number_to_add was zero, or would result in more than 16 desktops. The on_layout_change notification will be sent if a changed was made.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.add_desktops_on_display(1, display_id)


538
539
540
# File 'lib/totalspaces2.rb', line 538

def add_desktops_on_display(number_to_add, display_id)
  TSApi.tsapi_addDesktopsOnDisplay(number_to_add, display_id)
end

.api_versionObject

Returns the version of the api present in TotalSpaces2, a string such as “1.0” You should be using the same dylib version as that returned by the this call

puts "TotalSpaces2 API version: #{TotalSpaces2.api_version}"

 if TotalSpaces2.lib_total_spaces_version.split('.')[0] != TotalSpaces2.api_version.split('.')[0]
  puts "Comms error!"
  exit(1)
end


199
200
201
# File 'lib/totalspaces2.rb', line 199

def api_version
  string_and_free(TSApi.tsapi_apiVersion)
end

.cancel_on_layout_changeObject

Cancel the layout change notification



430
431
432
433
# File 'lib/totalspaces2.rb', line 430

def cancel_on_layout_change
  $tsapi_on_layout_change_block = nil
  TSApi.tsapi_unsetLayoutChangedCallback
end

.cancel_on_space_changeObject

Cancel the on_space_change notification.



402
403
404
405
# File 'lib/totalspaces2.rb', line 402

def cancel_on_space_change
  $tsapi_on_space_change_block = nil
  TSApi.tsapi_unsetSpaceWillChangeCallback
end

.current_spaceObject

Returns the number of the current space on the main display. Numbering starts at 1.

puts "Current space number: #{TotalSpaces2.current_space}"


242
243
244
# File 'lib/totalspaces2.rb', line 242

def current_space
  TSApi.tsapi_currentSpaceNumberOnDisplay(0)
end

.current_space_on_display(display_id) ⇒ Object

Returns the number of the current space on the given display. Space numbering starts at 1

display_id = TotalSpaces2.displays[0]
puts "Current space number: #{TotalSpaces2.current_space_on_display(display_id)}"


252
253
254
# File 'lib/totalspaces2.rb', line 252

def current_space_on_display(display_id)
  TSApi.tsapi_currentSpaceNumberOnDisplay(display_id)
end

.display_listObject

Returns an array of hashes with information about attached displays. The ids returned from this call can be used where a display id is required in the other calls in this library.

puts "Attached displays: #{TotalSpaces2.display_list}"

[{:display_id=>69679040, :display_name=>"Color LCD", :width=>1440, :height=>900}, 
{:display_id=>69514913, :display_name=>"LED Cinema Display", :width=>2560, :height=>1440}]


219
220
221
222
223
224
225
# File 'lib/totalspaces2.rb', line 219

def display_list
  list = TSApi.tsapi_displayList
  displays = Displays.new(list)
  result = displays.null? ? [] : displays.display_info
  TSApi.tsapi_freeDisplayList(list)
  result
end

.grid_columnsObject

Returns the number of columns defined in TotalSpaces2 for the main display

puts "Number of columns: #{TotalSpaces2.grid_columns}"


300
301
302
# File 'lib/totalspaces2.rb', line 300

def grid_columns
  TSApi.tsapi_definedColumnsOnDisplay(0)
end

.grid_columns_on_display(display_id) ⇒ Object

Returns the number of columns defined in TotalSpaces2

display_id = TotalSpaces2.main_display[:display_id]
puts "Number of columns: #{TotalSpaces2.grid_columns_on_display(display_id)}"


309
310
311
# File 'lib/totalspaces2.rb', line 309

def grid_columns_on_display(display_id)
  TSApi.tsapi_definedColumnsOnDisplay(display_id)
end

.lib_total_spaces_versionObject

Returns the version of the dylib, a string such as “1.0” You should be using the same dylib major version number as that returned by the api_version call

puts "libTotalSpaces2 version: #{TotalSpaces2.lib_total_spaces_version}"

if TotalSpaces2.lib_total_spaces_version.split('.')[0] != TotalSpaces2.api_version.split('.')[0]
  puts "Comms error!"
  exit(1)
end


185
186
187
# File 'lib/totalspaces2.rb', line 185

def lib_total_spaces_version
  string_and_free(TSApi.tsapi_libTotalSpacesVersion)
end

.main_displayObject

Returns information about the main display. Methods that do not take a display id always operate on this display.

puts "Main display id: #{TotalSpaces2.main_display_id}"

{:display_id=>69679040, :display_name=>"Color LCD", :width=>1440, :height=>900}


234
235
236
# File 'lib/totalspaces2.rb', line 234

def main_display
  self.display_list[0]
end

.move_space_to_position(space_number, position_number) ⇒ Object

Move space to a new position in the grid on the main display.

Returns false if the space_number or position_number is not valid.

TotalSpaces2.move_space_to_position(4, 2)


499
500
501
# File 'lib/totalspaces2.rb', line 499

def move_space_to_position(space_number, position_number)
  TSApi.tsapi_moveSpaceToPositionOnDisplay(space_number, position_number)
end

.move_space_to_position_on_display(space_number, position_number, display_id) ⇒ Object

Move space to a new position in the grid. Spaces can only be moved within their own display.

Returns false if the space_number or position_number is not valid.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.move_space_to_position_on_display(4, 2, display_id)


511
512
513
# File 'lib/totalspaces2.rb', line 511

def move_space_to_position_on_display(space_number, position_number, display_id)
  TSApi.tsapi_moveSpaceToPositionOnDisplay(space_number, position_number, display_id)
end

.move_to_space(space_number) ⇒ Object

Command TotalSpaces2 to switch to the given space number on the main display. Returns false if the space number was invalid. The on_space_change notification will be sent.

TotalSpaces2.move_to_space(1)


344
345
346
# File 'lib/totalspaces2.rb', line 344

def move_to_space(space_number)
  TSApi.tsapi_moveToSpaceOnDisplay(space_number, 0)
end

.move_to_space_on_display(space_number, display_id) ⇒ Object

Command TotalSpaces2 to switch to the given space number. Returns false if the space number was invalid. The on_space_change notification will be sent.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.move_to_space_on_Display(1, display_id)


355
356
357
# File 'lib/totalspaces2.rb', line 355

def move_to_space_on_display(space_number, display_id)
  TSApi.tsapi_moveToSpaceOnDisplay(space_number, display_id)
end

.move_window_to_space(window_id, space_number) ⇒ Object

Move a window to a given space The window_id parameter must be fetched using window_list. Returns false if the space_number or window_id is invalid.



481
482
483
# File 'lib/totalspaces2.rb', line 481

def move_window_to_space(window_id, space_number)
  TSApi.tsapi_moveWindowToSpaceOnDisplay(window_id, space_number, 0)
end

.move_window_to_space_on_display(window_id, space_number, display_id) ⇒ Object

Move a window to a given space on the main display The window_id parameter must be fetched using window_list. Returns false if the space_number or window_id is invalid.



489
490
491
# File 'lib/totalspaces2.rb', line 489

def move_window_to_space_on_display(window_id, space_number, display_id)
  TSApi.tsapi_moveWindowToSpaceOnDisplay(window_id, space_number, display_id)
end

.name_for_space(space_number) ⇒ Object

Returns the name for a space on the main display. The returned string will be empty if the space number is not valid

current_space = TotalSpaces2.current_space
puts "Current space is called: #{TotalSpaces2.name_for_space(current_space)}"


262
263
264
265
# File 'lib/totalspaces2.rb', line 262

def name_for_space(space_number)
  name = string_and_free(TSApi.tsapi_spaceNameForSpaceNumberOnDisplay(space_number, 0))
  name.force_encoding("UTF-8")
end

.name_for_space_on_display(space_number, display_id) ⇒ Object

Returns the name for a space. The returned string will be empty if the space number is not valid

current_space = TotalSpaces2.current_space
display_id = TotalSpaces2.main_display[:display_id]
puts "Current space is called: #{TotalSpaces2.name_for_space_on_display(current_space, display_id)}"


274
275
276
277
# File 'lib/totalspaces2.rb', line 274

def name_for_space_on_display(space_number, display_id)
  name = string_and_free(TSApi.tsapi_spaceNameForSpaceNumberOnDisplay(space_number, display_id))
  name.force_encoding("UTF-8")
end

.number_of_spacesObject

Returns the total number of spaces including fullscreens, dashboard (if it’s a space).

puts "Total number of spaces: #{TotalSpaces2.number_of_spaces}"


283
284
285
# File 'lib/totalspaces2.rb', line 283

def number_of_spaces
  TSApi.tsapi_numberOfSpacesOnDisplay(0)
end

.number_of_spaces_on_display(display_id) ⇒ Object

Returns the total number of spaces including fullscreens, dashboard (if it’s a space).

display_id = TotalSpaces2.main_display[:display_id]
puts "Total number of spaces: #{TotalSpaces2.number_of_spaces_on_display(display_id)}"


292
293
294
# File 'lib/totalspaces2.rb', line 292

def number_of_spaces_on_display(display_id)
  TSApi.tsapi_numberOfSpacesOnDisplay(display_id)
end

.on_layout_change(&block) ⇒ Object

Register for notifications on layout change. The given block will be called whenever the layout changes - this could be due to making an app fullscreen, changing a space name, or changing the layout of the TotalSpaces2 grid. There are no arguments passed to the block.

TotalSpaces.on_layout_change {puts "Spaces changed"}

sleep

When you get a notification from this method, you should re-fetch any information about the spaces that you may be storing.

There can only be one block registered at any time, the most recently registered one will be called.



423
424
425
426
# File 'lib/totalspaces2.rb', line 423

def on_layout_change(&block)
  $tsapi_on_layout_change_block = block  # prevent GC
  TSApi.tsapi_setLayoutChangedCallback(block)
end

.on_space_change(&block) ⇒ Object

Register for notifications on space change. The given block will be called whenever you move from one space to another. The arguments are the space number you moved from, and the one you are moving to.

TotalSpaces2.on_space_change {|from, to, displayID| puts "Moving from space #{from} to space #{to}"}

sleep

There can only be one block registered at any time, the most recently registered one will be called. This callback is called just before the space actually changes - current_space will still report the from space.



395
396
397
398
# File 'lib/totalspaces2.rb', line 395

def on_space_change(&block)
  $tsapi_on_space_change_block = block  # prevent GC
  TSApi.tsapi_setSpaceWillChangeCallback(block)
end

.remove_desktops(number_to_remove) ⇒ Object

Remove desktops The highest numbered desktops are removed. Removing a desktop you are currently on will result in TotalSpaces2 switching to another dektop. Any windows present on a desktop being removed will be moved to one of the remaining desktops. Returns true on success, false if number_to_remove was zero or would result in less than 1 desktop remaining. The on_layout_change notification will be sent if a change was made.

TotalSpaces2.remove_desktops(1)


554
555
556
# File 'lib/totalspaces2.rb', line 554

def remove_desktops(number_to_remove)
  TSApi.tsapi_removeDesktopsOnDisplay(number_to_remove, 0)
end

.remove_desktops_on_display(number_to_remove, display_id) ⇒ Object

Remove desktops The highest numbered desktops are removed. Removing a desktop you are currently on will result in TotalSpaces2 switching to another dektop. Any windows present on a desktop being removed will be moved to one of the remaining desktops. Returns true on success, false if number_to_remove was zero or would result in less than 1 desktop remaining. The on_layout_change notification will be sent if a change was made.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.remove_desktops_on_display(1, display_id)


571
572
573
# File 'lib/totalspaces2.rb', line 571

def remove_desktops_on_display(number_to_remove, display_id)
  TSApi.tsapi_removeDesktopsOnDisplay(number_to_remove, display_id)
end

.set_grid_columns(columns) ⇒ Object

Sets the number of columns defined in TotalSpaces2 for the main display.

This does not change the actual number of desktops present, you should call add_desktops or remove_desktops as appropriate after changing the number of columns.

TotalSpaces2.set_grid_columns(3)


321
322
323
# File 'lib/totalspaces2.rb', line 321

def set_grid_columns(columns)
  TSApi.tsapi_setDefinedColumnsOnDisplay(columns, 0)
end

.set_grid_columns_on_display(columns, display_id) ⇒ Object

Sets the number of columns defined in TotalSpaces2.

This does not change the actual number of desktops present, you should call add_desktops_on_display or remove_desktops_on_display as appropriate after changing the number of columns.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.set_grid_columns_on_display(3, display_id)


334
335
336
# File 'lib/totalspaces2.rb', line 334

def set_grid_columns_on_display(columns, display_id)
  TSApi.tsapi_setDefinedColumnsOnDisplay(columns, display_id)
end

.set_name_for_space(space_number, name) ⇒ Object

Set the name for a space on the main display. Note that using this command will cause a layout change notification to be sent if the new name was different from that previously set. The maximum length for a name is 255 bytes.

TotalSpaces2.set_name_for_space(1, "Home")


366
367
368
# File 'lib/totalspaces2.rb', line 366

def set_name_for_space(space_number, name)
  TSApi.tsapi_setNameForSpaceOnDisplay(space_number, name, 0)
end

.set_name_for_space_on_display(space_number, name, display_id) ⇒ Object

Set the name for a space. Note that using this command will cause a layout change notification to be sent if the new name was different from that previously set. The maximum length for a name is 255 bytes.

display_id = TotalSpaces2.main_display[:display_id]
TotalSpaces2.set_name_for_space_on_display(1, "Home", display_id)


378
379
380
# File 'lib/totalspaces2.rb', line 378

def set_name_for_space_on_display(space_number, name, display_id)
  TSApi.tsapi_setNameForSpaceOnDisplay(space_number, name, display_id)
end

.total_spaces_versionObject

Returns the version of TotalSpaces2 running on the system, a string such as “2.0.12”

puts "TotalSpaces2 version: #{TotalSpaces2.total_spaces_version}"


207
208
209
# File 'lib/totalspaces2.rb', line 207

def total_spaces_version
  string_and_free(TSApi.tsapi_totalSpacesVersion)
end

.window_listObject

Get a list of all the windows on your mac It returns an array containing a hash for each window. The hash contains the display id (key :display_id) and space number (key :space_number) and details for each window. The windows are in front to back order within each space. Each window hash also contains a window_id, title, frame, app_name and is_on_all_spaces flag

The below example would move the frontmost window to the next space to the right.

windows = TotalSpaces2.window_list
current_space = TotalSpaces2.current_space
main_display_id = TotalSpaces2.main_display[:display_id]
if !windows.empty?
  current_space_windows = windows.select {|window| window[:display_id] == main_display_id 
                                                   && window[:space_number] == current_space}
  front_window = current_space_windows[0]
  TotalSpaces2.move_window_to_space(front_window[:window_id], TotalSpaces.current_space + 1)
end


454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/totalspaces2.rb', line 454

def window_list
  result = []
  list = TSApi.tsapi_windowList
  main_array = Windows.new(list)

  (0...main_array[:count]).each do |n|
    window = Window.new(main_array[:windows_array] + n * Window.size)
    window_hash = {}
    window_hash[:window_id] = window[:window_id]
    window_hash[:title] = window[:title].dup.force_encoding("UTF-8")
    window_hash[:frame] = window[:frame].dup.force_encoding("UTF-8")
    window_hash[:is_on_all_spaces] = window[:is_on_all_spaces]
    window_hash[:app_name] = window[:app_name].dup.force_encoding("UTF-8")
    window_hash[:display_id] = window[:display_id]
    window_hash[:space_number] = window[:space_number]
    result << window_hash
  end
  
  TSApi.tsapi_freeWindowList(list)

  result
end