March 14, 2009, 3:14 pm · 3 comments · Filed under: Apple, AppleScript, Dashboard, JavaScript, Widgets
Can your web browser do this?
You’ll never get rich digging a ditch, nor building Dashboard widgets.
A Kryptonite™ lock can be defeated in 11 seconds, but you still lock your bike, right?
Gaining Twitter followers is a little like losing weight. You have to try.
Over or under? It’s the age-old question when it comes to the orientation of toilet paper rolls.
I am a web developer, recently returned to the States after 3 years in New Zealand. I’m into my family, photography and frisbee sports.
Nothing will benefit human health and increase chances for survival of life on earth as much as the evolution to a vegetarian diet.
–Albert Einstein
Apple · AppleScript · Business · Coda · CSS · Dashboard · Design · Google · InSTEDD · JavaScript · jQuery · Life · Marketing · Music · New Mexico · New Zealand · Open Source Software · Photography · PHP · Politics · Ruby on Rails · Scree · Subversion (SVN) · Twitter · Usability · Web Development · Widgets
CSS Fast Nav: Because (perception of) speed matters! · Personal Branding for Introverts · Stupid WebKit Tricks · Add an interactive legend to a MarkerManager managed Google Map · Dude. Mikeyy can’t even spell his own name. · Dashboard Widgets for Fun and Profit · Animating your iPhone web application · How-to recover from checksum mismatch errors in SVN · Why Apple can afford to charge so little for Snow Leopard · When is a global variable not a variable?
CSS Fast Nav: Because (perception of) speed matters! · When is a global variable not a variable? · Our misguided culture of cool · InSTEDD: Open Source Software that saves lives · Add an interactive legend to a MarkerManager managed Google Map · Personal Branding for Introverts · Moments of Rangitoto · Some Twitter conventions · Why Apple can afford to charge so little for Snow Leopard · Stupid WebKit Tricks
Twitshirt is a tweet on a shirt. Buy the one below or check out my most recent tweets.
If I ever meet an engineer from the IE6 team... I swear to dog I'll kick him in the nuts.
See a random Twitshirt-worthy tweet.
80/20 · 90 Seven Design · Alyson Hurt · Andrew Nimick · Apps & Hats · Ben Young · Brian Arnold · Brian Warren · Carl Bolter · Chris Burgess · Christine Morris · Cristina Stoian · Daniel Lyons · Daniel Schwartz · David Hedges · Hamish Campbell · Jochen Daum · John Visser · Joseph McLaughlin · Joshua Sallach · Julian Pistorius · Justine Sanderson · Kalena Jordan · Katie Graham · Kelly Green · Kevin Potis · Mark Bixby · Matt Henry · Method Arts · Morgan Pyne · Peter Michaux · Philip Tellis · Piers Harding · Rebecca Murphey · Reid Givens · Rey Bango · Rhett Anderson · Richard Paul · Rob Pongsajapan · Robin Taylor · Ryan Park · Shaun Lee · Simon Young · Su Yin Khoo · Toni Barrett · Vaughan Rowsell · Vincent Thomé · Voom Studio
My bias is for references over “cookbooks.” I want to know all of my options, not just one way to do something. Show me the why as well as the how and I am happy.
JavaScript: The Good Parts · Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries · JavaScript: The Definitive Guide · Designing with Web Standards · CSS: The Definitive Guide · Prioritizing Web Usability · The Elements of User Experience · Web ReDesign: Workflow that Works · Don't Make Me Think: A Common Sense Approach to Web Usability
I’ve hosted this website with pair Networks since 1997. They rock.
This blog is powered by…software I wrote.
Feeling generous? Knock yourself out!
Growl, if you’re not familiar, is a notification system for Mac OS X. It allows any application to notify you when it has, for example, finished uploading a file or changed the currently playing music track. Growl supports AppleScript, and that’s all it takes to know we can use it within Dashboard.
My use case was that users of my URL shortening widgets wanted to be able to exit Dashboard and be notified when the URL had been shortened and copied to the pasteboard. I didn’t think of this initially because the process is quite fast, but Hurler user Barry Briggs contacted me on Twitter and asked for it because he happens to run about a gazillion virtual machines at a time which slows his system down quite a bit. A day later, Hurler (and tr.im.it) had Growl support.
Note: the code I share below isn’t exactly what you’ll find if you explore the current versions of my widgets. I have generalized the code to make it more immediately useful to other widget authors. It’s all tested, though, and this code will go out in their next releases.
Here’s an overview of what I’m about to show you.
First, the JavaScript.
widget.system('growl-enabled.sh', function (obj) {
var cmd;
if (+obj.outputString > 0) {
cmd = '/usr/bin/osascript growl-notify.scpt ' +
'"URL shortened" "Hurler Widget" "Dashboard" ' +
'"http://hurl.ws/1bgq" "The URL has been shortened."';
widget.system(cmd, function (obj) {});
}
});
This script makes a widget.system call, running the growl-enabled.sh shell script. When the shell script returns, it’s value is passed into the anonymous callback function.
The shell script is quite simple. It invokes the command-line utility osascript and runs a simple AppleScript asking for a count of the number of processes named “GrowlHelperApp”. If Growl is enabled, the script returns a 1. Otherwise, the script returns a 0.
#!/bin/sh osascript<<END tell application "System Events" return count of (every process whose name is "GrowlHelperApp") end tell END
If Growl is enabled, the JavaScript goes on to compose the command that will make the Growl notification happen. The command consists of calling osascript and passing it the following 6 parameters, in order:
growl-notify.scpt"URL shortened""Hurler Widget""Dashboard""http://hurl.ws/1bgq""The URL has been shortened."osascript runs growl-notify.scpt and passes the other parameters to it. The AppleScript to make the notification request is as follows:
on run argv
tell application "GrowlHelperApp"
set the allNotificationsList to {item 1 of argv}
set the enabledNotificationsList to {item 1 of argv}
register as application ¬
item 2 of argv all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application item 3 of argv
notify with name ¬
item 1 of argv title item 4 of argv ¬
description ¬
item 5 of argv application name item 2 of argv
end tell
end run
I won’t explain this in too much detail as AppleScript is fairly self-explanatory and this script is largely lifted from the Growl documentation, where it is explained in detail. I will point out that on run argv sets up an array of the arguments passed to the script, which are then accessed by asking for item 1 of argv ("URL shortened"), item 2 of argv ("Hurler Widget"), and so on.
Download growl-notify.scpt
That’s it. I hope this is helpful. Post any questions or suggestions in the comments. Happy Growling!
Short URL to this article:
Tweet this article!
3 comments
Thanks!
Great job! I will make sure to come back and read more about php as I have word press and always looking for great coding to customize default templates. Learn from mistakes all the time,is the best everyone can do :-)
Keep in touch
Comments close automatically after 90 days.
Still have something to say? Drop me a line!
Awesome post Andrew! I’m looking forward to putting this into my is.gd widget! Thanks for sharing!
★ Posted by: Joseph McLaughlin · March 14, 2009, 1:41 pm