You are not logged in.

#1 2013-01-12 14:38:16

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,838
Website

dmenu menu with spaces?

I am trying to create a dmenu, that runs specific commands on each entry. but i fail to use entries with spaces.
This is how i am trying to archive it, without success, i might add:

#!/bin/bash
prog="
test
test with space
"

cmd=$(dmenu <<< "$prog")

case ${cmd%% *} in

    test)   notify-send "foo" ;;
    test with spaces)  notify-send "bar"
;;
   *)      exec "'${cmd}'"  ;;
esac

what would be a better solution?


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2013-01-12 15:26:38

schalox
Member
From: Finland
Registered: 2011-05-10
Posts: 21

Re: dmenu menu with spaces?

I used an associative array in my power-script:

#!/bin/bash

declare -A actions=(
    ['poweroff']='systemctl poweroff'
    ['hibernate']='systemctl hibernate'
    ['suspend']='systemctl suspend')

chosen=$(printf "%s\n" "${!actions[@]}" | dmenu) || exit 1

${actions[$chosen]}

Offline

#3 2013-01-12 15:49:42

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,838
Website

Re: dmenu menu with spaces?

this is nice.. but is it possible to define variables beforehand that can be used in the actual command?

TIME=3000
FILEMASK=%Y-%m-%d-@%H-%M-%S-scrot.png
function notify() {
  notify-send -u low --hint=int:transient:1 -t ${1} "Scrot" "${2}"
}

