hotkeys 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +10 -2
  2. data/lib/hotkeys.rb +41 -6
  3. data/lib/hotkeys/version.rb +1 -1
  4. 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
- @hotkeys.addHotString("R+COMMAND") do
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
@@ -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
- @keys[key.to_i].call
43
- end
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
- def addHotString(keyString, &block)
46
- args = HotKeys::Support::Keys.parse(keyString)
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
- @keys ||= []
61
+ def addHotString(str, options = {}, &block)
62
+ args = HotKeys::Support::Keys.parse(str)
63
+ @configurations ||= []
49
64
  key = self.send(:"addShortcut:withKeyModifier", *args)
50
- @keys[key] = block
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
@@ -22,7 +22,7 @@ class Hotkeys
22
22
  module Version
23
23
  MAJOR = 0
24
24
  MINOR = 1
25
- PATCH = 1
25
+ PATCH = 2
26
26
  BUILD = nil
27
27
 
28
28
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: hotkeys
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rob Lowe