Replace tabs by space on git history

So, I prefer spaces, and when I press tab, it should instead write 4 spaces. I forgot to set this option on Sublime Text and did some commits with tabs. The following git command will rewrite the repo history and replace tabs by 4 spaces.

git filter-branch --tree-filter 'git diff-tree --name-only --diff-filter=AM -r --no-commit-id $GIT_COMMIT | find . -exec perl -pi -e "s/\t/    /g" {} \;' HEAD

Download file using WinJS.xhr

Snippet to download a file using WinJS.xhr and save it. The file variable below is a StorageFile, result of pickSaveFileAsync.

Note the usage of responseType: “blob” as an option to xhr, and how it is copied to the file stream with msDetachStream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WinJS.xhr({
    responseType: "blob",
    type: "GET",
    url: "http://example.com/",
}).then(function (response) {
    var fileContents = response.response;
 
    return file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
        return Windows.Storage.Streams.RandomAccessStream.copyAsync(fileContents.msDetachStream(), stream).then(function () {
            return stream.flushAsync().then(function () {
                stream.close();
                fileContents.msClose();
            });
        });
    });
});

I tried to use replaceWithStreamedFileFromUriAsync and writeBufferAsync, but never managed to make it work. If you do, leave a comment!

Powershell script to snap an app in Windows 8

You can not, from inside a Windows 8 application, snap it.

That doesn’t mean there is no other way :)

At work, we have to test our app in a snapped state, so we need a way to programmatically make it happen. We discovered that there are two keyboard shortcuts for this action. Windows Key + . (the period key) snaps to the right edge, and Windows Key + Shift + . snaps to the left edge.

So all we have to do is send these keys to the operating system. Sending the Windows Key is tricky, and not supported using only [System.Windows.Forms.SendKeys]::SendWait.

So here’s an alternative:

Download InputSimulator.dll

Run the following commands on a Powershell prompt:

1
2
[Reflection.Assembly]::LoadFile("C:\path\to\InputSimulator.dll")
[WindowsInput.InputSimulator]::SimulateModifiedKeyStroke('LWIN', 'OEM_PERIOD')

And your app will snap to the right edge. I am still researching how to snap to the left edge, it’s a more complicated call to SimulateModifiedKeyStroke, but will keep researching.

Javascript function to find background task by name on Windows 8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FindTask: function (taskName) {
    var background = Windows.ApplicationModel.Background;
    var iter = background.BackgroundTaskRegistration.allTasks.first();
    var hascur = iter.hasCurrent;
 
    while (hascur) {
        var cur = iter.current.value;
 
        if (cur.name === taskName) {
            return cur;
        }
 
        hascur = iter.moveNext();
    }
 
    return null;
}