declare -A actions=(
    ['1. Quick Fullscreen']='scrot -d 1 "$FILEMASK" && notify ${TIME} "Screenshot saved"'

doesnt work


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#4 2013-01-12 16:05:48

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,502
Website

Re: dmenu menu with spaces?

#!/bin/bash

list="test\ntest with space"

cmd=$(echo -e $list | dmenu)
echo $cmd

Last edited by Trilby (2013-01-12 16:14:27)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#5 2013-01-12 16:16:24

schalox
Member
From: Finland
Registered: 2011-05-10
Posts: 21

Re: dmenu menu with spaces?

What if you use double quotes when assigning array's items?

declare -A actions=(
    ['1. Quick Fullscreen']="scrot -d 1 "$FILEMASK" && notify ${TIME} "Screenshot saved""

Offline

#6 2013-01-12 16:32:42

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,838
Website

Re: dmenu menu with spaces?

schalox wrote:

What if you use double quotes when assigning array's items?

declare -A actions=(
    ['1. Quick Fullscreen']="scrot -d 1 "$FILEMASK" && notify ${TIME} "Screenshot saved""

since declare opens a subshell this wont work either. too bad, it looks much cleaner smile

anyway, i was able to solve my good old case - here the whole script:

#!/bin/bash
# teiler - A script to share (german word: teilen) screenshots/casts for tiling WMs - Pun intended
# (c) Rasmus Steinke <rasi at xssn dot at> 
# Additional Ideas, testing and some code by Zeltak <zeltak at gmail dot com>
#
# Requirements:
# xclip, fb-client, pinta, dzen2, dmenu, ffmpeg, scrot, bc, some notification daemon


#changelog
#v0.8
#Put notify-send into its own function, Made delay being calculated from counter value.
#v0.7
#Massive Code Cleanup
#v0.6
#Countdown script stolen from Google and integrated.
#v0.5
#added clip uploading
#v0.4
#added screencast function
#v0.3
#added 1.notifications 2.unique names for each type (for quick launch) 3.better photo editor (pinta) 4.dmenu title


############### Configuration options ##############################


IMG_PATH=/home/carnager/Pictures/Screenshots/
FILEMASK=%Y-%m-%d-@%H-%M-%S-scrot.png
UL=fb
EDIT=pinta
RES=1920x1080   #set your screen resolution
TIME=3000          #How long will notifications be visible? (in miliseconds)
COUNTER1=3      #Seconds to count for delay (Use $COUNTD variable)
COUNTER2=6      #Seconds to count for delay (Use $COUNTD2 variable)


#DMENU/DZEN2 CONFIGURATION
FONT="Envy Code R-9"
NF="#7c7c72"
NB="#1a1a1a"
SF="#FFFFFF"
SB="#33B5E5"
DZEN2_W=100     #Width of dzen2
DZEN2_X=850     #X Padding

################## Configuration End ##############################


# Needed for the countdown
# Based on some script by  Marco Fontani - MFONTANI at cpan dot org
set -bm
COLOR='#7c7c72'
function countdown () {
    seq 1 ${1:-10} | tac | \
        perl -ne'BEGIN{$|++;$msg=shift}$m=int($_/60);$s=int($_-$m*60);printf("$m:%02d -- $msg\n",$s);sleep 1;' \
        "${2:-ready...}"
}


# Outsource the notfiy-send to its own function to clean up the mess below a bit ;)
function notify() {
  notify-send -u low --hint=int:transient:1 -t ${1} "Scrot" "${2}"
}

####Some Variables to clean up the code a bit
COUNTD="countdown $COUNTER1 GO | dzen2 -fn \"$FONT\" -fg \"$NF\" -ta c -w \"$DZEN2_W\" -bg \"$NB\" -x \"$DZEN2_X\""
COUNTD2="countdown $COUNTER2 GO | dzen2 -fn \"$FONT\" -fg \"$NF\" -ta c -w \"$DZEN2_W\" -bg \"$NB\" -x \"$DZEN2_X\""
XCLIP="(xclip -o;echo) | xclip -selection clipboard"

cd $IMG_PATH

#!/bin/bash

MENU="1. Quick Fullscreen
2. Select Area
3. Fullscreen Shot and Edit
4. Selection and Edit
f. Fullscreen and Upload
d. Delayed Fullscreen and Upload
e. Fullscreen and Edit and Upload
s. Selection and Upload
p. Selection and Edit and Upload
x. Upload Clipboard Content
c. Start Recording a Videocast
k. Stop Recording a Videocast
"

CHOICE="$(echo "$MENU" | dmenu -o 0.8 -l 20 -fn 'Envy Code R-10' -nf '#999' -nb '#000' -sf '#eee' -sb '#0077bb' -p "Make Your Wish:")"

 

  case "$CHOICE" in
    "1. Quick Fullscreen")
      scrot -d 1 "$FILEMASK" && notify ${TIME} "Screenshot saved"
  ;;

    "2. Selection")
      eval $COUNTD & scrot -d $(echo $COUNTER1+1 | bc) "$FILEMASK" && notify ${TIME} "Screenshot saved"
  ;;
    "3. Fullscreen Shot and Edit")
      scrot -d 1 "$FILEMASK" -e "$EDIT \$f" && notify ${TIME} "Screenshot edited and saved"
  ;;
    "4. Selection and Edit")
      scrot -s "$FILEMASK" -e "$EDIT \$f" && notify ${TIME} "Screenshot edited and saved"
  ;;
    "f. Fullscreen and Upload")
      scrot -d 1 "$FILEMASK" -e "$UL \$f" && eval $XCLIP && notify ${TIME} "Screenshot uploaded"
  ;;
    "d. Delayed Fullscreen and Upload")
      eval $COUNTD & scrot -d $(echo $COUNTER1+1 | bc) "$FILEMASK" -e "$UL \$f && rm -f \$f" && eval $XCLIP && notify ${TIME} "Screenshot uploaded"
  ;;
    "e. Fullscreen and Edit and Upload")
      scrot -d 1 "$FILEMASK" -e "$EDIT \$f && $UL \$f && rm -f \$f" && eval $XCLIP && notify ${TIME} "Screenshot uploaded"
  ;;
    "s. Selection and Upload")
      scrot -s "$FILEMASK" -e "$UL \$f && rm -f \$f" && eval $XCLIP && notify ${TIME} "Screenshot uploaded"
  ;;
    "p. Selection and Edit and Upload")
      scrot -s "$FILEMASK" -e "$EDIT \$f && $UL \$f && rm -f \$f" && eval $XCLIP && notify ${TIME} "Screenshot uploaded"
  ;;
    "x. Upload Clipboard Content")
      eval $COUNTD2 & sleep $(echo $COUNTER2+1 | bc) && xclip -o | fb && eval $XCLIP && notify ${TIME} "Clipboard uploaded"
  ;;
    "c. Start Recording a Videocast")
      ffmpeg -r 25 -f x11grab -s $RES -i $DISPLAY+0,0 -vcodec libx264 temp_cast.mkv && notify ${TIME} "Screencast started"
  ;;
    "k. Stop Recording a Videocast")
      kill $(pgrep -f x11grab) && sleep 3 && $UL temp_cast.mkv && rm -f temp_cast.mkv && eval $XCLIP && notify ${TIME} "Screencast uploaded"
  ;;
  *)
    notify-send "No Selection Made"
  ;;
esac

and here is how it looks:
taDFvcw


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB