Created
April 21, 2011 18:04
-
-
Save metavida/935119 to your computer and use it in GitHub Desktop.
A script that tweaks the Firefox bundle in OS X to always launch with a specified profile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make your life even easier by adding this line to your .bash_login (or equivalent) file. | |
alias firefox-bin-profile='~/.bash_profile_scripts/firefox-bin-profile.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'fileutils' | |
usage = <<-USAGE | |
firefox-bin-profile.rb (2011-04-19) | |
Usage: firefox-bin-profile.rb [--profile <profile_name>] [-rdh] firefox_app | |
Options: | |
--profile <name> Use the given profile, not the detected version. | |
-d, --default A shortcut for --profile 'default' | |
-r, --revert Remove the custom bin from this firefox_app. | |
-h, --help Show this information. | |
Customize the given firefox_app to always use a specific profile instead of defaulting to the last used profile. | |
If no --profile is specified, each time Firefox launches the profile will be selected based on the name of the Firefox.app. For example, with the name "Firefox 4.0.app" the "4.0" profile will be used. With "Firefox Aurora.app" the "Aurora" profile will be used. The given firefox_app .app name must begin with "Firefox" for this feature to work. | |
Inspired by these instructions: http://hints.macworld.com/article.php?story=20091106222902593 | |
USAGE | |
manual_profile = nil | |
flag = ARGV.shift | |
case flag | |
when '-r', '--revert' | |
flag = :revert | |
when '-d', '--default' | |
manual_profile = 'default' | |
when '--profile' | |
manual_profile = ARGV.shift | |
when /^--?h(elp)?$/ | |
puts usage | |
exit 0 | |
when /^-/ | |
puts "invalid option #{flag}\n\n" | |
puts usage | |
exit 1 | |
else | |
# If none of the patterns match, no flags are given & this was the firefox_app path. | |
ARGV.unshift(flag) | |
end | |
if ARGV.last.nil? | |
puts "No firefox_app specified!\n\n" | |
puts usage | |
exit 1 | |
end | |
ff_path = File.expand_path(ARGV.last.to_s) | |
bin_path = File.join(ff_path, 'Contents/MacOS/firefox-bin') | |
real_path = "#{bin_path}-real" | |
unless File.exists?(ff_path) && File.exists?(bin_path) | |
puts "This firefox_app you provided doesn't look like a copy of Firefox: #{ff_path}\n\n" | |
puts usage | |
exit 1 | |
end | |
if flag == :revert | |
unless File.exists?(real_path) && `file "#{real_path}"` =~ /binary/ | |
puts "This copy of Firefox hasn't been updated, so doesn't need reverting: #{ff_path}" | |
exit 1 | |
end | |
FileUtils.remove_file(bin_path) if File.exists?(bin_path) | |
FileUtils.move(real_path, bin_path) | |
puts "This copy of Firefox has been reverted to the default behavior: #{ff_path}" | |
exit 0 | |
end | |
unless `file "#{bin_path}"` =~ /binary/ || (File.exists?(real_path) && `file "#{real_path}"` =~ /binary/) | |
puts "This copy of Firefox is in an un-manageable state. Something already happened to the firefox-bin: #{ff_path}" | |
exit 1 | |
end | |
begin | |
# If the file in bin_path is a binary, then we need to replace it with our script. | |
# This happens when running this script on fresh FF isntalls or after a Firefox update. | |
if `file "#{bin_path}"` =~ /binary/ | |
FileUtils.remove_file(real_path) if File.exists?(real_path) | |
FileUtils.move(bin_path, real_path) | |
end | |
File.open(bin_path, 'w') do |bin| | |
bin.puts <<-RUBY | |
#!/usr/bin/ruby | |
ff_path = File.expand_path(__FILE__) | |
app_name = ff_path.split('/').detect{|d| d =~ /^Firefox(.*)\.app$/} | |
real_path = "\#{ff_path}-real" | |
version = #{ manual_profile ? "'#{manual_profile}'" : "$1.to_s.strip.gsub(/\\s/, ' ')" } | |
version = version.to_s.gsub(/[^a-z0-9.\\-_ ]/i, '') | |
version = 'default' if version.empty? | |
system("\\"\#{real_path}\\" -P '\#{version}' &") | |
sleep 3 | |
`osascript -e 'tell application "\#{app_name}" to activate'` | |
RUBY | |
end | |
FileUtils.chmod(0755, bin_path) | |
if manual_profile | |
puts "This copy of Firefox will use the '#{manual_profile}' profile when launching: #{ff_path}" | |
else | |
puts "This copy of Firefox will use a profile based on its name when launching: #{ff_path}" | |
end | |
rescue | |
puts "Reverting back to standard Firefox behavior: #{ff_path}" | |
FileUtils.move(real_path, bin_path) if File.exists?(real_path) && `file "#{real_path}"` =~ /binary/ | |
puts $!.message | |
puts $!.backtrace | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment