Category Archives: Computers

Mac OS X and Windows

PowerShell String.Split() Off-by-Method-Overload Error

This seemed to me an error and I and was on the point of raising it as a bug on the Powershell github repo:

PS> "\this".Split( [char]'\', [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 2

Presumably it is because [StringSplitOptions]::RemoveEmptyEntries is coerced to a [char] and so the line is parsed as:

PS> "\this".Split( ([char]'\', [StringSplitOptions]::RemoveEmptyEntries) ).Length

Instead of as

PS> \this".Split( (,[char]'\'), [StringSplitOptions]::RemoveEmptyEntries).Length

If the first parameter is a string not a character then it works as expected:

PS> "\this".Split( '\', [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 1

But the really unfortunate case is :

PS> "\this".Split( [System.IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 2

which results in

PS> "\this".Split( [System.IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries).[0]
# >> $null
# instead of 
# >> "this"

It turns out that it’s fixed in Powershell 6 Beta; or to be more precise, it doesn’t happen in PowerShell 6. What changed is that the underlying .Net framework has added new overloads to String.Split():

string[] Split(char separator, System.StringSplitOptions options)                                                                                    
string[] Split(char separator, int count, System.StringSplitOptions options)                                                                         
string[] Split(string separator, System.StringSplitOptions options)                                                                                  
string[] Split(string separator, int count, System.StringSplitOptions options)                                                                       

Whereas PowerShell 5 only has these overloads available:

string[] Split(Params char[] separator)                                                                                                              
string[] Split(char[] separator, int count)                                                                                                          
string[] Split(char[] separator, System.StringSplitOptions options)                                                                                  
string[] Split(char[] separator, int count, System.StringSplitOptions options)                                                                       
string[] Split(string[] separator, System.StringSplitOptions options)                                                                                
string[] Split(string[] separator, int count, System.StringSplitOptions options)                                                                     

And so the best-match overload that PowerShell 6 chooses is different to PowerShell 5’s best match.

Using Windows keystrokes on a Mac

Alas. I’ve been using Windows so long that now I go back to the Mac and want Ctrl-C to work. Sad, but there it is. At last I found Karabiner, which does keyboard remapping, and created a set of Karabiner rules to map the main Windows Ctrl-keystrokes to the matching ⌘-keystroke on Mac:

Karabiner Rules for main Windows Ctrl-keystrokes on MacOs

Okay, so Ctrl-W isn’t strictly a windows keystroke. But who can work without Ctrl-W?

Karabiner Complex Rules Snippet

 
{
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {  /* ... etc ... */ },
"README": "********************************************************************************************************",
"README": "*" COPY JUST THE ELEMENTS OF THIS "rules" array into your profiles.complex_modifications.rules array. "*",
"README": "********************************************************************************************************",
              
                "rules": [
                    {
                        "description": "Change Control+Z to Command+Z",
                        "manipulators": [ { "from": { "key_code": "z","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "z","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+X to Command+X",
                        "manipulators": [ { "from": { "key_code": "x","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "x","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+C to Command+C",
                        "manipulators": [ { "from": { "key_code": "c","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "c","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+V to Command+V",
                        "manipulators": [ { "from": { "key_code": "v","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "v","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+N to Command+N",
                        "manipulators": [ { "from": { "key_code": "n","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "n","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+S to Command+S",
                        "manipulators": [ { "from": { "key_code": "s","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "s","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+O to Command+O",
                        "manipulators": [ { "from": { "key_code": "o","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "o","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+Y to Command+Y",
                        "manipulators": [ { "from": { "key_code": "y","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "y","modifiers": ["command"]}],"type": "basic"}]
                    },
                    {
                        "description": "Change Control+W to Command+W",
                        "manipulators": [ { "from": { "key_code": "w","modifiers": {"mandatory": ["control"],"optional": ["any"]}},"to": [{"key_code": "w","modifiers": ["command"]}],"type": "basic"}]
                    }
                ],
"README": "******************************************************************************************************",
"README": "******************************************************************************************************",
            /* ... etc ... */
            }
        }
    ]
}

Editing the Keyboard Map

This approach didn’t work for me. I’m not sure what I missed.

chuser(1) – man page

chuser(1) - man page

Name

chuser- change the current user

Synopsis

chuser [options]

Options

  -n, --now       immediately terminate this user.

  -s, --soft      if painless termination protocols are available,
                  use them. This option is ignored on systems without
                  appropriate hardware.

  -R, --recursive terminate users recursively. This option causes
                  chuser to spawn new chuser processes repeatedly
                  until a user capable of terminating it does so.

  -v, --verbose   replace the usual output of chuser with the
                  input from the microphone.

History

chuser is intended for users who are instructed to use it by 
remote helpdesk operators. It is scheduled for depre-
cation when IT customer services are replaced by robots
with infinite patience.

Comments

chuser attempts to detach the current user from the current
machine. The result of running chuser is non-deterministic
and depends on available hardware. chuser typically fails when run 
by a user who intended to run it, but may succeed when used 
unwittingly. 

chuser expects to run on a foreground thread, so attempting
to background it or run it in a terminal controlled by screen(1)
or tmux(1) may increase the chance of success.

See also

shutdown(8) 
kill(1)
sudo rm -Rf /

iTunes Home Sharing doesn’t work with AppleTV when it should

For about a year, I’ve been stumped on iTunes Sharing not working on AppleTV when it worked fine between laptops. Until today, when I accidentally discovered that iTunes -> Preferences -> Sharing -> “Share Library on Network” —which works for computers running iTunes— is not the same things as iTunes -> File -> Home Sharing, which is what AppleTV uses.

Oh well.

http://www.imore.com/itunes-9-shared-libraries-home-sharing

‘bash\r’: No such file or directory. Or, editing unix files on a Windows machine

'bash\r': No such file or directory

What does that mean, you ask yourself? It usually means you are editing *nix script files on a windows system, and then running the files on a *nix machine. For instance in docker, or a VM. Your solution is to use a text editor that allows you to save with unix line endings. `Notepad++` calls it, very reasonably, `’EOL conversion’`. Sublime text calls it `View->Line Endings`.

A further complication if you are using git is that it keeps getting reset back to windows. You could fix this with `git config core.autocrlf false` but that may have other consequences which you don’t want to bother with.