findercolor 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ == 0.5.0 / 2007-07-30
2
+
3
+ * 1 major enhancement
4
+ * Birthday!
5
+
@@ -0,0 +1,9 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/color_client.rb
6
+ bin/color_server.rb
7
+ lib/finder_color.rb
8
+ lib/queue_server.rb
9
+ test/test_finder_color.rb
@@ -0,0 +1,58 @@
1
+ FinderColor
2
+ by Matthew King
3
+ http://findercolor.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ FinderColor is a an extremely simple interface to the Finder label
8
+ colors in Mac OS X. It uses Apple Events via the rb-appscript bridge
9
+ to allow the getting and setting of colors without locking up the
10
+ Finder using AppleScript. FinderColor can get and set labels by index
11
+ or by color names (as symbols, e.g. :none, :orange, :red).
12
+
13
+ == FEATURES/PROBLEMS:
14
+
15
+ * The Finder doesn't always respond to Apple Events that set label indexes.
16
+ For this reason, the private method that sets color has an until loop
17
+ that keeps trying until the label index matches the desired value.
18
+ Even this doesn't work sometimes, and I can't figure out why. Failure
19
+ rate is reasonably low, though.
20
+
21
+ == SYNOPSIS:
22
+
23
+ require 'finder_color'
24
+ FinderColor.get_index('/Users/matthew/Documents/fan_letters/Stephenson,Neal') #=> 0
25
+ FinderColor.set_index('/Users/matthew/Documents/fan_letters/Stephenson,Neal', :green)
26
+
27
+ == REQUIREMENTS:
28
+
29
+ * sudo gem install rb-appscript
30
+
31
+ == INSTALL:
32
+
33
+ * sudo gem install findercolor
34
+
35
+ == LICENSE:
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2007 FIX
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/finder_color.rb'
6
+
7
+ Hoe.new('FinderColor', FinderColor::VERSION) do |p|
8
+ p.name = 'findercolor'
9
+ p.extra_deps = [
10
+ ['rb-appscript', '>=0.3.0']]
11
+ p.rubyforge_name = 'findercolor'
12
+ p.author = 'Matthew King'
13
+ p.email = 'automatthew@gmail.com'
14
+ p.summary = 'set Finder labels (colors) in Mac OS X using AppleEvents instead of osascript'
15
+ p.description = p.paragraphs_of('README.txt', 2).join("\n\n")
16
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[2..-1]
17
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
18
+ end
19
+
20
+ # vim: syntax=Ruby
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thread'
4
+ require 'drb'
5
+
6
+ NAME = 'foo'
7
+
8
+ DRb.start_service('druby://localhost:0')
9
+ @queue = DRbObject.new_with_uri('druby://127.0.0.1:21001')
10
+
11
+
12
+ @queue.enq(hash)
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'finder_color')
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'queue_server')
5
+
6
+ include FinderColor
7
+
8
+ run_queue do |batch|
9
+ if batch.respond_to? '[]'
10
+ batch_set(batch)
11
+ end
12
+ end
@@ -0,0 +1,86 @@
1
+ module FinderColor
2
+ VERSION = '0.5.0'
3
+ end
4
+
5
+ require 'rubygems'
6
+ require 'appscript'
7
+ include Appscript
8
+
9
+ module FinderColor
10
+ extend self
11
+
12
+ Labels = [:none, :orange, :red, :yellow, :blue, :purple, :green, :gray ]
13
+ Finder = app.by_id( 'com.apple.Finder')
14
+
15
+ # Returns the Finder label index for the given file
16
+ def self.get_index(file_path)
17
+ mac_file_url(file_path).label_index.get
18
+ end
19
+
20
+ # Returns a symbol from FinderColor::Labels (e.g. :blue, :purple) to indicate the file color
21
+ def self.get_color(file_path)
22
+ Labels[mac_file_url(file_path).label_index.get]
23
+ end
24
+
25
+ # Set the Finder label to the given index, which must an integer from 0 to 7.
26
+ # See FinderColor::Labels for a mapping of indices to colors
27
+ def self.set_index(file_path, index)
28
+ set_label_color(mac_file_url(file_path), index)
29
+ end
30
+
31
+ # Given a symbol in [:none, :orange, :red, :yellow, :blue, :purple, :green, :gray ],
32
+ # sets the Finder label color accordingly.
33
+ def self.set_color(file_path, color)
34
+ set_label_color(mac_file_url(file_path), color)
35
+ end
36
+
37
+ # batch_set takes a hash where the keys are indices or color symbols (e.g. :green)
38
+ # and the values are arrays of file paths.
39
+ #
40
+ # Example:
41
+ # { :blue => [ '/Users/matthew/Documents/fan_letters/Wolfe,Gene',
42
+ # '/Users/matthew/Documents/fan_letters/Duncan,Dave'],
43
+ # :red => [ '/Users/matthew/Documents/restraining_orders/Wolfe,Gene'] }
44
+ def self.batch_set(batch)
45
+ batch.each do |k,files|
46
+ files.each do |file|
47
+ set_label_color mac_file_url(file), k
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def mac_file_url(posix_path)
55
+ raise "#{self.posix_path} does not exist" unless File.exist?(posix_path)
56
+ mac_file_url = MacTypes::FileURL.path(posix_path)
57
+ Finder.items[mac_file_url]
58
+ end
59
+
60
+ def label_index(file_url)
61
+ file_url.label_index.get
62
+ end
63
+
64
+ def label_color(file_url)
65
+ Labels[file_url.label_index.get]
66
+ end
67
+
68
+ def set_label_color(file_url, color=:none)
69
+ if color.is_a?(Fixnum) && Labels[color]
70
+ color_index = color
71
+ elsif Labels.include?(color)
72
+ color_index = Labels.index(color)
73
+ else
74
+ raise ArgumentError, "#{color} is not an acceptable argument"
75
+ end
76
+ label_index(file_url)
77
+ until color_index == (actual = label_index(file_url))
78
+ file_url.label_index.set(color_index) unless file_url.label_index.get == color_index
79
+ # sleep 0.01
80
+ end
81
+ actual
82
+ end
83
+
84
+
85
+
86
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # queue_server
3
+
4
+ require 'thread'
5
+ require 'drb'
6
+
7
+ $SAFE = 0
8
+
9
+ def run_queue(url='druby://127.0.0.1:21001')
10
+ queue = Queue.new
11
+
12
+ DRb.start_service(url, queue)
13
+ puts 'Listening for connection...'
14
+ while job = queue.deq
15
+ begin
16
+ yield job
17
+ rescue StandardError => e
18
+ puts e
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,101 @@
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+ require File.join(File.dirname(__FILE__), '/../lib/finder_color')
4
+
5
+ class FinderColorTest < Test::Unit::TestCase
6
+ include FinderColor
7
+
8
+ def setup
9
+ range1 = (1..200)
10
+ range2 = (201..400)
11
+ range3 = (401..600)
12
+ range4 = (601..800)
13
+ @files = (1..800).map {|i| "files/test_file_#{i}"}
14
+ reset_files
15
+ # @file_refs = @files.map{|f| mac_file_url(full_path(f))}
16
+ @indices = (0..7).to_a
17
+ @loop = @indices.push(@indices.shift)
18
+ @file_path = full_path('files/test_file_1')
19
+ @mac_file_url = mac_file_url(@file_path)
20
+ @batch = { 1 => range1.map {|i| full_path("files/test_file_#{i}")},
21
+ 2 => range2.map {|i| full_path("files/test_file_#{i}")},
22
+ 3 => range3.map {|i| full_path("files/test_file_#{i}")},
23
+ 4 => range4.map {|i| full_path("files/test_file_#{i}")},
24
+ }
25
+ end
26
+
27
+ def teardown
28
+ # remove_files
29
+ end
30
+
31
+
32
+ def test_set_label_color
33
+ li = label_index(@mac_file_url)
34
+ set_label_color(@mac_file_url, @loop[li])
35
+ assert_label_index @loop[li], @mac_file_url
36
+ FinderColor::Labels.each do |label_name|
37
+ assert_nothing_raised do
38
+ set_label_color(@mac_file_url, label_name)
39
+ assert_label_index Labels.index(label_name), @mac_file_url
40
+ end
41
+ end
42
+ end
43
+
44
+ def test_get
45
+ assert_equal 0, FinderColor.get_index(@file_path)
46
+ assert_equal :none, FinderColor.get_color(@file_path)
47
+ end
48
+
49
+ def test_set
50
+ FinderColor.set_index(@file_path, 3)
51
+ assert_equal 3, FinderColor.get_index(@file_path)
52
+ FinderColor.set_color(@file_path, :blue)
53
+ assert_equal :blue, FinderColor.get_color(@file_path)
54
+ assert_raises ArgumentError do
55
+ FinderColor.set_index(@file_path, 't')
56
+ end
57
+ assert_raises ArgumentError do
58
+ FinderColor.set_index(@file_path, 9)
59
+ end
60
+ end
61
+
62
+
63
+ def test_batch_processing
64
+ 3.times do |i|
65
+ puts "iteration #{i}"
66
+ reset_files
67
+ batch_set(@batch)
68
+ labels = @files.map {|file| label_index(mac_file_url(full_path(file))) }
69
+ zeroes = labels.reject {|l| l != 0}
70
+ assert_equal 0, zeroes.size
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ def assert_label_index(expected, file_url)
77
+ assert_equal expected, label_index(file_url)
78
+ end
79
+
80
+ def full_path(path)
81
+ File.join(File.dirname(File.expand_path(__FILE__)), path)
82
+ end
83
+
84
+ def create_test_file(path)
85
+ FileUtils.touch path
86
+ end
87
+
88
+ def create_files
89
+ @files.each {|f| create_test_file full_path(f) }
90
+ end
91
+
92
+ def remove_files
93
+ @files.each {|f| FileUtils.rm full_path(f) if File.exist?(full_path(f))}
94
+ end
95
+
96
+ def reset_files
97
+ remove_files
98
+ create_files
99
+ end
100
+
101
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: findercolor
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2007-07-30 00:00:00 -05:00
8
+ summary: set Finder labels (colors) in Mac OS X using AppleEvents instead of osascript
9
+ require_paths:
10
+ - lib
11
+ email: automatthew@gmail.com
12
+ homepage: http://findercolor.rubyforge.org/
13
+ rubyforge_project: findercolor
14
+ description: FinderColor is a an extremely simple interface to the Finder label colors in Mac OS X. It uses Apple Events via the rb-appscript bridge to allow the getting and setting of colors without locking up the Finder using AppleScript. FinderColor can get and set labels by index or by color names (as symbols, e.g. :none, :orange, :red).
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Matthew King
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - bin/color_client.rb
37
+ - bin/color_server.rb
38
+ - lib/finder_color.rb
39
+ - lib/queue_server.rb
40
+ - test/test_finder_color.rb
41
+ test_files:
42
+ - test/test_finder_color.rb
43
+ rdoc_options:
44
+ - --main
45
+ - README.txt
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.txt
50
+ executables:
51
+ - color_client.rb
52
+ - color_server.rb
53
+ extensions: []
54
+
55
+ requirements: []
56
+
57
+ dependencies:
58
+ - !ruby/object:Gem::Dependency
59
+ name: rb-appscript
60
+ version_requirement:
61
+ version_requirements: !ruby/object:Gem::Version::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 0.3.0
66
+ version:
67
+ - !ruby/object:Gem::Dependency
68
+ name: hoe
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Version::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.2.2
75
+ version: