Tahir Hassan's Blog

My Technical Notes

Friday, 13 April 2018

LibreOffice Draw: Changing default shape color

By default, shapes have a blue fill. To change this default:

  • Draw a shape (e.g. a rectangle)
  • Right click it and select "Edit Style..."
  • On the "Area" tab, change the color.

Because you are changing the style and not an individual shape's color, this affects not just the shape you drew, but all subsequent shapes.

Wednesday, 14 March 2018

PowerShell Helper Function: Waiting for tea to brew

Making tea may be a simple skill, but what drastically alters its taste is how long you leave the tea bag in for. Too long and it tastes too bitter; too little and it tastes too weak.

Fortunately, the PowerShell function below can help. By default it waits for two minutes, but you can specify `Minutes` and `Seconds`, according to your taste.


Function Wait-TeaBrew {
    [CmdletBinding()]

    param(
        [float][ValidateRange(0,[float]::MaxValue)]$Minutes=0,
        [int][ValidateRange(0,[int]::MaxValue)]$Seconds=0
    )
    
    $userSpecified = ($Minutes * 60) + $Seconds;
    $secondsToWait = if ($userSpecified -gt 0) { $userSpecified } else { 2*60; };
    
    $beforeTime = [DateTime]::Now;
    
    $getElapsedSeconds = { ([DateTime]::Now - $beforeTime).TotalSeconds };
        
    do {
        $elapsed = &$getElapsedSeconds;
        $elapsedPercentage = ($elapsed / $secondsToWait) * 100;
        
        Write-Progress -Activity "Tea brewing in progress..." -Status "$('{0:F1}' -f $elapsedPercentage)% complete:" -PercentComplete $elapsedPercentage; 
        Start-Sleep -Milliseconds 100;
    } until ((&$getElapsedSeconds) -ge $secondsToWait);
      
    Write-Host "Tea has successfully brewed" -ForegroundColor Green;
    
    if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
        $afterTime = [DateTime]::Now;
        Write-Verbose "Attempted seconds: $secondsToWait";
        Write-Verbose "Actual seconds: $(($afterTime-$beforeTime).TotalSeconds)";    
    }
}

Tuesday, 13 March 2018

Generating a SQL Script from Entity Framework

First you must enable migrations:


Enable-Migrations

Then you get it to generate the SQL (which will be in a new tab):


Update-Database -Script -SourceMigration: $InitialDatabase 

You can thereafter delete the `Migrations` folder.

Monday, 12 March 2018

Connection String to an MDF file stored in App_Data

LocalDb is installed as part of Visual Studio 2012 and later. It is sometimes useful to create the database within the `App_Data` folder, for example, you could use it to include a sample database for a project you added to GitHub. Expecting a person who downloads your sample to then set up a SQL server database is sometimes too much to ask for.

First you need to find out the name of your local database instance by doing:


sqllocaldb i

I have two names listed `MSSQLLocalDB` which is what is created when Visual Studio is installed and `ProjectsV13` (which I have never used, and did not create). I would use the name `MSSQLLocalDB` because I know it was created during VS's install process.

To create an MDF file, you can right-click the `App_Data` folder, "New" and then click on the "Data" option and select "SQL Server Database" (the extension will be .mdf).

An example connection string would be:


Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=ContosoUniversity;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\ContosoUniversity.mdf

Notice the `AttachDBFilename=|DataDirectory|\ContosoUniversity.mdf` at the end of the connection string. That specifies the location of the MDF file.

Monday, 5 March 2018

NodeJS: JSON to CSV

First install the `csv` package:


npm install csv
The following code is reading from `C:\...\data.json` and writing to `C:\...\data.csv`, with the columns provided, and it also writes out the headers.

// load the data
var data = require("C:\\Temp\\data.json");
// load the stringify function
const stringify = require('csv-stringify/lib/sync');
// turn `data` into a csv string
var content = stringify(data, { columns: ['forename', 'surname', 'dob', 'title', 'grade'], header: true });

// write `content` out to a file.
var fs = require('fs');
fs.writeFile("C:\\Temp\\data.csv", content);          

Monday, 19 February 2018

Downloading to MP3 using `youtube-dl`

Using the command-line utility `youtube-dl`, do:


youtube-dl --extract-audio --audio-format mp3 <video URL>

In order to make this work, download `ffprobe.exe` and add it to the `PATH` environment variable.