rename2mac 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +26 -0
  4. data/bin/rename2mac +156 -0
  5. data/rename2mac.gemspec +23 -0
  6. metadata +63 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8599bcf752af590a428546ecc70ce7578a44f792
4
+ data.tar.gz: e8e7406f2c38815dab761cdf302c1ac9180b8fec
5
+ SHA512:
6
+ metadata.gz: b2a184cf5133bfebd4a1071d6099b3db32ee2407bc9d91404eec6d6f6733b805dc4121da5691287341e4ca35b0bd4eeef768e740787f6d974ca8dc626d5ef6b7
7
+ data.tar.gz: 1f6440252cca2772a31b4557bcbfd27b07dfcfe487204ff1a77a4b74d96ec5fb20ce3285929c90069b6590869d7607a00d16735ae8d0096bb36f0b76c62b3245
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Mac Ma comomac(at)runbox(dot)com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ rename2mac
2
+ ==========================
3
+ Rename folderful of filenames to Mac OS X supported format (UTF8-MAC). Mac OS X use only.
4
+
5
+ Background:
6
+ --------------------------
7
+ The Mac OS X system handles filenames in modified Unicode format called UTF8-Mac. If a filename contain none UTF8-Mac character, the Finder may exihibit behavour such as disappearing file, constant refresh, unable to open file, etc. To fix this problem, the file has to be renamed using UTF8-Mac format. This program will recursively rename all the filenames that needs to be renamed to UTF8-Mac format.
8
+
9
+ Installation:
10
+ --------------------------
11
+ 1. Install [MacPorts](http://www.macports.org/)
12
+ 2. sudo port install ruby20 rb-rubygems
13
+ 3. sudo gem install rename2mac
14
+
15
+ Usage:
16
+ --------------------------
17
+ rename2mac \[-n\] \<directory\>
18
+
19
+ Infomation:
20
+ --------------------------
21
+ If -n argument is applied, it will only show what will be renamed without making any changes.
22
+
23
+ License:
24
+ --------------------------
25
+ MIT
26
+ Mac Ma comomac(at)runbox(dot)com (C) 2013 Copyright
data/bin/rename2mac ADDED
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ #
5
+ # Rename folderful of filenames to Mac OS X supported format (UTF8-MAC). Mac OS X use only.
6
+ # Mac Ma comomac (at) runbox (dot) com (c) Copyright 2013
7
+ # License MIT
8
+ #
9
+
10
+ if RUBY_VERSION <= '1.9'
11
+ puts 'Require Ruby 1.9 or above.'
12
+ exit
13
+ end
14
+
15
+ unless RbConfig::CONFIG['host_os'] =~ /darwin/
16
+ puts 'For Mac OS X only.'
17
+ exit
18
+ end
19
+
20
+ require 'find'
21
+ require 'fileutils'
22
+ require 'term/ansicolor'
23
+
24
+ include Term::ANSIColor
25
+
26
+ class String
27
+ def to_m
28
+ # convert string to UTF8-MAC format
29
+ self.encode('UTF8-MAC', 'UTF-8')
30
+
31
+ # convert with extra function of replace invalid character to null
32
+ #self.encode('UTF8-MAC', 'UTF-8', invalid: :replace, undef: :replace, replace: '')
33
+ end
34
+
35
+ def to_u
36
+ # convert string to UTF-8 format
37
+ self.encode('UTF-8', 'UTF8-MAC')
38
+
39
+ # convert with extra function of replace invalid character to null
40
+ #self.encode('UTF-8', 'UTF8-MAC', invalid: :replace, undef: :replace, replace: '')
41
+ end
42
+
43
+ def dn
44
+ File.dirname( self )
45
+ end
46
+
47
+ def bn
48
+ File.basename( self )
49
+ end
50
+
51
+ def um_different?
52
+ # return if the string is different when converted to UTF8-MAC format
53
+ self.unpack('H*') != self.to_m.unpack('H*')
54
+ end
55
+
56
+ def print_diff
57
+ # highlight the character that is not UTF8-MAC compatible
58
+ print self.dn + '/'
59
+ self.bn.each_char { |c|
60
+ if c.um_different?
61
+ print c.red.bold
62
+ else
63
+ print c
64
+ end
65
+ }
66
+ puts
67
+ end
68
+ end
69
+
70
+ def Dir.restricted?( dir, excludes=[] )
71
+ rdirs = [
72
+ '.Trash', '.Trashes', '.fseventsd', '.Spotlight-V100', '.DocumentRevisions-V100',
73
+ '.$EXTEND',
74
+ '_SYNCAPP',
75
+ 'Corrupted',
76
+ 'System Volume Information', 'RECYCLER',
77
+ '.sparsebundle',
78
+ '.tmpdir', '.tmp7z',
79
+ '.AppleDouble'
80
+ ]
81
+
82
+ for word in rdirs - excludes
83
+ return true if dir.include?( word )
84
+ end
85
+ return false
86
+ end
87
+
88
+ if ARGV.length == 0
89
+ puts "no dir specified.."
90
+ exit
91
+ else
92
+ if ARGV[0] == '-n'
93
+ # -n argument will show what files/folders will be changed, without making changes
94
+ noop = true
95
+ src_dir = File.expand_path(ARGV[1])
96
+ else
97
+ noop = false
98
+ src_dir = File.expand_path(ARGV[0])
99
+ end
100
+
101
+ unless FileTest.directory?( src_dir )
102
+ puts "dir don't exists. #{src_dir}"
103
+ exit
104
+ end
105
+ end
106
+
107
+ total_success = 0 # success rename counter
108
+ total_fail = 0 # fail rename counter
109
+
110
+ # repeat if discover some success, because new files might be discovered
111
+ total_files = 0
112
+ flag = true
113
+ while flag
114
+ total_files = 0
115
+ flag = false
116
+
117
+ Find.find( src_dir ) {|fullpath|
118
+ # skip files and folders that shouldn't be touched
119
+ Find.prune if Dir.restricted?( fullpath )
120
+ next if fullpath.bn[0..0] == '.'
121
+
122
+ total_files += 1
123
+
124
+ # Unicode of the file
125
+ u = fullpath.bn.unpack('H*')
126
+ # UTF8-MAC code of the file
127
+ m = fullpath.bn.to_m.unpack('H*')
128
+
129
+ if u == m
130
+ # file name compatible with UTF8-MAC
131
+ #puts fullpath.bn.green
132
+ else
133
+ # file name incompatible with UTF8-MAC
134
+
135
+ if FileTest.directory?(fullpath) or FileTest.file?(fullpath)
136
+ # rename directory or file
137
+ begin
138
+ # tell program to restart the rename from beginning after finish, because some files maybe revealed after rename
139
+ FileUtils.mv(fullpath, fullpath.dn.to_m + '/' + fullpath.bn.to_m, :noop => noop)
140
+ fullpath.print_diff
141
+ total_success += 1
142
+ flag = true
143
+ rescue => e
144
+ print 'failed: '
145
+ fullpath.print_diff
146
+ p e.exception
147
+ puts e.backtrace.join("\n\t")
148
+ total_fail+=1
149
+ end
150
+ end
151
+ end
152
+ }
153
+ end
154
+
155
+ puts "Found #{total_files} files, diff filename #{total_success + total_fail}"
156
+ puts "Renamed: #{total_success} Failed: #{total_fail}"
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rename2mac'
3
+ s.version = '0.9.4'
4
+ s.date = '2013-11-15'
5
+ s.summary = "rename2mac"
6
+ s.description = "Rename folderful of filenames to Mac OS X supported format (UTF8-MAC). Mac OS X use only."
7
+ s.authors = ["Mac Ma"]
8
+ s.email = 'comomac@runbox.com'
9
+ s.homepage = 'http://rubygems.org/gems/rename2mac'
10
+ s.license = 'MIT'
11
+
12
+ s.required_rubygems_version = '>= 2.0.3'
13
+
14
+ s.add_dependency 'term-ansicolor', '>= 1.2.2'
15
+
16
+ s.executable = 'rename2mac'
17
+
18
+ s.files = Dir.glob('*')
19
+ s.files.reject! { |fn| fn.include?( '._' ) }
20
+ s.files.reject! { |fn| fn.include?( '.DS_Store' ) }
21
+ s.files.reject! { |fn| fn.include?( '.git' ) }
22
+ s.files.reject! { |fn| fn.include?( '.sublime-' ) }
23
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rename2mac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.4
5
+ platform: ruby
6
+ authors:
7
+ - Mac Ma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: term-ansicolor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.2
27
+ description: Rename folderful of filenames to Mac OS X supported format (UTF8-MAC).
28
+ Mac OS X use only.
29
+ email: comomac@runbox.com
30
+ executables:
31
+ - rename2mac
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - rename2mac.gemspec
38
+ - bin/rename2mac
39
+ homepage: http://rubygems.org/gems/rename2mac
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 2.0.3
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.1.11
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: rename2mac
63
+ test_files: []