How to get icon from current system theme in bash script (ubuntu)? - bash

In Ubuntu 14.10, I would like to display a notification with an icon from the current system theme. In the following code, line 3 is an example of what I mean:
1 #!/bin/bash
2 # Display a notification with an icon
3 ICON="$(somehow-get-path-to-system-theme-root-dir)/rest/of/path/to/icon.svg"
4 notify-send -i $ICON "Some notification text"
Does such a command exist, and if so, what is it?

I think you should do something like this
#!/bin/bash
# Display a notification with an icon
THEME=$(gsettings get org.gnome.desktop.interface icon-theme | tr -d "'")
ICON="/usr/share/icons/${THEME}/rest/of/path/to/icon.svg"
notify-send -i $ICON "Some notification text"

Related

Displaying an Xcode variable in a shell script in an alert fails to display the alert

I have a shell script that I run from Xcode to open the root folder in the Terminal. It works as expected when invoked by a function key in Xcode and opens a Terminal window pointing to the value of $SRCROOT as defined in Xcode and an alert with the expected message appears before that.
#! /bin/bash
osascript -e 'display dialog "message" with title "Hi"'
open -a Terminal "$SRCROOT"
Yet when I try to replace "message" to display the contents of $SRCROOT, the dialog doesn't display at all. I've tried all of the approaches listed in the solutions here: osascript using bash variable with a space
Using any of those approaches results in the alert not displaying at all.
I even tried to display the contents of $SRCROOT in a notification with various methods of escaping it but that just displays an empty notification.
Any ideas? TIA.
Save following in test.sh :
#!/usr/bin/env bash
SRCROOT=~/Developer/SwiftVC
osascript \
-e "on run(argv)" \
-e 'display dialog item 1 of argv with title "Hi"' \
-e "end" \
-- "$SRCROOT"
open -a Terminal "$SRCROOT"
and run it with :
chmod +x test.sh
./test.sh

Display live git-clone progress using dialog

I've been trying to get git-clone to provide output to a textbox in dialog. I tried something like this:
git clone github.com/CrazyWillBear/yes-replacement 2> /tmp/clone.log &
dialog --title "Cloning repo..." --textbox /tmp/clone.log 50 100
It doesn't work and usually displays nothing, writes to the terminal, or just the first line of the output. A loop doesn't work either, it creates some really funky problems.
The result I'm trying to get is this but with the entire output instead of just the first line: example
Use tee to write the output to a file, then use the tailbox feature on dialog to read it live.
git clone https://github.com/CrazyWillBear/pig --progress 2>&1 | tee -a /tmp/clone.$$ &> /dev/null &
dialog --title "Progress" --tailbox /tmp/clone.$$ 50 100

How could I pass shell script output to notification?

