CocoaDialog
overview | download | documentation | examples | todo | sf.net project page

ok-msgbox shell script

download

#!/bin/bash

CD="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"

rv=`$CD ok-msgbox --text "We need to make sure you see this message" \
    --informative-text "(Yes, the message was to inform you about itself)" \
    --style critical --no-newline`
if [ "$rv" == "1" ]; then
    echo "User said OK"
elif [ "$rv" == "2" ]; then
    echo "Canceling"
    exit
fi
			

back to the top

yesno-msgbox shell script

download

#!/bin/bash

CD="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"

### Example 1
rv=`$CD yesno-msgbox --no-cancel --string-output --no-newline \
    --text  "This is a simple first example" \
    --informative-text "We're just going to echo the string output"`
echo "User pressed the $rv button"

### Example 2
rv=`$CD yesno-msgbox --text "Do you like CocoaDialog?"`
if [ "$rv" == "1" ]; then
    echo "User likes this program"
elif [ "$rv" == "2" ]; then
    echo "User does not like this program"
elif [ "$rv" == "3" ]; then
    echo "User has no opinion (pressed cancel)"
fi
			

back to the top

msgbox shell script

download

#!/bin/bash

CD="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"

rv=`$CD msgbox --no-newline \
    --text "What's your favorite OS?" \
    --informative-text "The 'Cancel' label auto-binds that button to esc" \
    --button1 "OS X" --button2 "GNU/Linux" --button3 "Cancel"`
if [ "$rv" == "1" ]; then
    echo "User likes Macs"
elif [ "$rv" == "2" ]; then
    echo "User likes Linux"
elif [ "$rv" == "3" ]; then
    echo "User doesn't care"
fi
			

back to the top

standard-inputbox Perl script

download

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

our $CD = "$ENV{HOME}/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";

my $rv = `$CD standard-inputbox --title "Your Name" --no-newline \\
    --informative-text "Enter your name"`;

my ($button_rv, $name) = split /\n/, $rv, 2;
if ($button_rv == 1) {
    print "Hello $name\n";
} elsif ($button_rv == 2) {
    print "No name given\n";
}
			

back to the top

inputbox Perl script

download

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

our $CD = "$ENV{HOME}/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";

my $rv = `$CD inputbox --title "Search" --no-newline \\
    --informative-text "Enter your search term" \\
    --text "foobar" \\
    --button1 "Search" --button2 "Search all" \\
    --width 600`;

my ($button_rv, $term) = split /\n/, $rv, 2;
if ($button_rv == 1) {
    print "Search for '$term'\n";
} elsif ($button_rv == 2) {
    print "Search all files for '$term'\n";
}
			

back to the top

progressbar Perl script

download

#!/usr/bin/perl -w

use strict;
use IO::File;

our $COCOA_DIALOG = "$ENV{HOME}/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";
die "$COCOA_DIALOG doesn't exist" unless -e $COCOA_DIALOG;

###
### EXAMPLE 1
###

### Open a pipe to the program
my $fh = IO::File->new("|$COCOA_DIALOG progressbar");
die "no fh" unless defined $fh;
$fh->autoflush(1);

my $percent = 0;
for (my $percent = 0; $percent <= 100; $percent++) {
    if (!($percent % 5)) {
        ### Update the progressbar and its label every 5%
        print $fh "$percent we're at $percent%\n";
    } else {
        ### Update the progressbar every percent
        print $fh "$percent\n";
    }
    ### simulate a long operation
    1 for (0 .. 90_000);
}

### Close the filehandle to send an EOF
$fh->close();

###
### EXAMPLE 2
###

### Now let's do an indeterminate one
$fh = IO::File->new("|$COCOA_DIALOG progressbar --indeterminate");
die "no fh" unless defined $fh;
$fh->autoflush(1);

### Just loop an arbitrary number of times to simulate something taking
### a long time
for (0 .. 1_500_000) {
    ### Update the label every once and a while.
    if (!($_ % 300_000)) {
        my @msgs = ('Still going', 'This might take a while',
            'Please be patient', 'Who knows how long this will take');
        my $msg = @msgs[rand @msgs];
        ### It does not matter what percent you use on an indeterminate
        ### progressbar.  We're using 0
        print $fh "0 $msg\n";
    }
}

### Close the filehandle to send an EOF
$fh->close();

