Tuesday, April 11, 2017

Super Awesome Debugging Technique with Comments

Here is a trick I learned many many years ago and it remains one of the best tools in my debugging toolbox for all programming languages. This example just happens to be in C++.


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
//*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}

now remove the first slash in the front of the first comment and you get:


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
/*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}


This is such a handy debugging technique.

Thursday, April 6, 2017

Programmatically Creating a Window on macOS

Programmatically creating windows without using a NIB file can be tricky on the Mac. Especially if you're developing in C or C++ instead of Objective-C. Here's one of the issue most common that may come up.

Assuming you got the window creation code right, you may be thinking everything is going to work, but you get this error:

Oct 30 17:34:39  myapp[48617] : kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Oct 30 17:34:39 myapp[48617] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.



This means your NSApplication is not being initialized. In other words, the Objective-C side of your system needs to be properly initialized. Adding the following line may help:

[NSApplication sharedApplication];

Most likely you aren't trying to do this so it is never an issue, but for those few souls out there this might just save you some serious time.

Thursday, March 30, 2017

500 Posts!

I'm closing in on 500 posts over the last 10 years, I know it isn't much but considering I don't actually post that much it's rather remarkable. I've also had well over 1 million views! So stay tuned because if you like to read my blog, I have some plans will bring even more of my ramblings to your screen.

Wednesday, March 29, 2017

More Ergonomics

It turns out I write quite a bit about ergonomics, and those are some of my most popular posts which is surprising but not too surprising since that is one thing that affects everyone.

Desk Ergonomics - Every Diagram on the internet is wrong!

Make the Apple Magic Trackpad Ergonomic

Ergonomics of Using a Cell Phone

Ergonomics

All these issues are still valid, in fact maybe even more so. Sit-stand desks are more popular than ever, but they still aren't good enough. Companies like Fitbit and Apple have pushed wearable devices to help but relatively few people wear them and I'm not sure how much they really help. I personally don't wear a device like a Fitbit or Apple Watch. If this is an interesting subject to you as a reader of this blog, write a comment or like this post so I know to write more. I have a lot more information and access to people who know even more and I believe sharing information is good.

Tuesday, March 21, 2017

C++: Convert Unicode String to ANSI String

I've seen so many examples and questions of how to convert between unicode string and ANSI string using C++. So here is by far the easiest way to do this conversion that I've implemented:

template <typename Facet>
struct TFacet : Facet
{
    using Facet::Facet;
};

wstring stringtowstring(string value)
{
    std::wstring_convert<TFacet<std::codecvt<wchar_t, char, std::mbstate_t>>> conv;
    return conv.from_bytes(value);
}

string wstringtostring(wstring value)
{
    std::wstring_convert<TFacet<std::codecvt<wchar_t, char, std::mbstate_t>>> conv;
    return conv.to_bytes(value);

}

Wednesday, March 1, 2017

Adding a Private Key to macOS Keychain

On macOS, the native SSH client can use the built-in keychain directly which is awesome because you don't have to type in the password every time. To add your private key to the keychain simply use the command:

ssh-add -K /path/of/private/key

For example if your private key's filename is ~/.ssh/id_rsa, you would use the command:

ssh-add -K ~/.ssh/id_rsa

You will then be prompted to enter your password. From now on it's all automatic. Viola!

Update: There appears to be an issue in macOS Sierra where the password is only stored for the currently session. You can follow a thread here. I'll add more information as I run across it.

Monday, January 30, 2017

Make Bootable macOS Sierra USB Install Drive

1. Format drive with the defaults using Disk Utility and name it "Untitled".
2. Download macOS Installer App from Mac App Store (Note: If you use this installer it will not be usable again).
3. Open Terminal and run:

sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ macOS\ Sierra.app --nointeraction

4. Go do the computer you want to install Sierra on.
5. Plug in the USB drive.
6. Turn the machine on and hold down the Option (ALT) key until you get a list of drives.
7. Choose the installer, and follow the directions.

Viola!

Friday, January 13, 2017

Blurred Faces in Final Cut Pro X

This is by far the best tutorial on blurred faces or censoring I've come across for Final Cut Pro X. The only thing I'd add is I suggest starting from the beginning, add the keyframe for the position and scale, then use the left and right arrow keys to jog and move the shape to the new position for each frame.


Monday, January 9, 2017

Spam Email Tips

Spammers like to validate that you read your email. They get a lot of satisfaction from this. Also they sell the data. Reading email isn't inherently bad like it was in the past, but there are still some pitfalls such as executing Javascript or loading remote images. Gmail strips Javascript so your'e good there. But they do load remote images by default. From Gmail help:

Choose to show or not show images

  1. Open Gmail.
  2. In the top right, click Settings Settings.
  3. Click Settings.
  4. Scroll down to the "Images" section.
  5. Select Always display external images or Ask before displaying external images. If you chose the "Ask before displaying external images" setting, messages that have images will have a "Display images below" option.
  6. At the bottom of the page, click Save Changes.

Thursday, December 15, 2016

Reduce Hard Drive Size When Running Windows 10 in a VM


If you are running Windows 10 in a VM chance are you could use every bit of hard drive space available. Get a few gigs back by disabling hibernation, which you probably aren't using if you're running in a VM. Here's a link to the Microsoft support article here but the directions are copied below just to make things easier.

  1. Click Start, and then type cmd in the Start Search box.
  2. In the search results list, right-click Command Prompt, and then click Run as Administrator.
  3. When you are prompted by User Account Control, click Continue.
  4. At the command prompt, type powercfg.exe /hibernate off, and then press Enter.
  5. Type exit, and then press Enter to close the Command Prompt window.

Tuesday, December 6, 2016

Bash Script Arugments

I write a lot of bash scripts. It's really out of laziness. Here is a script that allows parsing of specific arguments and passthrough of all other arguments. There are obviously many ways to do this but this is one of the ways that I've found to work best.

# Here is a boolean argument
ARG_NAME_1="-arg1"
ARG_VALUE_1=false

# Here is an argument with a value
ARG_NAME_2="-debug:"
ARG_VALUE_2=""

# Argument parsing.
ARGS=()
for i in "$@"; do
  if [[ "$i" == ${ARG_NAME_1} ]]; then
    ARG_VALUE_1=true
  if [[ "$i" == ${ARG_NAME_2}* ]]; then
        ARG_VALUE_2 =${i:${#ARG_NAME_2}}
  else
    ARGS+=("\"$i\"")
  fi
done

eval AppToLaunch $(IFS=$' '; echo "${ARGS[*]}")


Thursday, December 1, 2016

CAD for 3D Printing

I've been doing a lot of 3D printing with a Lulzbot Taz 5 lately and I've been working with lots of different CAD programs. I purchased ViaCAD and it's a pretty good program but it crashes once in a while and when it crashes, I loose the entire file, not just my work but the entire file! So I've been looking for an alternative. I run a Mac so I'd prefer to use something native to the platform rather than run a Windows VM. Here's a short list of the paid programs:

  • SolidWorks
  • Inventor
  • Rhino
  • Fusion360
  • Moment of Inspiration
  • SketchUp
  • Metasequoia
SolidWorks is by far the industry standard. If you pickup any product most likely it's been designed with SolidWorks. But it's expensive. Very expensive. Inventor is Auto Desk's version of solid works with about 20% of the market. It's good, but also expensive. Rhino is pretty darn good but they made a huge user interface change in the last few years making it terrible in my opinion. It isn't exactly inexpensive either. Fusion360 is a much less expensive cloud version of Inventor with less features and a monthly fee. Moment of Inspiration is $300. I actually rather like it but it is limited. On Mac it uses Wine which is less than ideal for a $300 piece of software in my opinion. SketchUp is both free and paid, but if you want to generate STL files you need the paid version. SketchUp is really good at architectural drawings but not good to non-geometric stuff. In order to get good prints from SketchUp you have to create your model really big and then scale it down. Even doing this you'll get funny odd jagged edges.

Now for the free stuff:
  • FreeCAD
  • LibreCAD
  • OpenSCAD
  • 123 Design
  • Antimony
  • TinkerCAD
Antimony has an interesting user interface. I'll keep my eye on it but it isn't usable. TinkerCAD is good for kids. LibreCAD and FreeCAD are powerful but extremely difficult to work with in my opinion. As it is I tend to build things in either 123 Design or OpenSCAD. I'll post more about 123 Design or OpenSCAD later.