This forum has been archived. All content is frozen. Please use KDE Discuss instead.

I need help with adding notifications to a script.

Tags: None
(comma "," separated)
User avatar
oracle2b
Registered Member
Posts
78
Karma
0
OS
I have a Service menu entry that executes a script . The script creates a directory based on a imgur album url and download the images to it. I did not create the script. The servicemenu entry basically makes it easy for me to right-click and execute the script in the current directory with an imgur url in klipper.

I like the notification dialog that appears in the tray icon which shows a download/copy progress with the number of files, download speed and when it's complete. I'd like to add that to the script but I don't know how. I tried "kiolient download" to replace "curl -so" in the download portion of the code and it worked to an extent. I got the file file dialog for each and every image in the imgur album with the correct filenames. I just want it to download silently without user intervention. How can I make this possible?

Ideally, this is the kind of notification i'm referring to. http://i.imgur.com/9GkfE.jpg

album url to work with: http://imgur.com/a/hJr2R

albumr.sh https://github.com/nkouevda/albumr/raw/master/albumr.sh

this is the portion i changed:

Code: Select all
# Download and store the images concurrently
        verbose && echo "downloading image: http://i.imgur.com/$image"
        kioclient download "$album_hash/$filename" "http://i.imgur.com/$image"
        return_code=$?


Service menu entry
http://pastebin.com/CAP7kuny

Code: Select all
[Desktop Entry]

    Type=Service

    X-KDE-ServiceTypes=KonqPopupMenu/Plugin

    MimeType=inode/directory;

    Actions=curl.imgur.album

    X-KDE-Priority=TopLevel

    [Desktop Action curl.imgur.album]

    Name=curl.imgur.album

    Exec=cd %f; albumr.sh $(qdbus org.kde.klipper /klipper getClipboardContents)


All answers are all replies, but not all replies are answers.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Do you have "File transfers and jobs" checked in the Notifications applet settings?


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
oracle2b
Registered Member
Posts
78
Karma
0
OS
I don't see that option available. I'm using KDE 4.9.2. I right-clicked on system tray settings and looked at "entries".


All answers are all replies, but not all replies are answers.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You have probably ended up in the System Tray applet settings rather than the Notification applet settings. Can you please double check? (I was having trouble accessing it yesterday - expanding a notification and right clicking on that was more reliable - although it has been behaving today).


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
oracle2b
Registered Member
Posts
78
Karma
0
OS
Yes "File transfers and jobs is checked. how can i incorporate it into my script though?


All answers are all replies, but not all replies are answers.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Your script should automatically incorporate it - assuming it has D-Bus / X access, which if it is invoked from service menus, it should have.

For me, I tested this by running the following in Konsole, which did lead to the behaviour you desired.
Note: only large files seem to trigger the progress bar, small files get a small pause before a download complete notification is shown.
Code: Select all
kioclient download ftp://ftp.slingshot.co.nz/pub/TEST10MB.TXT


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
oracle2b
Registered Member
Posts
78
Karma
0
OS
I'm trying to bypass the save as kdialog altogether and have the file automatically save to the directory the script(via service menu) was ran from(i think like this : %f). I notification is the intended effect but the dialog is what I want to avoid.


All answers are all replies, but not all replies are answers.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You probably want "kioclient copy" instead then.
Code: Select all
kioclient copy ftp://ftp.slingshot.co.nz/pub/TEST10MB.TXT /destination/for/file/


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
oracle2b
Registered Member
Posts
78
Karma
0
OS
bcooksley wrote:You probably want "kioclient copy" instead then.
Code: Select all
kioclient copy ftp://ftp.slingshot.co.nz/pub/TEST10MB.TXT /destination/for/file/



i tried
from my ~/tmp folder and it saved the file as "%f" .. How can I save it to the current folder the command was launched from with the original filename?

How can I incorporate it into this script?

Code: Select all
Nikita Kouevda
# 2013/01/19

