I wanted a context menu hook that would rename any file to one that is compatible with the android resource filename format. I tried to get a craigslist programmer to do it, however craigslist programmers are whiny, insecure bitches. And...I ended up doing it myself to prove a point to one of them. The code looks like shit and has a few bugs on it, but I've already wasted enough time. I'm pasting it here in case any of you do Android programming.
require 'win32/registry.rb'
#
# To Install:
#
# 1. r-click on command window, run as administrator
# 2. ruby [this script name] --register
def registerContextMenu(myPath, rubyLoc)
#http://www.howtogeek.com/107965/how-to-add-any-application-shortcut-to-windows-explorers-context-menu/
keyParent = "*\\shell\\"
myPath = rubyLoc + " " + myPath + " %1"
key = keyParent + "Rename as Android Resource"
begin
reg = Win32::Registry::HKEY_CLASSES_ROOT.create(key, Win32::Registry::KEY_WRITE)
commandReg = Win32::Registry::HKEY_CLASSES_ROOT.create(key + "\\command", Win32::Registry::KEY_WRITE)
commandReg.write_s(nil, myPath)
rescue Win32::Registry::Error
puts "Access is denied; run command line as Administrator"
exit
end
end
def fileToAndroid(name)
a = name.reverse.split(".", 2).reverse
base = filePartToAndroid(a[0].reverse, dupeBuster())
ext = (a.length > 1) ? filePartToAndroid(a[1].reverse, "") : ""
filename = (base + "." + ext).gsub(/[\.]$/,'')
if File.exist?(filename)
filename = base + dupeBuster()
end
return filename
end
def filePartToAndroid(name, default)
name = name.gsub(/[-\. !@#$%^&*(),]/,'_').split(/_+/).join("_").gsub(/^_+/,'')
return ((name.empty?) ? default : name)
end
def dupeBuster()
count = Dir['**/*'].count { |file| File.file?(file) }
return count.to_s(16)
end
def renameToAndroid(filename)
File.rename(filename, File.dirname(filename) + "/" + fileToAndroid(File.basename(filename)));
end
begin
if ARGV[0].nil?
puts "at least one argument expected. use --register to register in context menu"
sleep(2)
exit
elsif ARGV[0] === "--register"
rubyLoc = "C:\\Ruby193\\bin\\ruby.exe"
registerContextMenu(File.expand_path(File.dirname(__FILE__)) + "/" + $0, rubyLoc)
sleep(2)
else
if !File.exists?(ARGV[0])
puts "file does not exist"
sleep(2)
exit
end
renameToAndroid(ARGV[0]) #"--test-a-b-.-c-"
end
rescue => msg
puts "error encountered: " + msg.to_s
puts msg.backtrace
sleep(10)
end
# ----------------- other notes -- this shit didnt work ---------------
# context menu
# http://msdn.microsoft.com/en-us/library/aa753589%28v=vs.85%29.aspx
# ruby call DLL
# http://stackoverflow.com/questions/8099694/what-are-the-ruby-win32api-parameters-how-do-i-pass-a-null-pointer/14182709#14182709
#or..
#http://stackoverflow.com/questions/190168/persisting-an-environment-variable-through-ruby
#useful
#http://en.wikibooks.org/wiki/Ruby_Programming/Standard_Library/Win32::Registry
def registerContextMenu2(myPath)
#HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment