Problem using gtkdialog to make a playlist for mpc.

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
Trapster
Posts: 185
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 57 times

Problem using gtkdialog to make a playlist for mpc.

Post by Trapster »

I usually make scripts using examples from others'.
This one has me stumped. I am trying to make some dialog to search my music folder and then create a playlist for mpc.
I had this working once but I must have "tweaked" something wrong. :mrgreen:

Can someone tell me why ARTIST1 is not being recognized as a variable?
In this script, it gets to the point of "No Artist Specified" ?? Help.

Code: Select all

#!/bin/bash

#################################################
########Playlists can also be created with
########  mpc ls | grep <artist>
########  mpc add
########  mpc save
#################################################
GTKDIALOG=gtkdialog
export mpcpl='
<window title="mpcpl - Playlist Generator">
<vbox>
<hbox>
<text><label>MPC Playlist Generator.  Enter an Artist Name.</label></text>
</hbox>
 <hbox>
  <text><label>Artist:</label></text>
  <entry accept="text"><variable>ARTIST1</variable><input>cat /usr/local/mpcpl/artist1</input></entry>
 </hbox>
  <hbox>
  <button help>
   <action>`Xdialog --wrap --screencenter --left --title "mpcpl - HELP" --msgbox "mpcpl is a simple GUI for Retrieving Artists From a Music Server and Creates a Playlist. \n\n1. Enter an Artist and Follow the Prompts \n3. Click "ok"\n \n\n " 600x0`</action>
  </button>
  <text><label>_____________</label></text>
  <button ok></button>
  <text><label>_____________</label></text>
  <button cancel></button>
  </hbox>
</vbox>
</window>
'
gtkdialog3 --program=mpcpl --center

#I=$IFS; IFS=""
#for STATEMENTS in  $(gtkdialog3 --program=mpcpl --center); do
#   eval $STATEMENTS
#done
#IFS=$I
#if [ $EXIT = OK ]; then

#######################

echo "$ARTIST1"
#echo $ARTIST1 > /usr/local/mpcpl/artist1
#ARTIST1=$(cat /usr/local/mpcpl/artist1)

if [ "$ARTIST1" = "" ];then
gxmessage -bg orange -center -title "mpcpl" "
ERROR: No Artist was specified.
" \
	-buttons 'OK:1, QUIT:2'
case $? in
    2) killall mpc-pl
	;;

esac

The remainder of the code works if I get the variable.

This is the output from the terminal, so It recognizes what I type in.

Code: Select all

root@linux_server:~$ # mpc-pl
ARTIST1="elton john"
EXIT="OK"
Terminated
User avatar
MochiMoppel
Posts: 1247
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 446 times

Re: gtkdialog

Post by MochiMoppel »

Trapster wrote: Tue Jan 02, 2024 10:41 pm

Can someone tell me why ARTIST1 is not being recognized as a variable?

because it was created in the gtkdialog child process. Your bash script - the parent process - doesn't know about it unless you fetch the gtkdialog variables with something like

Code: Select all

#I=$IFS; IFS=""
#for STATEMENTS in  $(gtkdialog3 --program=mpcpl --center); do
#   eval $STATEMENTS
#done
#IFS=$I 

In your code these lines are commented out, thus have no effect.
Uncomment them and instead comment out the preceding

Code: Select all

gtkdialog3 --program=mpcpl --center
User avatar
Trapster
Posts: 185
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 57 times

Re: gtkdialog

Post by Trapster »

Thank you, thank you, thank you!
That was it.
I had those commented out while trying to debug.
All's well now. :thumbup:

mow9902
Posts: 196
Joined: Fri Jul 24, 2020 11:57 pm
Has thanked: 15 times
Been thanked: 62 times

Re: gtkdialog

Post by mow9902 »

..programming style is quite personal. I think you might also have an unclosed "if" statement.
Anyway - here's my preferred style - which I find more readable.

#gtkdialog3 --program=mpcpl --center

