Command Line Utility to Make Files Executable
If you are like me and you make a lot of command line utilities then this script is for you. There are only a few steps to making an executable but this is exactly what makes it a good candidate for automation.
I make this script an executable naming it 'make_exe' and it goes in a folder called 'my_bin' in my home directory. Then when I want to make another file executable I just type 'make_exe' in a Terminal (I mainly use iTerm) it pops up a dialog asking me to pick the file, give it a name and it does the rest.
It makes a copy of the file that was chosen giving it the new name specified. It then chmods the new file to 755 (change to your liking). After that it is moved to '/users/user_name/my_bin/' (again, change to your liking).
Add this folder's path to your '~/.bash_login' or '.bash_profile' whichever one you use.export PATH="$HOME/my_bin:$PATH"
If you have a terminal session open you will need to create a new one or reload the .bash file.
. ~/.bash_login
#!/usr/bin/env ruby -w
=begin
Make this file an executable file and
place in your @default_bin folder.
I made a folder called 'my_bin' in my
home directory.
Add this folder's path to your '~/.bash_login'
or '.bash_profile' whichever one you use.
eg. export PATH="$HOME/my_bin:$PATH"
Each file you make executable will be moved
to your default folder.
=end
class MakeExecutable
require 'appscript'; include Appscript
require 'osax'; include OSAX
require 'tmpdir'
def initialize
@default_bin = "#{ENV['HOME']}/my_bin"
@chmod = 755
@temp_error_file = "#{Dir.tmpdir}/make_exe_errors.txt"
end
def main_worker_bee
choose_file
choose_name
create_exe
error = move_to_bin
if error == false
error_message(error)
end
end
def choose_file
@file = osax.choose_file('Choose file to make executable')
@dir = File.dirname(@file.to_s)
end
def choose_name
name = osax.display_dialog('Name your executable',
:default_answer => File.basename(@file.to_s, '.*'),
:buttons => ['Cancel ', 'Do it!'],
:default_button => 2
)
exit if name[:button_returned] == 'Cancel '
@name = name[:text_returned]
end
def create_exe
@new_name_path = "#{@dir}/#{@name}"
`cp '#{@file}' '#{@new_name_path}'`
`chmod #{@chmod} #{@new_name_path}`
end
def move_to_bin
command = "#{@new_name_path} #{@default_bin} 2> #{@temp_error_file}"
return system("mv #{command}")
end
def error_message(error)
answer = osax.display_alert("There was an error\nError: #{error}",
:as => :warning,
:buttons => ['Try again', 'Exit'],
:default_button => 1
)
if answer[:button_returned] == 'Try again'
main_worker_bee
else
exit
end
end
end
if __FILE__ == $0
MakeExecutable.new.main_worker_bee
end

