Forums

Trivial code: Opening URLs in Firefox, one at a time

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Trivial code: Opening URLs in Firefox, one at a time

Postby ObtuseAngle » Tue Jun 07, 2011 5:32 pm

Occasionally at work I get a list of URLs (well, it starts out as an Excel spreadsheet where one column contains barcode numbers that become pieces of URLs, but that's beside the point for this forum - see my previous post about reading from Excel) to be checked.

Rather than laboriously cutting and pasting each URL into a browser, I have a little Python program that steps through the list opening up the URLs in a web browser, one at a time (uses wx(Widgets) for the minimal interface). It's not always me checking the lists, and it's not always on the same machine.

For your amusement, below is my first attempt at doing the same in Cobra using WPF.
I'm not a professional programmer, and this is what I want Cobra for, not large significant projects but small time-saving utilities.

Interesting points (for me, anyway): I want a small stay-on-top window with a Next button, I want the URL to open in Firefox (even if that's not the default browser on the machine in use).

Feel free to criticise, correct, or improve the code, I'm still taking baby steps with Cobra, .NET, and the use of contracts. (And yes, I should probably move generating the list of URLs outside the button event-handling function.)

Cheers, Obtuse.

Code: Select all
"""
Reads a list of URLs from a text file (one line per URL),
opens them up one at a time in Firefox,
leaves a button hanging about to get next URL.

Modified the WPF example in the Cobra documentation.
See http://cobra-language.com/how-to/WPF/
"""

@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework'
@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore'
@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase'

use System.Windows
use System.Windows.Controls
use System.Windows.Controls.Primitives
use System.Windows.Media
use System.Windows.Threading
use System.Diagnostics

class MainWindow inherits Window

    var i = 0       # count of button clicks
    var lst = []    # List of URLs
    var fileName = "tocheck.txt"

    cue init
        """
        Set up the GUI - a small window with one button
        """
        base.init
        .title = 'One at a time'
        .width=70           # this is probably crude and unnecessary
        .height=65          #   - haven't yet got the hang of WPF auto-sizing
        .topmost = true   # I want this window (and button) to stay on top

        panel = StackPanel(margin=System.Windows.Thickness(5))
        .content = panel
        button = Button(content='Start')
        listen button.click, ref .buttonClicked
        panel.children.add(button)


    def readListFromFile(fileName as String)
        """
        Reads URLs from a text file, one per line
        and puts them into a list.
       
        In my final program, reads numbers from an Excel file and generates URLs
        """
        require
            File.exists(fileName)
        ensure
            .lst.count > 0
        body
            rl = File.openText(fileName)
            while true
                line = rl.readLine
                if line is nil, break
                .lst.add(line.trim)
            rl.close

    def buttonClicked(sender, ev as RoutedEventArgs )
        if .i == 0           # if this is the first time the button has been pressed,
            .readListFromFile(.fileName)       #   load up the list of URLs,
            sender.content = 'Next' # and change the button from Start to Next
        .openLink(.lst[.i])     # open up the appropriate URL,
        .i += 1                   #     and keep count of the button presses
        if .i == .lst.count     # if we've opened up the last URL,
            .close                #     shutdown the GUI

    def openLink(url as String)
        """
        Open the URL in Firefox,
            even if it is not the default web browser
        """
        Process.start('firefox.exe', url)


class Program

    """
    Set up GUI framework, copied straight from WPF example in Cobra documentation,
    with comments removed.
    See http://cobra-language.com/how-to/WPF/
    """

    def main has STAThread
        Program().run

    def run
        app = Application()
        listen app.dispatcherUnhandledException, ref .dispatcherUnhandledException
        app.run(MainWindow())

    def dispatcherUnhandledException(sender, args as DispatcherUnhandledExceptionEventArgs)
        msg = 'Exception: ' + args.exception.message
        MessageBox.show(msg, 'Unhandled Exception', MessageBoxButton.OK)
        args.handled = true
99 programming bugs in the code, 99 bugs in the code, fix 1 bug and compile it again, 101 programming bugs in the code
ObtuseAngle
 
Posts: 42
Location: Gippsland, Australia

Return to Discussion

Who is online

Users browsing this forum: No registered users and 6 guests

cron