diff --git a/examples/alarmclock.rb b/examples/alarmclock.rb new file mode 100644 index 0000000..a23511a --- /dev/null +++ b/examples/alarmclock.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby +# Simple reminders app written in Ruby and DialogBind. +# +# Copyright (C) Tim K 2018-2019. Licensed under MIT License. +# This file is a DialogBind example. + +require 'dialogbind' +require 'time' + +# Get the name of the reminder +notification_name = guigets('What should I remind you?') +if notification_name == '' || notification_name == nil then + # If nil or empty, then exit + guierror('You did not specify a valid name for a reminder. Exiting.') + exit 1 +end + +# Get the time of the reminder +time_alert = guigets('When should I remind you? (type the time in the following format: HH:MM)') +if not time_alert.to_s.include? ':' then + # If there is no :, then the time was not specified at all + guierror('You did not specify a valid time. Exiting.') + exit 2 +elsif time_alert.to_s.include? ' ' then + # If there is a space, then the time was specified in a wrong format + guierror('Please specify the date in the following format next time: HH:MM. For example: 22:30. Exiting for now.') + exit 3 +end + +# Convert our stringified time to Ruby's Time object +time_real = Time.parse(time_alert) +if time_real < Time.now then + guierror('Late night/early morning appointments are not supported.') + exit 3 +end + +# Tell the user that we will remind him about his appointment +guinotify('You will be notified about "' + notification_name + '" on ' + time_real.to_s, 'Reminder added') + +while Time.now < time_real do + sleep 0.01 +end + +guinotify('It\'s "' + notification_name + '" time.', 'Important!') +exit 0 + +exit 1 diff --git a/examples/bulkprepend.rb b/examples/bulkprepend.rb new file mode 100644 index 0000000..71d8a4f --- /dev/null +++ b/examples/bulkprepend.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby +# A Ruby program that bulk renames all files in a specific directory +# by prepending a prefix specified by the user. +# +# Copyright (C) Tim K 2018-2019. Licensed under MIT License. +# This file is a DialogBind example. + +require 'dialogbind' +require 'fileutils' + +# First get the directory where the files are stored +directory_original = guidirectoryselect('Please select a directory with the files that you want to rename.') + +# Now get the prefix that the user wants to prepend to each file +prepend_prefix = guigets('What would you like to prepend to each file in that directory?') + +# Iterate over files and rename them +files_processed = 0 + +Dir.glob(directory_original + '/*').each do |file| + path_orig = file + prepend_prefix_applied = prepend_prefix + File.basename(file) + path_final = File.dirname(path_orig) + '/' + prepend_prefix_applied + + FileUtils.mv(path_orig, path_final) + files_processed += 1 +end + +# Notify the user that we have just finished +guinotify('Processed and renamed ' + files_processed.to_s + ' files.', 'Finished!') + +# Exit +exit 0 + diff --git a/lib/dialogbind.rb b/lib/dialogbind.rb index 82de19f..51ee2c4 100644 --- a/lib/dialogbind.rb +++ b/lib/dialogbind.rb @@ -56,13 +56,13 @@ def kdialog(arg, redirect_output=false) end # @!visibility private -def macdialog(text, buttons=['OK'], type='dialog', error=false, dryrun=false) +def macdialog(text, buttons=['OK'], type='dialog', error=false, dryrun=false, notificationtitle='') text_fixed = text.gsub("!", "").gsub("'", '').gsub('"', '').gsub('$', '') cmd = "osascript -e 'tell app \"System Events\" to display " + type + ' "' + text_fixed + '"' if type != 'notification' then cmd += ' buttons ' + buttons.to_s.gsub('[', '{').gsub(']', '}') else - cmd += ' with title "' + File.basename($0) + '"' + cmd += ' with title "' + notificationtitle.gsub("!", "").gsub("'", '').gsub('"', '').gsub('$', '') + '"' end if error then cmd += ' with icon caution' @@ -195,18 +195,11 @@ def win32_vbinputbox(text) end # @!visibility private - def win32_activexopen(filters, title) - filters_str = '' - filters.each do |filter_pattern| - to_append = 'Files matching pattern ' + filter_pattern + '|' + filter_pattern - if filters_str == '' then - filters_str = to_append - else - filters_str += '|' + to_append - end - end - generated_vbs = "fso=CreateObject(\"UserAccounts.CommonDialog\")\r\nfso.Filter=\"" + filters_str + "\"\r\n" - generated_vbs += "fso.FilterIndex=" + filters.length.to_s + "\r\nif fso.showOpen then\r\nWScript.Echo fso.fileName\r\nend if" + def win32_activexopen(title) + generated_vbs = 'set ob=CreateObject("Shell.Application")' + generated_vbs += "\r\nset fldr=ob.BrowseForFolder(0, \"" + title.gsub("\r\n", "\n").gsub("\n", "").gsub('"', "") + '", &H4000, "C:")' + generated_vbs += "\r\nif not fldr is Nothing then" + generated_vbs += "\r\nWScript.Echo fldr.Self.Path\r\nend if" return win32_generatevbs(generated_vbs) end @@ -220,7 +213,7 @@ def win32_vbbrowseforfolder(title) end else # @!visibility private - def win32_activexopen(filters, title) + def win32_activexopen(title) return '' end @@ -434,7 +427,7 @@ def nativesoundplay(sound_path) if $dialogbind_dialog_backend == 'win32' then win32_activexplay(sound_path) elsif $dialogbind_dialog_backend == 'macos' then - system('afplay "' + unix_cmd_optimized_path + '" > /dev/null 2>&1') + system('afplay "' + unix_cmd_optimized_path + '" > /dev/null 2>&1 &') else if system('command -v play > /dev/null 2>&1') then system('play "' + unix_cmd_optimized_path + '" > /dev/null 2>&1') @@ -535,6 +528,8 @@ def guifileselect(filter=[], title='DialogBind') elsif $dialogbind_dialog_backend == 'zenity' then zenity({ 'title' => title, 'file-selection' => nil, '%' => zenityfilter(filter), ' > /tmp/zenity.sock 2>/dev/null' => nil }) return File.read('/tmp/zenity.sock').gsub("\n", "") + elsif $dialogbind_dialog_backend == 'win32' then + return win32_activexopen(title) else raise 'The selected backend does not support file selection dialog boxes.' return '' @@ -587,3 +582,24 @@ def guigets(text='Type something:', title='DialogBind') end return '' end + +# Shows a notification in system tray or a message box, depending on operating system notifications support. +# +# @param text [String] the text that should be displayed in a notification. +# @param title [String] an optional parameter specifying the title of the notification. +# @return [Boolean] true on success, false on fail. +def guinotify(text, title='DialogBind', sound=DialogBindSystemSounds::Success) + if $dialogbind_dialog_backend != 'win32' && $dialogbind_dialog_backend != 'kdialog' then + guisound(sound) + end + if $dialogbind_dialog_backend == 'macos' then + return macdialog(text, [], 'notification', false, false, title) + elsif $dialogbind_dialog_backend == 'kdialog' then + return kdialog({ 'title' => title, 'passivepopup' => [ text, 10 ] }) + elsif $dialogbind_dialog_backend == 'zenity' then + return zenity({ 'title' => title, 'notification' => nil, 'text' => text }) + else + return guiputs(text, title) + end + return false +end