###
### EXAMPLE 3
###

### Here's a more practical example of using an indeterminate progressbar
my $args = '--title "Working..." --text "This will take a while"';
$fh = IO::File->new("|$COCOA_DIALOG progressbar --indeterminate $args");
die "no fh" unless defined $fh;
$fh->autoflush(1);

# Do your really long operation here.
sleep 8;

$fh->close();
			

back to the top

progressbar shell script

Thanks to Kevin Hendricks for writing this and sending it in.
download

#!/bin/bash

# create a named pipe
rm -f /tmp/hpipe
mkfifo /tmp/hpipe

# create a background job which takes its input from the named pipe
~/Desktop/CocoaDialog.app/Contents/MacOS/CocoaDialog progressbar \
	--indeterminate --title "My Program" \
	--text "Please wait..." < /tmp/hpipe &

# associate file descriptor 3 with that pipe and send a character through the pipe
exec 3<> /tmp/hpipe
echo -n . >&3

# do all of your work here
sleep 20

# now turn off the progress bar by closing file descriptor 3
exec 3>&-

# wait for all background jobs to exit
wait
rm -f /tmp/hpipe
exit 0
			

back to the top

textbox Perl script

download

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

our $CD = "$ENV{HOME}/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";

my $rv;

### Non-editable example
if (-e "COPYING") {
    $rv = `$CD textbox --title "License" --no-newline \\
        --informative-text "Do you agree with the terms of this license?" \\
        --text-from-file COPYING --button1 Ok --button2 Cancel`;
} else {
    $rv = `$CD textbox --title "License" --no-newline \\
        --informative-text "Do you agree with the terms of this license?" \\
        --text "This is the text of the license...." \\
        --button1 Ok --button2 Cancel`;
}
if ($rv == 1) {
    print "User agrees\n";
} else {
    print "User canceled\n";
}

### Editable example
$rv = `$CD textbox --title "Tell me a story" \\
    --informative-text "Write up a story..." \\
    --button1 "Echo" \\
    --button2 "Cancel" \\
    --text "Whatever you want" \\
    --selected \\
    --scroll-top top \\
    --editable`;
# First line is the button value, the rest is the textbox
my ($button_rv, $text) = split /\n/, $rv, 2;
if ($button_rv == 1) {
    print "$text";
} elsif ($button_rv == 2) {
    print "User hit cancel\n";
}
			

back to the top

fileselect shell script

download

#!/bin/bash

CD="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"

### Example 1
rv=`$CD fileselect \
    --text "Choose the source file for the main controller" \
    --with-extensions .c .m .cpp`
if [ -n "$rv" ]; then  ### if $rv has a non-zero length
    echo "Main source: $rv"
else
    echo "No source file selected"
fi

### Example 2
rv=`$CD fileselect \
    --text "Pick some files and/or directories" \
    --with-directory $HOME/Documents/ \
    --select-directories \
    --select-multiple`
if [ -n "$rv" ]; then
    ### Loop over lines returned by fileselect
    echo -e "$rv" | while read file; do
        ### Check if it's a directory
        if [ -d "$file" ]; then
            echo "Directory: $file"
        ### Else a regular file
        elif [ -e "$file" ]; then
            echo "Regular file: $file"
        fi
    done
else
    echo "No files chosen"
fi
			

back to the top

PHP wrapper functions and examples

Thanks to Stefan Lange-Hegermann for sending in this set of PHP functions to wrap various CocoaDialog controls.

download

#!/usr/bin/php
<?php
/*
Very Simple PHP Wrapper for CocoaDialog
This is free, no limitations whatsoever
use at your own risc

Written by Stefan Lange-Hegermann
*/

// Settings:
/**********/

define("CDPATH", "~/Applications/CocoaDialog.app");

/**********/


function CDmsgbox($title,$text,$informtext,$button1,$button2='',$button3='',$style='informational')
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog msgbox \\
			--text \"$text\" --informative-text \"$informtext\" \\
			--button1 \"$button1\" --button2 \"$button2\" \\
			--button3 \"$button3\" --style $style --title \"$title\"";
    return exec($execstr);
}

function CDokmsgbox($title,$text,$informtext,$nocancel=false,$style='informational')
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog ok-msgbox \\
			--text \"$text\" --informative-text \"$informtext\" \\
			--style $style --title \"$title\" ";
    if ($nocancel) $execstr.='--no-cancel';
    return exec($execstr);
}

