#! /usr/bin/perl -w # # un_commentLines.pl - Comments or uncomments the selected lines # Uses '# ' for Perl and shell scripts; '--' for .applescript's, '// ' otherwise # # -- PB User Script Info -- # %%%{PBXName=Un/Comment Selection}%%% # %%%{PBXInput=Selection}%%% # %%%{PBXOutput=ReplaceSelection}%%% # %%%{PBXKeyEquivalent=@/}%%% # my $outputString = ""; my $perlCmt = "#"; my $cCmt = "//"; my $aCmt = "--"; my $aExt = ".applescript"; my $aExt2 = ".scpt"; # Get the file name/path # my $filePath = <<'FILEPATH'; %%%{PBXFilePath}%%% FILEPATH # Get the text of the file my $fileString = <<'ALLTEXT'; %%%{PBXAllText}%%% ALLTEXT # determine the type of file we have by looking for the #! line at the top # careful--it might already be commented out! my $commentString; if ($fileString =~ m!^($perlCmt|$cCmt)?#\!\s*.*?/perl|^($perlCmt|$cCmt)?#\!\s*.*?/sh!) { $commentString = $perlCmt; } elsif (($filePath =~ $aExt) || ($filePath =~ $aExt2)) { $commentString = $aCmt; } else { $commentString = $cCmt; } my @selection = ; # read the selection from standard input if (!@selection) { exit; }; # no chars in selection, nothing to do # add or remove comment markers depending on the state of the first line of the selection # if it is uncommented, comment all lines. If it is commented, remove comment markers, if present my $firstLineOfSelection = $selection[0]; #get first line my $addingCommentsString = 1; if ($firstLineOfSelection =~ /^$commentString/) { #selection starts with comment $addingCommentsString = 0; } foreach my $line (@selection) { if ($addingCommentsString == 1) { $outputString .= $commentString.$line; } else { $line =~ s/^$commentString//; $outputString .= $line; } } print "%%%{PBXSelection}%%%"; print $outputString; print "%%%{PBXSelection}%%%";