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


Click here to return to the 'Perl script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perl script
Authored by: hayne on Dec 10, '04 06:33:04AM
Here (below) is a Perl script that will pass the appropriate 'man:' URL to Safari, opening either a new window or a new tab according to your choice. (Edit the last line of the script to choose which way you want it to work.) Note that opening a new tab (as in the original script suggested in the article) requires that "Assistive Devices" be enabled in the Universal Access preferences. The script checks for this and shows you the Apple web page about enabling this if it is currently disabled.

As usual, you need to save this script into a file, make that file executable (with 'chmod +x') and make sure that file is in a folder that is on your execution PATH.

Sample use (in a Terminal window), assuming you saved it in a file called "mann":

mann ps
mann

The first would show the man page for 'ps'.
The second would show the index page of all available man pages.


#!/usr/bin/perl -w
use strict;

#---------------------------------------------------------------
# This Perl script is for use with "Bwana" (or other 'man:' protocol handler)
# as described on this MacOSXHints page:
# http://www.macosxhints.com/article.php?story=200412080923108
#
# Invoke this script with one command-line argument specifying the name
# of a Unix man page topic and it will display that man page in Safari.
# If you invoke it without any command-line argument, it will show an
# index page of all available man pages.
# (If you invoke it with the special command-line argument "refresh_index",
#  it will pass this on to Bwana with the effect of ignoring any cached
#  index page or MANPATH.) 
#
# Note that there are two different Perl functions available:
# - newSafariWindowWithUrl
# - newSafariTabWithUrl
# The latter requires that "Assistive Devices" be enabled in the
# Universal Access System Preferences.
# The default is to use 'newSafariWindowWithUrl' so if you prefer
# having a new tab instead of a new window, change the last line
# of the script to use 'newSafariTabWithUrl' instead.
#
# Cameron Hayne (macdev@hayne.net) December 2004
#---------------------------------------------------------------

# -- FUNCTIONS --

# runAppleScript: Runs the supplied AppleScript
#                 The argument is the text of the AppleScript.
sub runAppleScript($)
{
    my ($ascript) = @_;

    my $result = `/usr/bin/osascript<<"    EOT"
    $ascript
    EOT
    `;
    chomp($result);
    return $result;
}

# newSafariWindowWithUrl: Opens a new Safari window with the given URL
sub newSafariWindowWithUrl($)
{
    my ($url) = @_;

    runAppleScript(qq(

    tell application "Safari"
	activate
	make new document at end of documents
	set URL of document 1 to "$url"
    end tell

    ));
}

# newSafariTabWithUrl: Opens a new Safari tab with the given URL
sub newSafariTabWithUrl($)
{
    my ($url) = @_;

    runAppleScript(qq(

    -- bring Safari to the front
    tell application "Safari"
	activate
    end

    -- check that GUI Scripting has been enabled
    tell application "System Events"
	if UI elements enabled is false then
	    tell application "Safari"
		make new document at end of documents
		set URL of document 1 to "http://www.apple.com/applescript/uiscripting/01.html"
	    end tell
	    set message to "You need to enable Assistive Devices as explained on this web page"
	    display dialog message buttons {"OK"}
	    return
	end if
    end tell

    -- create the new tab
    tell application "System Events"
	tell process "Safari"
	    click menu item "New Tab" of menu "File" of menu bar 1
	end tell
    end tell

    -- set the new tab to show the given URL
    tell application "Safari"
	set URL of document 1 to "$url"
    end tell

    ));
}


# -- MAIN --

my $topic = "";
if (@ARGV >= 1)
{
    $topic = $ARGV[0];
}

# Uncomment whichever of the two functions you want to use:
#newSafariTabWithUrl("man:$topic");
newSafariWindowWithUrl("man:$topic");



[ Reply to This | # ]
Perl script
Authored by: farranco on Dec 10, '04 08:06:28AM

I added the ability to modify the Bwana AppleScripts internally in the new version and made this script available with the new Bwana. All you have to do is change the script in the file scripts.strings in Bwana's resource folder.



[ Reply to This | # ]