parents - Dave's Blog

Search
My timeline on Mastodon

WinRT Toast from PowerShell

2016 Jun 15, 3:54

I've made a PowerShell script to show system toast notifications with WinRT and PowerShell. Along the way I learned several interesting things.

First off calling WinRT from PowerShell involves a strange syntax. If you want to use a class you write [-Class-,-Namespace-,ContentType=WindowsRuntime] first to tell PowerShell about the type. For example here I create a ToastNotification object:

[void][Windows.UI.Notifications.ToastNotification,Windows.UI.Notifications,ContentType=WindowsRuntime];
$toast = New-Object Windows.UI.Notifications.ToastNotification -ArgumentList $xml;
And here I call the static method CreateToastNotifier on the ToastNotificationManager class:
[void][Windows.UI.Notifications.ToastNotificationManager,Windows.UI.Notifications,ContentType=WindowsRuntime];
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppUserModelId);
With this I can call WinRT methods and this is enough to show a toast but to handle the click requires a little more work.

To handle the user clicking on the toast I need to listen to the Activated event on the Toast object. However Register-ObjectEvent doesn't handle WinRT events. To work around this I created a .NET event wrapper class to turn the WinRT event into a .NET event that Register-ObjectEvent can handle. This is based on Keith Hill's blog post on calling WinRT async methods in PowerShell. With the event wrapper class I can run the following to subscribe to the event:

function WrapToastEvent {
param($target, $eventName);

Add-Type -Path (Join-Path $myPath "PoshWinRT.dll")
$wrapper = new-object "PoshWinRT.EventWrapper[Windows.UI.Notifications.ToastNotification,System.Object]";
$wrapper.Register($target, $eventName);
}

[void](Register-ObjectEvent -InputObject (WrapToastEvent $toast "Activated") -EventName FireEvent -Action {
...
});

To handle the Activated event I want to put focus back on the PowerShell window that created the toast. To do this I need to call the Win32 function SetForegroundWindow. Doing so from PowerShell is surprisingly easy. First you must tell PowerShell about the function:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
}
"@
Then to call:
[PInvoke]::SetForegroundWindow((Get-Process -id $myWindowPid).MainWindowHandle);

But figuring out the HWND to give to SetForegroundWindow isn't totally straight forward. Get-Process exposes a MainWindowHandle property but if you start a cmd.exe prompt and then run PowerShell inside of that, the PowerShell process has 0 for its MainWindowHandle property. We must follow up process parents until we find one with a MainWindowHandle:

$myWindowPid = $pid;
while ($myWindowPid -gt 0 -and (Get-Process -id $myWindowPid).MainWindowHandle -eq 0) {
$myWindowPid = (gwmi Win32_Process -filter "processid = $($myWindowPid)" | select ParentProcessId).ParentProcessId;
}
PermalinkComments.net c# powershell toast winrt

Retweet of ericlaw

2015 Jul 7, 7:41
Love it: "Never give your parents a hard time about having to teach them computer stuff. They had to teach you to use a spoon."
PermalinkComments

A Dad’s Plea To Developers Of iPad Apps For Children (smashingmagazine.com)

2012 Mar 12, 7:02

Set of issues run into by children using iPad apps.  Should be generally appropriate though:

Designing apps for children is extremely hard. Not only is quality, age-appropriate content hard to create, but designing the flow and interaction of these apps is made more difficult because designers must refrain from implementing advanced gestures, which would only confuse and frustrate kids (and, by extension, their parents). Yet all apps can and should adhere to certain basics. Hopefully, the four guidelines discussed here can become fixtures of all children’s apps.

PermalinkCommentstechnical ui ipad design children programming

California Wedding Planning Trip

2010 Apr 11, 2:34

Sarah and I are just back from a successful wedding planning trip to California. We now have cake, food, officiant, makeup, and some other things. Planning weddings is tough. It was also, of course, a pleasure to see my parents who made home made pasta -- yum!

PermalinkCommentswedding parents family weekend

Every College Student Should Just Buy a Typewriter - Typewriters - Gizmodo

