Growl notifications

Share your scripts and packages with other Mudlet users.
User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Growl notifications

Post by Vadi »

It is in the manual actually: http://mudlet.org/asciidoc/manual.html#hasFocus

I just can't update the main one, only Heiko can.

doddzy39
Posts: 1
Joined: Mon Mar 22, 2010 6:41 am

Re: Growl notifications

Post by doddzy39 »

From what I can tell, hasFocus() was added in the last update (1.1.1).

For those of you using Snarl for windows, here is another way to handle doing notifications, using the snarl network protocal. Its a lot more long winded then just calling os.execute, but it means you don't have to put up with the the command window popping up each time you send a notification.

In order to get it to work, I had to download and place the LuaSocket library from here, and place the socket and mine folders, and the .lua files inside the lua folder, into "C:\Documents and Settings\<user>\Local Settings\Application Data\Mudlet. I also placed the socket folder inside the lua folder into "C:\Documents and Settings\<user>\.config". I'm not sure if I got the folders right..but everything seems to work. I also wasn't sure if there was already a inbuilt way to handle TCP sockets already built into Mudlet, but couldn't seem to find anything.

The following code will allow you to send notifications using Snarl:
Code: [show] | [select all] lua
-------------------------------------------------
--         Snarl notifications using SNP       --
-- ----------------------------------------------

-- Usage-  	   		       						   
-- 		First, you will want to change the Snarl.mudletIcon path to either point
--		to your mudlet installation directory, or another icon of your choice.

--    Functions:
--			Snarl.startup()  - Registers mudlet with Snarl, and starts sending notifactions
--			Snarl.shutdown() - Unregister mudlet, and stop notifactions
--			Snarl.notify(title, message, icon, timeout, class)
--				- Only the title and message options are required.  Will
--				  display a notification using Snarl if the main Mudlet window is not
--				  in focus (or if Snarl.notifyWithFocus is set to true)
						
 
require("socket")

Snarl = Snarl or {}
Snarl.connected = false

--A few options
Snarl.notifyWithFocus = false
Snarl.defaultTimeout = 10
Snarl.mudletIcon = "C:\\Documents and Settings\\<User>\\Local Settings\\Application Data\\Mudlet\\mudlet.png"


Snarl.message = function (msg)
   cecho("<dodger_blue>[<white>Snarl<dodger_blue>]:")
	cecho("<white> " .. msg .. "<white>.\n")
end

Snarl.startup = function (Appname)
	Snarl.appName = Appname or "Mudlet"
		--Register our app
	if Snarl.register() then
		--Register our default class
		Snarl.addClass("General", "General Message")
		Snarl.message("Snarl notifications started successfully")
	else
		Snarl.message("Unable to start up Snarl notifications..is Snarl running?")
	end
end

Snarl.shutdown = function ()
		Snarl.unregister()
		Snarl.message("Snarl notifications have been shutdown")
end

--Creates a connection to snarl, and returns the socket
--return nil if it fails
Snarl.getSocket = function()
	local soc = socket.tcp()
	soc:settimeout(1) -- In case something goes wrong, and snarl isn't running
	local res, error = soc:connect("127.0.0.1", 9887)
	if not res then
		Snarl.message("Error connecting to snarl - " .. error)
		return
	end

	--return the connected socket
	return soc
end


-- getReturnMsg returns nil if the return message was the expected response, otherwise,
-- it returns the error, so the calling function can handle it
Snarl.getReturnMsg = function(soc, expectedResponce)

	local retMsg = soc:receive()
	soc:close() -- We can close the socket now
	
	if retMsg ~= expectedResponce then	
		return retMsg;
	end
end


Snarl.register = function ()

	local soc = Snarl.getSocket() -- Connect to snarl
	if not soc then return false end
	
	local msg = "type=SNP#?version=1.0#?action=register#?app=" .. Snarl.appName .. "\r\n";
	
	soc:send(msg);
	local ret = Snarl.getReturnMsg(soc, "SNP/1.1/0/OK");
	
	if not ret or ret == "SNP/1.1/203/Application is already registered" then
		 return true -- all good
	else
		 Snarl.message("Error with registering: " .. ret)
		 return false
	end
end

Snarl.unregister = function ()
	
	local soc = Snarl.getSocket() -- Connect to snarl
	if not soc then return false end
	
	local msg = "type=SNP#?version=1.0#?action=unregister#?app=" .. Snarl.appName .. "\r\n";
	
	soc:send(msg);
	local ret = Snarl.getReturnMsg(soc, "SNP/1.1/0/OK")

	if not ret or ret == "SNP/1.1/202/Application is not registered" then
		return true
	else
		Snarl.message("Error with unregistering: " .. ret)
		return false
	end
end

Snarl.addClass = function (className, classTitle)
	if not className then
		Snarl.message("AddClass - Please specify a className")
		return
	end
	
	local soc = Snarl.getSocket() -- Connect to snarl
	if not soc then return false end
	
	local msg = "type=SNP#?version=1.0#?action=add_class#?app=" .. Snarl.appName .. "#?"
	msg = msg .. "class=" .. className

	if classTitle then
		msg = msg  .. "#?title=" .. classTitle
	end

	msg = msg .. "\r\n"

	soc:send(msg);
	Snarl.getReturnMsg(soc, "SNP/1.1/0/OK");
end

