Skip to content

Instantly share code, notes, and snippets.

@jhuckaby
Created July 8, 2012 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhuckaby/3069181 to your computer and use it in GitHub Desktop.
Save jhuckaby/3069181 to your computer and use it in GitHub Desktop.
A better subl for launching Sublime Text 2 via the command-line (forces file to open in new window)
#!/usr/bin/perl
# A replacement for Sublime Text 2's built-in "subl" command-line utility,
# which forces files into new windows regardless of prefs.
# by Joseph Huckaby, 2012
use strict;
use Cwd qw/abs_path/;
use FileHandle;
while (my $path = shift @ARGV) {
$path = abs_path( $path );
if (-d $path) {
# sublime's own subl works fine for opening directories into new windows
print `"/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" "$path"`;
}
elsif (-e $path) {
# but individual files are merged into the frontmost window as a new tab regardless of prefs,
# so use applescript to force it into a new window every time
print call_applescript( join("\n",
'tell application "Finder"',
'open file (POSIX file "'.$path.'") using ((path to applications folder as text) & "Sublime Text 2")',
'end tell'
) );
}
else { die "File not found: $path\n"; }
}
exit;
sub call_applescript {
# invoke applescript given chunk of script code
my $scpt = shift;
my $temp_file = '/var/tmp/script_'.$$.'.scpt';
save_file( $temp_file, $scpt );
my $result = `/usr/bin/osascript $temp_file 2>&1`;
unlink $temp_file;
return $result;
}
sub save_file {
my $file = shift;
my $contents = shift;
my $fh = new FileHandle ">$file";
if (defined($fh)) {
$fh->print( $contents );
$fh->close();
return 1;
}
return 0;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment