I often use MSIE to browse the web on my NT box, and every so often I add a link to my "Favorites" bar. Now the list has gotten rather long and unweildy and so I said to myself, "Wouldn't it be nice to have a page with these links as my start page?" Well... it would. So I wrote this hack of a script to read my favorites and generate a page, which I then set as my start page.

Now I just need to figure out how to make it run automatically when I add links to my favorites.


#!perl -w use strict; my $file = "Link.html"; # File to generate. my $lcolor = "#C0C0C0"; # Color for links. my %links; for( <*.url> ) { my $link = `type "$_"` or die "$^E"; die "System Error! $?\n\$!=$!\n\$^E=$^E\n\t " if 0 != $? >> 8; die "Unusual link, '$link'\n" unless $link =~ s/^ (\[DEFAULT\]\nBASE|\[InternetShortcut\]\n) URL=([^\n]+) # This is the URL for the link. \n.*$/$2/sx; $links{+substr($_,0,-4)} = $link; } my @linkName = reverse sort keys %links; my @linkURL = @links{@linkName}; warn "Replacing $file!\n" and sleep 3 if -e $file; open FILE, "> $file" or die "Unable to open $file!\n\$!=$!\n\$^E=$^E\n +\t "; print FILE "<HTML>\n<HEAD><TITLE>Favorite Links</TITLE></HEAD>\n", "<BODY bgcolor=#000000 link=$lcolor alink=$lcolor vlink=$lc +olor>\n", "<CENTER><B><TABLE WIDTH=100%>\n" or die "Print Failed!\n\$!=$!\n\$^E=$^E\n\t "; print FILE "<TR><TD><A HREF=\"", pop( @linkURL ), "\">", pop( @linkNam +e ), ( @linkURL ? ( "</A></TD>\n<TD><A HREF=\"", pop( @linkURL ) +, "\">", pop( @linkName ) ) : "&nbsp;" ), "</A></TD></TR>\n" or die "Print Failed!\n\$!=$!\n\$^E=$^E\n\t " while @linkUR +L; print FILE "</TABLE>\n</BODY>\n</HTML>\n" or die "Print Failed!\n\$!=$!\n\$^E=$^E\n\t "; close FILE or die "Close Failed!\n\$!=$!\n\$^E=$^E\n\t "; ###################################################################### +######### INIT { die "Intended for use on MSWin32 system.\n" unless 'MSWin32' eq + $^O } =head1 Usage Open a command window on your MSWin32 and change directories to Your Favorites\Links\ directory. On my NT box this was: C:\WINNT\Profiles\Adam\Favorites\Links\ Run this script. You will now have a file called Links.html which makes for an excellen +t start page. =head1 Author Adam www.perlmonks.org =cut

