steve - Dave's Blog

Search
My timeline on Mastodon

Tweet from David Risney

2016 Dec 4, 2:26
Oh just realized Trevor, Simon, and Rebus are all the same person. Awesome. Assuming @StevenOgg isn't a robot
PermalinkComments

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 stevefaulkner

2016 Feb 10, 4:57
Chrome change breaks the visual viewport http://www.quirksmode.org/blog/archives/2016/02/chrome_change_b.html … by @ppk via @powrsurg
PermalinkComments

Retweet of stevesi

2015 Oct 25, 8:38
🙏 https://twitter.com/HuzefaMotiwala/status/658495033541283840 …
PermalinkComments

Retweet of stevefaulkner

2015 Mar 3, 2:49
browser support? "no clue" "I do not understand the point of this type. Neither do browsers." http://quirksmode.org/html5/inputs/mobile.html …
PermalinkComments

A Slower Speed of Light Official Trailer — MIT Game Lab (by...

2012 Nov 13, 7:41


A Slower Speed of Light Official Trailer — MIT Game Lab (by Steven Schirra)

“A Slower Speed of Light is a first-person game in which players navigate a 3D space while picking up orbs that reduce the speed of light in increments. A custom-built, open-source relativistic graphics engine allows the speed of light in the game to approach the player’s own maximum walking speed. Visual effects of special relativity gradually become apparent to the player, increasing the challenge of gameplay. These effects, rendered in realtime to vertex accuracy, include the Doppler effect; the searchlight effect; time dilation; Lorentz transformation; and the runtime effect.

A production of the MIT Game Lab.

Play now for Mac and PC! http://gamelab.mit.edu/games/a-slower-speed-of-light/

PermalinkCommentsscience game video-game mit 3d light-speed

Funny People: Steve Heinrich, You Don’t Know Jack head writer | Interview | The Gameological Society

2012 Jul 1, 3:38

A veteran of the satirical trivia game series since its first heyday in the 1990s, Heinrich talks about Jack’s writing process, its long hiatus, and that short-lived live-action TV version.

PermalinkCommentsgame interview video-game you-dont-know-jack ydkj

Scumbag Steve's Advice for Annoying Facebook Girl

2012 May 4, 2:46

Blake Boston of the Scumbag Steve meme writes an open letter to the woman in the Annoying Facebook Girl meme.

PermalinkCommentshumor meme scumbag-steve

Self-Driving Car Test: Steve Mahan (by Google)

2012 Mar 30, 2:38


Self-Driving Car Test: Steve Mahan (by Google)

PermalinkCommentsgoogle car self-driving-car blind video

Going Paper-Free for $220 / Steve Losh

2011 May 26, 1:17PermalinkCommentshowto paperless scanner ocr technical

Never-before-seen footage of Eric Stoltz as Marty McFly in Back To The Future

2010 Oct 13, 3:11"To celebrate the 25th Anniversary of Back To The Future, we've got never-before-seen footage of original star Eric Stoltz as Marty McFly. Plus Robert Zemeckis, Steven Spielberg and Bob Gale all explaining why they replaced him with Michael J. Fox"PermalinkCommentsbacktothefuture bttf movie video eric-stoltz

Tao Effect Blog » Blog Archive » Steve Jobs’ response on Section 3.3.1

2010 Apr 11, 2:16Lots of links, info, and thoughts on Apple's change to the iPhone SDK terms of service that now state "Applications must be originally written in Objective-C, C, C++, or JavaScript..." Means no other languages or third party platforms...PermalinkCommentssteve-jobs apple sdk api tos legal law iphone ipod ipad technical

Steven Pinker on the myth of violence | Video on TED.com

2010 Feb 1, 8:39"Steven Pinker charts the decline of violence from Biblical times to the present, and argues that, though it may seem illogical and even obscene, given Iraq and Darfur, we are living in the most peaceful time in our species' existence." Working up towards Forever Peace?PermalinkCommentsvideo history violence culture ted steven-pinker game-theory

YouTube - Steve Mcqueen Intro

2009 Dec 20, 12:24PermalinkCommentshumor video cat cute macgyver

Shoulder Surfing a Malicious PDF Author - Didier Stevens

2008 Nov 13, 10:21"Ever since I read about the incremental updates feature of the PDF file format, I've been patiently waiting for a malicious PDF document with incremental updates to come my way. Thanks to Bojan, that day has finally arrived."PermalinkCommentspdf security javascript exploit malware adobe

6.09: The World According to Woz

2008 Jul 7, 5:26"888-8888. This was his new cell-phone number, and his greatest philonumerical triumph. The number proved unusable. It received more than a hundred wrong numbers a day."PermalinkCommentsapple prank wired article phone education steve-wozniak

A jerk you'll want to buy rags from. - By Seth Stevenson - Slate Magazine

2008 Jun 19, 4:21Funny quotes + Billy Mays coverage! "He appears to be saying 'I am a carnie huckster, you know it and I know it, but that's OK because this product is that good.'"PermalinkCommentstv infomercial advertising article

c a t z o o k s . c o m - Building the Swift Solo

2008 Apr 7, 9:55Warren Steven's blog on building a sailboat.|Warren StevensPermalinkCommentswarren-stevens homepage blog boat sailing

SMBlog -- 12 November 2007

2007 Nov 14, 2:56News on recent privacy related matters with social websites and Donald Kerr's redefinition of privacy. Lots of good links.PermalinkCommentsprivacy anonymity blog post article government politics donald-kerr steven-bellovin

www.steve.museum

2007 Jun 17, 11:44Social tagging of museum pieces.PermalinkCommentssteve steve.museum art tag tagging folksonomy museum social via:felix42
Older Entries Creative Commons License Some rights reserved.