Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'safer editing via a Perl script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
safer editing via a Perl script
Authored by: hayne on Sep 29, '06 02:14:17PM
Here is a Perl script that does the editing described by this hint. It makes a backup copy of the file with a ".orig" suffix. (See this Unix FAQ if you need help on running scripts.)

#!/usr/bin/perl
use strict;
use warnings;

# removeFocusOnRss
# This script edits the Javascript file used by Safari's RSS page
# so that it doesn't place the keyboard focus in the RSS search field.
# See: http://www.macosxhints.com/article.php?story=20060924130906315
#
# Cameron Hayne (macdev@hayne.net)  Sept 2006

my $frameworkDir = "/System/Library/PrivateFrameworks/SyndicationUI.framework";
my $resourcesDir = "$frameworkDir/Resources";
my $jsfile = "$resourcesDir/Articles.js";
my $backupSuffix = ".orig";
$^I = $backupSuffix;  # edit in place, saving a copy to ".orig"
@ARGV = ($jsfile);    # make the "<>" magic apply to $jsfile

my $username = $ENV{'SUDO_USER'} ? $ENV{'SUDO_USER'} : $ENV{'LOGNAME'};
my $date = localtime();

if (! -w $resourcesDir)
{
    print STDERR "No write permission on \"$resourcesDir\"\n";
    print STDERR "Aborting\n";
    exit;
}

print STDERR "Editing \"$jsfile\"\n";

# We abort if there is already a ".orig" file
if (-e "$jsfile$backupSuffix")
{
    print STDERR "There is already a \"$backupSuffix\" copy of \"$jsfile\"\n";
    print STDERR "Aborting\n";
    exit;
}

my $targetFunc = "setupFilter";
my @linesToAdd = (
                 "// Following line commented out by $username ($date)",
                 );

while (<>)
{
    if (/^function $targetFunc\(/ .. /^}/)
    {
        if (/^\s*filterField\.focus\(\);\s*$/)
        {
            print join("\n", @linesToAdd), "\n";
            s|^|//|; # add "//" at the beginning of this line
            print STDERR "Commented out the focus line in \"$jsfile\"\n";
            print STDERR "Original file saved as \"$jsfile$backupSuffix\"\n";
        }
    }
    print;
}

# print a 'diff' of the files just as a confirmation that all went as planned
print STDERR "Changes made to \"$jsfile\":\n";
print STDERR `diff $jsfile$backupSuffix $jsfile`;
By the way, I decided not to make this change to the Articles.js file since my major problem is with the case when the page hasn't finished loading yet. As mentioned in an earlier comment, in this case the Javascript onload function hasn't been called yet, so this hint doesn't help things. And I usually page down with the arrow keys in any case and these are specially provided for in the code for the search field.

[ Reply to This | # ]