2009 Dec 11, 5:13"A real true history lesson: Before there were laptops, everyone had to carry entire desktop computers to class. Before there were desktops, they had to lug typewriters. Before that, everyone just tried real hard to remember stuff. Ask your grandparents!"PermalinkCommentshumor typewriter satire laptop college

Thanksgiving 2009

2009 Nov 29, 1:32

Pre Thanksgiving DinnerSarah and I had Thanksgiving dinner at our house the Sunday before. Sarah's parents and siblings came as well as my parents who came up for the a handful of days. It was our first time hosting Thanksgiving so I was a little nervous, but my parents helped us setup and get ready so of course it went well! I cheated a bit: I ordered a turkey online from Whole Foods where you can just tell them when you want to pick it up, they have it cooked and ready including garnish and you just need to warm it up. When we moved in together Sarah and I each had slightly different small dining room tables. Thankfully they're roughly the same height and width and we could put them together end to end and seat everybody with no room to spare. On actual Thanksgiving day we went over to Rachel & Anson's lovely new place for Thanksgiving and the annual game of Trivial Pursuit.

PermalinkCommentsturkey whole foods thanksgiving holiday

Parents Pre Thanksgiving

2009 Nov 29, 1:23

sequelguy posted a photo:

Parents Pre Thanksgiving

PermalinkCommentsthanksgiving family parents redmond wa

Parents, Carl, Jeannie on Roof

2009 Aug 28, 9:12

sequelguy posted a photo:

Parents, Carl, Jeannie on Roof

PermalinkCommentsseattle family friends parents jeannie wa

Parents Visited

2009 Aug 25, 12:34

My parents visited this past weekend, met Sarah's parents, saw our house, and met our bunny. On Friday we went to BluWater in Kirkland which was pretty busy and the service was slower and slightly worse than we usually find. Saturday my parents helped us with our yard quite a bit and for dinner we went to the Icon Grill with Sarah's parents. I had forgotten how much I enjoy the food at the Icon Grill - I had the very tasty meat loaf. Dinner went well and afterward we stopped at the Three Lions pub in Redmond. On all previous occasions I had tried to go in there the place was packed for a soccer game. This night however there was a man with a guitar, singing and it wasn't nearly as packed. I also found that near the bathrooms on the wall is what looks to be James Bond's jetpack.

On Sunday we went out to see Jeannie and Carl and see the renovations to Jeannie's place. We met up with them at the Fremont Market to which I hadn't been previously, and had a look around there before going back to Jeannie's to see the lovely work they'd done to her place. For dinner my parents took us out to the Melting Pot for my approaching birthday. It was fun having my parents up and I look forward to the next time they're here.

PermalinkCommentsfamily weekend

SCIENCE SCOUTS

2009 Jun 11, 3:16Humorous science merit badges. "The 'I build robots' badge (LEVEL IV): In which, technically technically, the recipient is not in the business of world domination (as dictated by membership rules), but has built a robot that is.", "The 'non-explainer' badge (LEVEL I): Where the recipient can no longer explain what they do to their parents", And other humorous badges.PermalinkCommentshumor web science math nerd badge via:boingboing

Parents at Eileen's Party

2009 Jun 1, 2:30

sequelguy posted a photo:

Parents at Eileen's Party

PermalinkCommentscalifornia family parents eileensparty

Our soon-to-be outdated beliefs

2009 May 22, 6:55"So many of our grandparents were racist, and some of our parents are homophobes. Which of our own closely held beliefs will our own children and grandchildren by appalled by?" I thought about this too but didn't come up with as good answers.PermalinkCommentsracism via:kottke

Halloween and Gas Park Weekend

2008 Nov 4, 10:14

Gas Works Park, SeattleGas Works Park, SeattleThe weekend before last Sarah and I went down to Gas Works Park in Seattle. Gas Works Park is a former Seattle Gas Light Company gasification plant now turned into a park with the machinery kept intact and found right on the shore of Lake Union. There's a large hill right next to the plant with an embedded art installation from which you get an excellent view of the park and the lake. Anyway a very cool place. Afer, we ate at Julia's of Wallingford where I stereotypically had the Santa Cruz omelet. Good food, nice place, nice neighborhood.