Snarl.notify = function(title, message, timeout, icon, class)
	if not Snarl.notifyWithFocus and hasFocus() then
		return -- We have focus, so don't send notification
	end		

	if not title or not message then
		return false
	end

	timeout = timeout or Snarl.defaultTimeout
	class = class or "General"
	icon = icon or Snarl.mudletIcon

	local soc = Snarl.getSocket() -- Connect to snarl
	if not soc then return false end

	local msg = "type=SNP#?version=1.0#?action=notification#?app=" .. Snarl.appName .. "#?class=" .. class .. "#?title=" .. title .. "#?text=" .. message .. "#?timeout=" .. timeout .. "#?icon=" .. icon .. "\r\n"

	soc:send(msg);
	Snarl.getReturnMsg(soc, "SNP/1.1/0/OK")
end

Its not exactly pretty, and it may be buggy (I've been playing with it all day, and it seems to work fine), but it does the job.

Just call Snarl.startup() at some point to enable notifications.

Snarl.notify(title, message, timeout, icon, class) - Allows you to send a notification. Only title and message are required, with the default timeout and icon set using the Snarl.defaultTimeout and Snarl.mudletIcon variables at the top of the script.

Wyd

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Growl notifications

Post by Vadi »

I think it'll be worthwhile to merge these into a single notify function for all platforms once mudlet ships with luasocket. we'd have to note in the manual that for pretty much each platform you have to install the proper programs.

guy
Posts: 26
Joined: Wed Mar 03, 2010 5:41 am
Location: Seattle

Re: Growl notifications

Post by guy »

There is also in Qt the QSystemTrayIcon (docs here) that looks like it has balloon popups with QSystemTrayIcon::showMessage(). Maybe not as fancy looking as standalone notification packages, but it is already included with Qt and cross platform (well, mac needs to install growl).

In addition, a small menu could be associated with the system tray icon so that when you click on it, it has a list of the last 5,10,whatever messages sent.

As for requiring these other notification packages, could the installer be modified to check for them and give a prompt if they're not detected?

vinc456
Posts: 3
Joined: Sun Jan 24, 2010 3:14 am
Location: Lusternia

Re: Growl notifications

Post by vinc456 »

Rakon wrote:Courtesy of Vadi, and with my simple modifications, here is one for Debian/Ubuntu systems using notify-send:

It will only send the notification if the main Mudlet window is NOT in focus.
Code: [show] | [select all] lua
function notify (...)
 if hasFocus() == false then
   os.execute(string.format("%s", [[notify-send --icon="/usr/share/app-install/icons/mudlet.png" "Mudlet" "]] .. ... .. [["]]))
 end -- if
end -- notify
On Debian Lenny notify-send is not installed by default. Using the instructions from here I installed libnotify-bin and notifications work now. Pretty cool!

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Growl notifications

Post by Phoenix »

The original growl code at the top was a bit lacking. Notable, anything with the ' character would kill it, due to limitations of the growlnotify itself (not the mudlet function). I've run a workaround for this which killed doublequotes... as such, it now will string.gsub all doublequotes into singlequotes. Also, I've added two sounds (high and low priority), and made it only notify if the window doesn't have focus for low priority. Last thing I did was add an option to make it sticky if you want, as the fourth parameter. Last thing I did was throw in an emergency priority for the alert... didn't feel like making a 5th parameter, so just tacked it in with 'priority'. Low and high folks, that's all there is.

TLDR: I rewrote the above growlNotify() to get rid of a few bugs and give more options.

Attachment is some example triggers that I threw together for Achaea for use with this.
Code: [show] | [select all] lua
function growlNotify(title, message, priority, sticky)
--sounds
--Change these sounds to what you want, and have.
        soundAlert = "/System/Library/Sounds/Indigo.aiff"
        soundNotify = "/System/Library/Sounds/Temple.aiff"

local path, icon -- Do not touch this line

--Path: This is the default path, change it if you have it somewhere else.
        path = "/usr/local/bin/growlnotify"
--Icon: This is an advanced change, try it if you want.
        icon = "-a Mudlet.app"


        assert(type(priority)=="boolean" or type(priority)=="nil",
         "Wrong type for the priority!")
        assert(type(sticky)=="boolean" or type(sticky)=="nil", 
         "Wrong type for the priority!")

        sticky = sticky and "-s" or ""
        message = string.gsub(message,[["]], "'") or ""
        priority = priority or false
        title = title or "Mudlet"
   if not priority and not hasFocus() then 
        playSoundFile(soundNotify) 
        os.execute( path .. " ".. sticky .. " " .. icon .. [[ -m " ]] ..
         message .. [[ " " ]]..title..[[ "]])
   end
   if priority then
        playSoundFile(soundAlert) 
        os.execute( path .. " ".. sticky .. " " .. icon .. [[ -p 2 -m " ]] ..
         message .. [[ " " ]]..title..[[ "]])
   end
end
Attachments
GrowlTriggers.xml
Triggers for use in Achaea with this version of growlNotify.
(9.46 KiB) Downloaded 451 times

Daagar
Posts: 89
Joined: Fri Feb 19, 2010 2:42 am

Re: Growl notifications

Post by Daagar »

Just wanted to necro this thread to say that Wyd's snarl script still works with the latest Snarl version just fine. I updated it to SNP 3.0 "just because" but it functions pretty much out of the box (A few minor changes will be necessary to the return codes if you continue to use SNP 1.0). This is just an excellent feature that I hadn't considered making use of before, and wanted to give it a little boost.

Post Reply