-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.rb
More file actions
112 lines (92 loc) · 3.56 KB
/
common.rb
File metadata and controls
112 lines (92 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
64
65
66
67
68
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'osx/cocoa'
include OSX
# The Common Module
# * This module hosts of a variety of common non-specific operations
module Common
include OSX
OSX.require_framework('SystemConfiguration')
# Applescript osascript wrapper
# * A primitive method that enables execution of a simple AppleScript expression
def osascript(*script)
begin
system('/usr/bin/osascript', '-e', script.join(" "))
rescue Exception => e
puts "Error: #{e}"
end
end
# Class methods for controlling Mac system volume
class VolumeControl
# Mutes the Mac OS system volume
def self.mute
begin
osascript 'set volume output muted true'
rescue Exception => e
puts "Error: #{e}"
end
end
# Un-mutes the Mac OS system volume
def self.unmute
begin
osascript 'set volume output muted false'
rescue Exception => e
puts "Error: #{e}"
end
end
# Set the Mac OS system volume to level
# * Accepts an integer in range 0-7
# * Level values less than 0 are interpreted as min. value
# * Level values greater than 7 are interpreted as max. value
def self.level(level)
begin
osascript('set volume', level)
rescue Exception => e
puts "Error: #{e}"
end
end
end
# Adds some nice Ruby-like method aliases to the existing class
# * load: read in a plist file at path as string
# * write: write out plist to file at path as string
class OSX::NSMutableDictionary
objc_alias_class_method('load:', 'dictionaryWithContentsOfFile:')
objc_alias_method('write:', 'writeToFile:atomically:')
end
#
class Plist < OSX::NSMutableDictionary
# This class is defined for convenience. It is essentially an ObjC "class" alias.
# Note: there are many other code bases which define a Plist class
# and this can cause conflicts.
end
class UUID < String
# 897A6343-628F-4964-80F1-C86D0FFA3F91
UUID = '([A-Z0-9]{8})-([A-Z0-9]{4}-){3}([A-Z0-9]{12})'
def initialize()
uuid = CFUUIDCreateString(nil, CFUUIDCreate(nil))
return super(uuid)
end
def self.match(string, options = nil)
string =~ Regexp.new(UUID, options) ? $& : false
end
def self.valid?(uuid, options = nil)
strictlyuuid = '^' + UUID + '$'
uuid =~ Regexp.new(strictlyuuid, options) ? true : false
end
end
# Class describing methods that pertain to common Mac OS X console qureries and operations
class Console
# Returns a Ruby Struct describing users that are logged via the Mac OS X Console
# * This list includes any users with Fast User Switching sessions
# * Struct: user names (array), UID/GID/name of the current console user (array), num of users logged on (int), all info parsed from the Dynamic Store (hash)
def self.get_users()
consoleUsers = Struct.new(:names, :current_user, :total_users, :info)
sc_dynstore_session_name = Proc.new { self.class.to_s + Common::UUID.new() } # Defined as a Proc to guarantee unique id
sc_dynstore_session = SCDynamicStoreCreate(nil, sc_dynstore_session_name.call, nil, nil)
key = SCDynamicStoreKeyCreateConsoleUser(nil)
dict = SCDynamicStoreCopyValue(sc_dynstore_session, key)
current_user = dict.to_ruby.reject { |k,v| k =~ /SessionInfo/ }
total_users = dict['SessionInfo'].to_ruby.size
names = dict['SessionInfo'].to_ruby.collect { |x| x['kCGSSessionUserNameKey'] }
consoleUsers.new(names, current_user, total_users, dict.to_ruby)
end
end
end