Trick-or-Treat at MSFT by Matt SwannThis past weekend was Halloween weekend. On Halloween at Microsoft parents bring their kids around the office buildings and collect candy from those who have candy in their office. See Matt's photo of one such hallway at Microsoft. The next day Sarah and I went to two birthday parties the second of which required costume. I went as House (from the television show House) by putting on a suit jacket and carrying a cane. Sarah wore scrubs to lend cred. to my lazy costume. Oh yeah and on Sunday Sarah bought a new car.

PermalinkCommentsgas works park halloween personal sarah

Parents of the Bride

2008 May 29, 12:26

sequelguy posted a photo:

Parents of the Bride

PermalinkCommentswedding arizona scottsdale westinkierlandresort claireandlloydwedding

California Trip

2007 Dec 23, 11:18Other Jelly FishSarah and I went down to California at the end of last month for a Thanksgiving visit with my parents. We visited the famous Monterey Bay Aquarium (fun fact: the aquarium scenes in Star Trek IV were filmed here) and saw many jelly fish. We wandered around Capitola where we visited many local shops and ate on the wharf. We shopped in Carmel and ate at the Forge in the Forest (fun fact: Clint Eastwood was the mayor of this city in the late 80s). We visited the Santa Cruz Beach Boardwalk which was very cold and closed.
Silly Party HatsMy parents took us out to dinner on the first night and the rest of the nights we ate dinner at their house. They had the Shelton's over and I got to see Chris and Alison. It was fun to talk with them and catch up since its been quite a while since I'd seen them.
Beach House LivingroomWe stayed in the lovely beach house of our family friend's the Goodwins. My parents have been helping them fix it up and decorate it and it looks great. It was quite a treat staying in a house right on the ocean. Also of note, this was the first trip on which I was old enough to rent the car and accordingly I did all of the driving. This trip was very fun and relaxing.PermalinkCommentsaquarium friends parents personal beach california vacation

Family's winner becomes a loser and loser a winner - smh.com.au

2007 Jun 13, 1:44Is this real? How could parents hate their children so much as to name them Winner and Loser?PermalinkCommentsarticle blog humor name

MoHoLiving

2007 May 13, 7:30My parents blog on their new motorhome exploits.|ParentsPermalinkCommentsmotorhome blog friend family

MoHo Living

2007 May 13, 12:16My parents and grandmother came to visit the weekend before this current weekend, starting Friday May 4th. They arrived via their new motor-home which is quite the machine. Of course its my parents motor-home so its very well decorated inside including drapes and mini-chandelier. I didn't have a memory card for my camera at the time but I'm sure my parents will put up photos on their new blog dedicated to their motor-home at some point in the future.

At any rate, they parked the motor-home in an RV park in Issaquah so that Friday night I drove over to them and we ate at the conveniently closely located Pogachas. The next day they came over and I showed them the various cool looking things my computer connected to my flat screen TV can do. This includes Vista Media Center showing my photos from recent trips and Google Earth mapping out our respective homes and my recent trips (and Paris). Additionally, we played Wii which, unsurprisingly based on anecdotal evidence from varied sources across the Internet, was a seeming hit. Mom broke records playing bowling with my dad and I, Dad did an excellent job fishing, and Grandma's slow but steady win's the race approach to cow racing worked very well.

The next day I drove them to Seattle and we walked around Pike's Place. My parents made dinner that night at my place which was very good and made my apartment actually smell like cooked food. Also, we exchanged Christmas gifts. For the past two years I've flown back to my parents' house for Christmas and ended up with gifts I couldn't take with me in both directions. Those I left at their house they drove up and I was able to give them the ones I left at my place. They started the drive back the next day. I really enjoyed seeing them here.PermalinkCommentsmotorhome family personal nontechnical
Older Entries Creative Commons License Some rights reserved.