I would like to show result of shell script run (exiftool -k -P -overwrite_original_in_place -ImageDescription= "$1"/*.ARW) in notification. I've tried to use $1 as text in the subject, but it shows exactly $1 instead of script output.
There needs to be a Set Value of Variable action in between the script and notification actions.
Set Value of Variable accepts input from previous action and assigns it to a variable with the name of your choice.
This variable is then available for other actions. Just start typing the name of the variable in a field and it should pop-up in the auto-suggestion.
In 10.9 or later, you can use the display notification AppleScript command:
osascript -e 'on run {t}' -e 'display notification "" with title "t"' -e end title
In 10.8 you can use terminal-notifier:
terminal-notifier -title title -message ''
terminal-notifier can be installed with sudo gem install terminal-notifier.

Hide dialog output in bash scrollback (Ubuntu)

I use the following script to display a dialog in the bash shell:
#!/bin/bash
TFILE=/tmp/habitat_resp_`whoami`.$$
dialog --menu "Commander?" 20 50 10 \
1 "MySQL" \
2 "Apache" \
3 "Postfix" \
4 "Dovecot" \
5 "Owncloud" \
2> $TFILE
# get response
RESPONSE=$(cat $TFILE)
echo $RESPONSE
clear
The problem is, when I scroll up, i can still see the dialog in my scrollback. I want it like vi. I open my script and the dialog appears and if the script is over you cant see the dialog in scrollback.
How can this be achieved?
Regards
S.
Add the --keep-tite option, which tells dialog to use the "alternative screen", which doesn't participate in terminal scrollback. This produces some slightly annoying display artefacts, but possibly not as annoying as polluting your scrollback.

How do I make a Mac Terminal pop-up/alert? Applescript?

I want to be able to have my program display an alert, notice, whatever that displays my custom text. How is this done? Also, is it possible to make one with several buttons that sets a variable?
Similar to batch's:
echo msgbox""<a.vbs&a.vbs
Use osascript. For example:
osascript -e 'tell app "Finder" to display dialog "Hello World"'
Replacing “Finder” with whatever app you desire. Note if that app is backgrounded, the dialog will appear in the background too. To always show in the foreground, use “System Events” as the app:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
Read more on Mac OS X Hints.
Use this command to trigger the notification center notification from the terminal.
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
If you're using any Mac OS X version which has Notification Center, you can use the terminal-notifier gem. First install it (you may need sudo):
gem install terminal-notifier
and then simply:
terminal-notifier -message "Hello, this is my message" -title "Message Title"
See also this OS X Daily post.
Adding subtitle, title and a sound to the notification:
With AppleScript:
display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
With terminal/bash and osascript:
osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
An alert can be displayed instead of a notification
Does not take the sub-heading nor the sound tough.
With AppleScript:
display alert "Alert title" message "Your message text line here."
With terminal/bash and osascript:
osascript -e 'display alert "Alert title" message "Your message text line here."'
Add a line in bash for playing the sound after the alert line:
afplay /System/Library/Sounds/Hero.aiff
Add same line in AppleScript, letting shell script do the work:
do shell script ("afplay /System/Library/Sounds/Hero.aiff")
List of macOS built in sounds to choose from here.
Paraphrased from a handy article on terminal and applescript notifications.
This would restore focus to the previous application and exit the script if the answer was empty.
a=$(osascript -e 'try
tell app "SystemUIServer"
set answer to text returned of (display dialog "" default answer "")
end
end
activate app (path to frontmost application as text)
answer' | tr '\r' ' ')
[[ -z "$a" ]] && exit
If you told System Events to display the dialog, there would be a small delay if it wasn't running before.
For documentation about display dialog, open the dictionary of Standard Additions in AppleScript Editor or see the AppleScript Language Guide.
And my 15 cent. A one liner for the mac terminal etc just set the MIN= to whatever and a message
MIN=15 && for i in $(seq $(($MIN*60)) -1 1); do echo "$i, "; sleep 1; done; echo -e "\n\nMac Finder should show a popup" afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Look away. Rest your eyes"'
A bonus example for inspiration to combine more commands; this will put a mac put to standby sleep upon the message too :) the sudo login is needed then, a multiplication as the 60*2 for two hours goes aswell
sudo su
clear; echo "\n\nPreparing for a sleep when timers done \n"; MIN=60*2 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done; echo "\n\n Time to sleep zzZZ"; afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Time to sleep zzZZ"'; shutdown -h +1 -s
Simple Notification
osascript -e 'display notification "hello world!"'
Notification with title
osascript -e 'display notification "hello world!" with title "This is the title"'
Notify and make sound
osascript -e 'display notification "hello world!" with title "Greeting" sound name "Submarine"'
Notification with variables
osascript -e 'display notification "'"$TR_TORRENT_NAME has finished downloading!"'" with title " ✔ Transmission-daemon"'
credits: https://code-maven.com/display-notification-from-the-mac-command-line
I made a script to solve this which is here.
Installation:
brew install akashaggarwal7/tools/tsay
Usage:
sleep 5; tsay
Feel free to contribute!

Resources