function CDyesnomsgbox($title,$text,$informtext,$nocancel=false,$style='informational')
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog yesno-msgbox \\
			--text \"$text\" --informative-text \"$informtext\" \\
			--style $style --title \"$title\"";
    if ($nocancel) $execstr.=' --no-cancel';
    return exec($execstr);
}

function CDfileselect($title,$text,$startdir='',$extlist='',$selectdirs=false,$selectmulti=false)
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog fileselect \\
			--text \"$text\" --with-directory \"$startdir\" \\
			--title \"$title\"";
    if ($selectdir) $execstr.=' --select-directories';
    if ($selectmulti) $execstr.=' --select-multiple';
    if ($startdir!='') $execstr.=" --with-extensions $extlist";

    return exec($execstr);
}

function CDtextbox($title,$text,$informtext,$button1,$button2='',
	$button3='',$editable=false,$selected=false,$scrollto='top')
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog textbox \\
			--text \"$text\" --informative-text \"$informtext\" \\
			--button1 \"$button1\" --title \"$title\" --scroll-to $scrollto";
    if ($button2!='')  $execstr.=" --button2 \"$button2\"";
    if ($button3!='')  $execstr.=" --button3 \"$button3\"";
    if ($editable) $execstr.=' --editable';
    if ($selected) $execstr.=' --selected';
    return shell_exec($execstr);
}

function CDprogressbar($title,$text,$percent=0,$indeterminate=false)
{
		$execstr=CDPATH."/Contents/MacOS/CocoaDialog progressbar \\
			--text \"$text\" --title \"$title\" --percent $percent";
    return popen ( $execstr, "w");
}

function CDbarvalue($bar,$value,$text='')
{
    $space='';
    if ($text!='') $space=' ';
    fwrite($bar,$value.$space.$text."\n");
}


// To use this as a library remove everything from

// ****** HERE ******


// msgbox
echo CDmsgbox("CocoaDialog : msgbox","message box",
	" This control provides a generic message box.\nIt allows you to customize the labels of the buttons.",
	"one","two","three")."\n";

// ok-msgbox
echo CDokmsgbox("CocoaDialog : ok-msgbox",
	"OK/Cancel message box",
	"This control provides a standard Ok/Cancel message box.",
	true)."\n";

// yesno-msgbox
echo CDyesnomsgbox("CocoaDialog : yesno-msgbox",
	"Yes/No/Cancel message box",
	" This control provides a standard Yes/No/Cancel message box.")."\n";

// fileselect
echo CDfileselect("CocoaDialog : fileselect",
	"file selection window")."\n";

// textbox
echo CDtextbox("CocoaDialog : textbox","text box",
	" This is a text box with a large text area.",
	"One","Two","",true)."\n";

// progressbar
$myPBar = CDprogressbar("CocoaDialog : progressbar",
	"progress bar");
for ($a=1;$a<101;$a++)
{

    for ($b=0;$b<1;$b=$b+0.0002)
    {
        $foo=sin($b);
    }
    CDbarvalue($myPBar,$a,"Calculation progress: ".$a."%");
}
fclose($myPBar);

echo CDokmsgbox("Done","All GUI Controls done",
	"You have seen everything, now let's hope for a simple one line Text input control ;)",
	true)."\n";

// **** To HERE ****
?> 
			

back to the top

Finding CocoaDialog executable

download shell script

#!/bin/bash

COCOA_DIALOG=""

if [ -d "/Applications/CocoaDialog.app" ]; then
    COCOA_DIALOG="/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"
elif [ -d "$HOME/Applications/CocoaDialog.app" ]; then
    COCOA_DIALOG="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"
else
    echo "CocoaDialog.app not found"
    exit 1
fi
			

download Perl script

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

our $COCOA_DIALOG = undef;
for my $path (('/Applications', "$ENV{HOME}/Applications")) {
    if (-d "$path/CocoaDialog.app") {
        $COCOA_DIALOG = "$path/CocoaDialog.app/Contents/MacOS/CocoaDialog";
        last;
    }
}
unless (defined $COCOA_DIALOG) {
    die "Could not find CocoaDialog.app";
}
			

back to the top