hotkeys 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -2
- data/lib/hotkeys.rb +41 -6
- data/lib/hotkeys/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
HotKeys
|
2
2
|
=====
|
3
3
|
|
4
|
-
A simple gem which allows you to bind global hot keys with macruby
|
4
|
+
A simple gem which allows you to bind global hot keys with macruby, optionally can also specify
|
5
|
+
which application needs to be frontmost (inspired by Keyboard Maestro)
|
5
6
|
|
6
7
|
1. `gem install hotkeys`
|
7
8
|
2. `macruby examples/simple.rb`
|
@@ -16,7 +17,9 @@ Usage example:
|
|
16
17
|
def applicationDidFinishLaunching(notification)
|
17
18
|
@hotkeys = HotKeys.new
|
18
19
|
|
19
|
-
|
20
|
+
# Will only trigger if Safari is frontmost application, second option can be left blank
|
21
|
+
# for truly global shortcut
|
22
|
+
@hotkeys.addHotString("Space+OPTION","com.apple.safari") do
|
20
23
|
puts "LOL MACRUBY RUNS"
|
21
24
|
end
|
22
25
|
|
@@ -40,4 +43,9 @@ Todo
|
|
40
43
|
=====
|
41
44
|
* Clean up shortcut.m
|
42
45
|
|
46
|
+
|
47
|
+
Known Bugs
|
48
|
+
=====
|
49
|
+
* Cannot unbind keys, (anyone awesome at objective+c?)
|
50
|
+
|
43
51
|
# Copyright (C) 2011 by Robert Lowe <rob[!]iblargz.com> - MIT
|
data/lib/hotkeys.rb
CHANGED
@@ -26,6 +26,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
26
26
|
framework 'AppKit'
|
27
27
|
framework 'Carbon'
|
28
28
|
framework 'Cocoa'
|
29
|
+
framework 'ScriptingBridge'
|
29
30
|
|
30
31
|
# required here, beause we subclass this sucker right away.
|
31
32
|
require File.dirname(__FILE__) + '/hotkeys/support/bundles/shortcut'
|
@@ -39,15 +40,49 @@ class HotKeys < Shortcut
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def hotkeyWasPressed(key)
|
42
|
-
|
43
|
-
|
43
|
+
# Matching int
|
44
|
+
configuration = @configurations.detect {|configuration| configuration[:key] == key }
|
45
|
+
str = configuration[:str]
|
46
|
+
|
47
|
+
# Sibling bindings
|
48
|
+
configurations = @configurations.select {|config| config[:str] == str}
|
44
49
|
|
45
|
-
|
46
|
-
|
50
|
+
# Run em
|
51
|
+
configurations.each do |configuration|
|
52
|
+
block = configuration[:block]
|
53
|
+
if configuration[:bundle_identifier] == frontmost_bundler_identifier
|
54
|
+
block.call
|
55
|
+
elsif configuration[:bundle_identifier].nil?
|
56
|
+
block.call
|
57
|
+
end
|
58
|
+
end if configurations
|
59
|
+
end
|
47
60
|
|
48
|
-
|
61
|
+
def addHotString(str, options = {}, &block)
|
62
|
+
args = HotKeys::Support::Keys.parse(str)
|
63
|
+
@configurations ||= []
|
49
64
|
key = self.send(:"addShortcut:withKeyModifier", *args)
|
50
|
-
@
|
65
|
+
@configurations << {
|
66
|
+
:key => key.to_s,
|
67
|
+
:str => str,
|
68
|
+
:block => block,
|
69
|
+
:bundle_identifier => options.delete(:bundle_identifier)
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def systemevents
|
76
|
+
@systemevents ||= SBApplication.applicationWithBundleIdentifier("com.apple.systemevents")
|
77
|
+
end
|
78
|
+
|
79
|
+
def frontmost
|
80
|
+
@frontmost = systemevents.processes.select {|process| process.frontmost == true }
|
81
|
+
@frontmost.first
|
82
|
+
end
|
83
|
+
|
84
|
+
def frontmost_bundler_identifier
|
85
|
+
frontmost.bundleIdentifier if frontmost
|
51
86
|
end
|
52
87
|
|
53
88
|
end
|
data/lib/hotkeys/version.rb
CHANGED