The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Help in UNIX (http://hintsforums.macworld.com/showthread.php?t=31468)

Abd73fr 12-02-2004 04:43 AM

Help in UNIX
 
hi,

I think it is possible, but don't know how to do it!!!

In a terminal:

echo " Hello"

will print Hello in the terminal.
I am asking if it is possible to get a graphical window to say Hello instead of printing it in the terminal.

Many thanks

macg4dave 12-02-2004 05:25 AM

you can use applescript
----
do shell script " echo Hello"
display dialog result
----

Abd73fr 12-02-2004 05:49 AM

Thanks macg4dave,

But I am writting a script using UNIX, and need from UNIX to pop up this window...

I don't kno if osascript can do that?

Thank you :)

macg4dave 12-02-2004 06:06 AM

What about NStask (See here ) for more info. But you do have to make it in C.

huskerchad 12-02-2004 08:54 AM

If you want to do it from a shell script, I believe osascript is just about your only option.

If you know how to use C, then check into CFUserNotification.

Abd73fr 12-02-2004 08:57 AM

Thanks again macg4dave for help :)

hayne 12-02-2004 11:24 AM

Here's an example of putting up a dialog (via AppleScript) from a shell script:
Code:

#!/bin/sh

# This script illustrates how to put up dialogs via AppleScript
# Note that some of the longer lines have been continued over two lines
# by typing a backslash immediately followed by a Return
# Cameron Hayne (macdev@hayne.net)  December 2004


# a dialog with a text message and some buttons
message="Hello World!"
button=`osascript << EOT
tell application "Terminal"
    say "$message"
    beep
    display dialog "$message" buttons {"Hello", "Goodbye"} \
                            default button "Hello" giving up after 15
    set result to button returned of result
end tell
EOT`

echo "button was $button"

# a dialog with a text message, a textfield, and one button
message="What is your name?"
defName="Fred"
name=`osascript << EOT
tell application "Terminal"
    say "$message"
    beep
    display dialog "$message" buttons {"OK"} default answer "$defName" \
                            default button "OK"
    set result to text returned of result
end tell
EOT`

echo "name: $name"


ezmobius 12-02-2004 05:25 PM

I've got the perfect solution for you! You want a program called cocoadialog. It's free from sourceforge and it allows you to throw up nice aqua dialogs with or without buttons and text inputs from shell scripts or php or perl or whatever. Heres an example of a shell script that throws a dialog

Quote:

#!/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
You can get it here:
http://cocoadialog.sourceforge.net/
It6's really cool I use it all the time for simple gui's for some php cli scripts that I wrote.

Abd73fr 12-03-2004 11:14 AM

Thank you ezmobius.. I am using Pashua:
http://www.bluem.net/downloads/pashua_en/

If you know it, can you please tell me if cocoadialog is better or not?..Thanks

ezmobius 12-03-2004 02:35 PM

I personally like cocoa dialog better. It seemed to have more options than pashua, but I admit that I haven't tried Pashua in a long time so maybe it's gotten better? Anyway cocoa dialog lets you make many different types of dialogs, including: timed warnings like just pop up a dialog with something you want to say to your user and then have the dialog automatically dissapear after x amount of sedconds. Or diaalogs with as manyt buttons as you want. Or the one I use a lot is the single line or many line text input dialog. Plus cocoa dialog has wrappers available for 5 or 6 different scripting languages. It's worth checking out at least.

ettorep 12-04-2004 04:20 AM

I just tried CocoaDialog, it works great. Thanks a lot for this one, it solved me a problem!

DeltaTee 12-08-2004 04:55 PM

If anyoen is interested, I have written a more AppleScript-like front end for Pashua and for ProgBar (displaying progress bars).

send me an email (or post) for more information.

Abd73fr 12-09-2004 05:28 AM

Hi ezmobius,

Thanks for your comments... In fact, I downloaded cocoaDialog but I don't know how it works !!!

Can you please tell me how to run one example (.sh) in a terminal??

Thanks

ezmobius 12-09-2004 10:55 AM

Sure-
Heres a great page with plenty of examples for shell scripts, perl, and even php scripts. It shows how to pop-up many different types of dialogs and how to read back in what button was pushed or what text was entered.
http://cocoadialog.sourceforge.net/examples.html

Abd73fr 12-09-2004 11:21 AM

Dear ezmobius,

Sorry, may be my question was not clear. I saw the examples page, but I ask about the command line that I have to write in a terminal to run this examples.

For example, using Pashua, in a terminal, I write:
./example.sh

where example.sh file exists in the folder of Examples , and it works fine.. :)

Thanks

ezmobius 12-09-2004 11:37 AM

Ok heres the scoop on how to make it happen. Make sure that you install cocoadialog in you /Applications folder without being in a folder of it's own. Just the app inside the Applications folder. Then copy this script:
Quote:

#!/bin/bash

CD="/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
In your terminal type:
pico test.sh

Once pico opens up then you want to paste in the above script. Now hit ctrl-o and return to save then hit ctrl-x to exit. Now you need to make it executable:
chmod +x test.sh
Now you should be able to run it like this:
./test.sh

Hope that answers you questions. On the examples page, they assume that you installed cocoadialog in ~/Applications but I like to put it in the main /Applications folder. So if you use any more of the examples from that page then you need to change the line:
CD="$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"
To:
CD="/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog"
Other than that it should work fine!

Abd73fr 12-09-2004 12:39 PM

it's Fine.. :)

it works now, thanks a lot...
My problem was with this line:

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

Here you are the results: :)

[6:37pm MyMac ~/Desktop/temps]% ./test.sh
User pressed the Yes button
User likes this program
[6:38pm MyMac ~/Desktop/temps]%

Thanks again

bjast 12-12-2004 11:20 PM

I need some help with cocoadialog. Using the script:

#!/bin/bash

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

### Find file or directory and open
rv=`$CD fileselect \
--text "Pick some files and/or directories" \
--with-directory $HOME/ \
--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

open $rv
___________________________________________________________

cocoadialog opens a single name file ('test') just fine. But how can I get it to open a multiname file ('test file')?

Thanks,

bjast

hermasj 12-13-2004 10:48 AM

Try quoting $rv when using it with the open command:

open "$rv"

Steve

bjast 12-13-2004 08:10 PM

Thanks!

That did the job. But how come it did the job?

bjast

ezmobius 12-14-2004 12:58 AM

When you quoted the variable it expands to look like this if you had selected a 2 name file with a space:
'file name'
This 'hides the space from the command line. So your script works. Whenever you write scripts that have variables that might expand into strings with spaces or weird characters. This way no matter what you throw at the script(within reason) will still be handled as you intended.

bjast 12-17-2004 02:32 PM

That's a very helpful insight!

Thanks,

bjast

schwie 03-02-2005 01:00 AM

Bash, inputbox, and variable
 
I haven't been able to find anything in this forum, the CocoaDialog
documentation, or on the web about setting variables in a shell script
through user input and then working with the data.

I'd like to be able to take user input via an inputbox or
standard-inputbox in a shell script and set a variable without the button information embedded in the variable. Can anyone explain to me how to do this?

I was able to use the Perl examples for a standard-inputbox and
inputbox to set a variable, but here's my problem. The variable is
set, but the first line of the variable has the number of the button
that was pressed and then the input data (the data I really care about)
is stuck on the second line of the variable. How do I set the
variable so that just the input data is contained? Does the shell have
a way to split data up like Perl does? Maybe I need to use pipes... Maybe I need to look at more shell examples on the web?


All times are GMT -5. The time now is 06:00 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.