I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog3 --program=mpcpl --center); do
eval $STATEMENTS
done
IFS=$I

if [ $EXIT = OK ]; then
#######################
echo "ok"

Code: Select all

if [ "$ARTIST1" = "" ]; then
#echo $ARTIST1 > /usr/local/mpcpl/artist1
#ARTIST1=$(cat /usr/local/mpcpl/artist1)
	gxmessage -bg orange -center -title "mpcpl" "
	ERROR: No Artist was specified.
	" \
	-buttons 'OK:1, QUIT:2'	
fi

fi

case $? in
2) killall mpc-pl
;;

esac
echo "ARTIST1=$ARTIST1"

User avatar
Trapster
Posts: 185
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 57 times

Re: gtkdialog

Post by Trapster »

FYI.....
Here's the full functioning script.

Code: Select all

#!/bin/bash

#################################################
########Playlists can also be created with
########  mpc ls | grep <artist>
########  mpc add
########  mpc save
#################################################
GTKDIALOG=gtkdialog
export mpcpl='
<window title="mpcpl - Playlist Generator">
<vbox>
<hbox>
<text><label>MPC Playlist Generator.  Enter an Artist Name.</label></text>
</hbox>
 <hbox>
  <text><label>Artist:</label></text>
  <entry accept="text"><variable>ARTIST1</variable><input>cat /usr/local/mpcpl/artist1</input></entry>
 </hbox>
  <hbox>
  <button help>
   <action>`Xdialog --wrap --screencenter --left --title "mpcpl - HELP" --msgbox "mpcpl is a simple GUI for Retrieving Artists From a Music Server and Creates a Playlist. \n\n1. Enter an Artist and Follow the Prompts \n3. Click "ok"\n \n\n " 600x0`</action>
  </button>
  <text><label>_____________</label></text>
  <button ok></button>
  <text><label>_____________</label></text>
  <button cancel></button>
  </hbox>
</vbox>
</window>
'
#gtkdialog3 --program=mpcpl --center

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 --program=mpcpl --center); do
   eval $STATEMENTS
done
IFS=$I
if [ $EXIT = OK ]; then

#######################

#echo $ARTIST1 > /usr/local/mpcpl/artist1
#ARTIST1=$(cat /usr/local/mpcpl/artist1)

if [ "$ARTIST1" = "" ];then
gxmessage -bg orange -center -title "mpcpl" "
ERROR: No Artist was specified.
" \
	-buttons 'OK:1, QUIT:2'
case $? in
    2) killall mpc-pl
	;;

esac
echo "hello"
echo "$ARTIST1" > /usr/local/mpcpl/artist1
mpc-pl
exit

fi

A="$ARTIST1"
B=(  $A  )
C=""${B[@]^}""
echo $A
#gxmessage -center -bg lightblue "$C"

gxmessage -center -bg lightblue "Please wait while the songs are being retrieved" &
pid="$!"
sleep 3

#############Enter Your Path to Your Music Files###########

ARTIST2=$(ls /mnt/sdb1/ogg | grep "$C")
str=$C
NAME="${str// /_}"
echo $NAME > /tmp/Artist1

kill $pid

gxmessage -center -bg lightblue "Playlist:
$ARTIST2" \
	-buttons 'OK:1, QUIT:2'
case $? in
    2) killall mpc-pl
	;;

esac

gxmessage -center -bg lightblue "Would You Like To Create The Playlist?" \
	-buttons 'OK:1,QUIT:2'
case $? in
    2)
	killall mpc-pl
	;;
    
esac ls /mnt/sdb1/ogg | grep "$C" > /mnt/sdb1/ogg/$NAME.m3u gxmessage -center -bg lightblue "Would you like to Start MPD with this Playlist?" \ -buttons 'OK:1,QUIT:2' case $? in 2) killall mpc-pl ;; esac mpc clear mpc load $NAME mpc play fi
Post Reply

Return to “Programming”