# Store the script name and directory
script_name="${0##*/}"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Prints usage
function usage() {
    echo "usage: $script_name [-h] [-v] [--] album ..."
}

# Prints help message
function help_message() {
    usage
    cat <<EOF
options:
    -h      Show this help message.
    -v      Enable verbose output.
EOF
}

# Returns 0 iff verbose output is enabled
function verbose() {
    [[ -n "$verbose_option" ]]
    return $?
}

# Iterate over options
while getopts "hv" option "$@"; do
    case "$option" in
        # Parsing error; print usage to stderr and exit
        \?)
            usage >&2
            exit 1
            ;;
        # Print help message and exit
        h)
            help_message
            exit 0
            ;;
        # Verbose output
        v)
            verbose_option=0
            ;;
    esac
done

# Set shell positional arguments to the remaining arguments
set -- ${@:$OPTIND}

# Print usage to stderr and exit if no positional arguments given
if [[ -z "$*" ]]; then
    usage >&2
    exit 1
fi

# Iterate over the given albums concurrently
for album in "$@"; do
    (
    # Print the album being processed
    verbose && echo "starting album: $album"

    # Determine the album hash
    if [[ -n "$(curl -sI -- "http://imgur.com/a/$album" | head -n 1 | grep 200)" ]]; then
        album_hash=${album:0:5}
    elif [[ -n "$(curl -sI -- "$album" | head -n 1 | grep 200)" ]]; then
        album_hash=$(echo "$album" | perl -ne 'm/imgur.com\/a\/([A-Za-z0-9]{5})/; print $1')
    else
        echo "error: could not read album: $album" >&2
        exit 1
    fi

    # Determine the album URL based on the hash
    album_url="http://imgur.com/a/$album_hash"

    # Print the album hash and URL
    verbose && echo "album hash: $album_hash"
    verbose && echo "album URL: $album_url"

    # Use the album hash to make the directory if necessary
    if [[ ! -e "$album_hash" ]]; then
        verbose && echo "making directory: $album_hash"
        mkdir "$album_hash"
    elif [[ ! -d "$album_hash" ]]; then
        echo "error: not a directory: $album_hash" >&2
        exit 1
    fi

    # Extract the image hashes and extensions
    images=$(curl -s "$album_url" | tr '}' '\n' | perl -ne 'print "$1$2\n" if m/"hash":"([A-Za-z0-9]{5,7})".+?"ext":"(.+?)"/')

    # Download and store the images concurrently
    for image in $images; do
        (
        # Strip any trailing characters for the local filename
        filename=$(echo $image | perl -pe 's/([A-Za-z0-9]\.[A-Za-z]{3,4}).*/$1/g')

        # Skip existing files
        if [[ -e "$album_hash/$filename" ]]; then
            verbose && echo "skipping existing file: $album_hash/$filename"
            exit 0
        fi

        # Download the image and save it, storing the exit status of curl
        verbose && echo "downloading image: http://i.imgur.com/$image"
        curl -so "$album_hash/$filename" "http://i.imgur.com/$image"
        exit_status=$?

        # Print success only if verbose output enabled
        verbose && [[ $exit_status -eq 0 ]] && echo "saved image: $album_hash/$filename"

        # Print error message and remove the file if failed
        if [[ $exit_status -ne 0 ]]; then
            echo "error: download or save failed: $image" >&2
            rm "$album_hash/$filename"
        fi
        ) &
    done

    # Wait for all images to download
    wait

    # Indicate album completion
    verbose && echo "finished album: $album"
    ) &
done

# Wait for all albums to finish, indicate completion, and exit
wait
verbose && echo "done"
exit 0


All answers are all replies, but not all replies are answers.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
If you pass "./filename.ext" then it should save it in the current directory as intended (assuming you want to name it filename.ext)
Otherwise, you can lookup the current working directory using the "pwd" command and generate a absolute path with that output.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]