Replies are listed 'Best First'.
Re: MSIE Favorite Start Page
by ichimunki (Priest) on Jan 12, 2001 at 17:31 UTC

    I'm ambivalent about making a reference to CGI.pm, since the script is so short and sweet. But I guess I will anyway. Handcoding HTML into scripts is a bad habit. And in this case, using CGI allows you to make program logic clearer. Plus, you can generate well-formed HTML with about half the typing, and a lot less worries about it breaking.

    Update

    Apparently I'm getting downvotes for a reasonable suggestion. Here is an example of how much cleaner the code looks when you use CGI to save a static HTML file:

    use CGI qw(:standard); open (FILE, ">test.html") or die "cannot open test.html: $!\n"; print FILE ( start_html( -title => "test page" ), p( "Test paragraph." ), p( "Here is a second paragraph." ), hr(), p( "This is the last." ), end_html() ); close FILE;

    PS. The <center> tag has been deprecated for three years.

      I absolutely agree with you that most CGI projects aught to use CGI.pm, however this is not a cgi script. Proper HTML formatting is only one of the benefits of CGI, but it also comes at a cost: you have to know what you are writing early. Example, the same table as before, but with CGI:
      my @table; while( @linkURL ) { my $linkLeft = td( a( { -href => pop( @linkURL ) } pop( @linkName +) ) ); my $linkRight = @linkURL ? td( a( { -href => pop( @linkURL ) } pop +( @linkName ) ) ) : "&nbsp;"; push @table, TR( $linkLeft, $linkRight ); } print FILE table( {}, @table ) or die "Print Failed, $!, $^E\n\t ";
      Yeah, that's more Perl-ish, but its also less straightforward. Why should I have to build up the entire table in memory before I can print it to the file?

      So I didn't use CGI. I did think about using CGI, I do know how to use CGI, and I made the decision that I didn't want or need CGI for this 10 minute project. Now, if I were runing a web server on my desktop and wanted this page generated on the fly (which would be cool) I would use CGI to generate the header() info for me. I would also use the start_html() and end_html() functions, but I still wouldn't use the table functions.

      Thank you, by the way, for bringing up the subject. Too many people forget that CGI is just another tool in the tool box. Sometimes it isn't the right solution, sometimes its not the right tool.

      PS: Thanks for the tip on <center> being deprecated. I did not know that. According to the w3c spec for HTML 4.01 I should have used <DIV align=center>.

        I said I was ambivalent about CGI due to the nature of the script, and I know why after trying to rewrite it-- there's no lovely solution when loops are involved. I've banged my head up against it because I apparently enjoy the pain. So here's how I solved it with sub refs. (I left the HTML in 3.2 because I got lazy after all this).
        use CGI qw(:standard -nodebug); ... same code through open () ... my $table_cell = sub { print "table_cell called\n"; (@linkName) ? return td( {-align => "left"}, a( {-href => pop( @linkURL ) }, pop( @linkName ) ) ) : return td(); }; my $table_rows = sub { print "table_rows called\n"; my $rows; foreach (@linkName) { $rows .= Tr( &$table_cell, &$table_cell ); } return $rows; }; print FILE ( start_html( -title => "Favorite Links", -bgcolor => "#000000", -link => "$lcolor", -alink => "$lcolor", -vlink => "$lcolor"), table( { -align => "center" }, &$table_rows ), end_html() ) or die "write error: $!\n"; ... and close ...

        For the record (on Win95), your original code died on a filename with the (r) mark in it. And this was after I had commented out the System Error line (line 12) because it kept tripping there-- would you explain what that line does for us less advanced types? Once those two issues were sidestepped, it worked, rendering the very handy page.

(Ovid) Re: MSIE Favorite Start Page
by Ovid (Cardinal) on Jan 13, 2001 at 00:26 UTC
    If you want to play around, you can check out my home page manager script. It allows you to set home pages based upon the day and time. In other words, your home page can automatically be Perlmonks in the morning, CNN at night, and have your favorite site that updates every Wednesday at 5:00 PM be your home page for every Wednesday after 5:00 PM.

    I wrote it quite a while ago and I wince when I look at the code right now, but it's easy to use and only incorporates standard modules.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: MSIE Favorite Start Page
by c-era (Curate) on Jan 13, 2001 at 02:29 UTC
    You can use the scedular to run this script every hour or so. You can't make a daemon, but you can make an NT service that may be able to update your file on change.
Re: MSIE Favorite Start Page
by Steeeeeve (Initiate) on Jan 16, 2001 at 20:59 UTC
    What you need is similar to what I developed for my news ticker application. I wrote a Perl method to read a flatfile and generate the list of ticker items whenever new items are added. It also purges old items. You might consider developing a small flatfile dBase of your links. That would allow for some nifty management capabillities. You could also relate each to a date, relegating their status to something other than recent after 60 days. They could then be categorized on permanent pages. You could also track the number of times you have accessed each page. Gee,, there's lots of stuff you could do. I'd just use EZDB from ezperl.com and use a flatfile. Its easier to write the Perl backend utilities with a flatfile. -Steeeeeve