Monday, October 24, 2016

Download applications from anywhere in OSX (Sierra, El Capitan, Yosemite, etc)

Apple in all its wisdom has decided that users are not capable of knowing what they should and shouldn't download from the Internet. So unless the app is correctly signed and sanctioned by Apple, you are likely not going to be able to download it, and you'll get an annoying message saying so.

You can change this.

If you open the Security & Privacy setting for your system, you will see the following:

Note the absence of an Anywhere option under the Allow apps downloaded from section. To fix, open Terminal and run the following command:

sudo spctl --master-disable
Re-open Security & Privacy and you should now be able to change the settings to allow any and all download you think is worthwhile:

Tuesday, October 18, 2016

Run Google Authenticator from your Macbook with VMWare Fusion

(But you can also do these steps more-or-less with VirtualBox too...)

So Google Authenticator is designed for 2FA and specifically, for mobile devices. Basically, the idea is that in order to log into something from your laptop, you need to consult a second device (two-factor!) to do so.

This is great security, but as is generally the case, security and convenience are at odds. The procedure here will get you a bit more convenience – that is you won't have to hunt down your phone or tablet to log in – but you are sacrificing a degree of security to do it.

Basically the idea is to run an Android system in emulation. Here we're going to do it using VMWare Fusion v8.

First, you're going to need to get an Android VM compatible with VMWare. I recommend one from I was using this one at the time of writing this.

Download the .vmdk file, then open VMWare Fusion. Go to File | New and select the Import an existing virtual machine option.

Browse and select the .vmdk file you downloaded (note, this might actually be an .ovf file). Follow the rest of the prompts and you should get a running Android system, with minimal fuss.

For me there was a problem where when you Command+Ctrl to ungrab your mouse from the VM that the Home key signal was sent, and the Authenticator app goes away. Not optimal when you need to see the Authenticator code to enter it. To fix I had to follow these instructions:

In the keyboard settings, if you remap the Windows key to the right command button, you can then use the left command button to release the mouse without triggering the home key response.

Now that everything is functioning right in your VM, you want to open Play Store, and install Google Authenticator. Then you simply add a new Authenticator account the usual way (typically, scanning a QR code).

There is one more bit of difficulty you may run into, that of scanning a QR code. The catch-22 here is that usually you use your phone's camera to scan a QR code that is on your laptop's screen to setup an account. The Android VM allows you to use the camera function, but it merely uses the builtin camera of the Macbook.

Obviously, you can't use that camera to scan a QR code on the screen of the laptop, heh.

Easy enough to solve, really. I used my phone. I got the QR code up on my laptop's screen:

Took a picture of it with my actual phone, then took a picture of that picture from the VM through the laptop's camera. (It's inception all the way down...)

The Authenticator account was now setup, and I can refer to it directly from the emulator:

No more hunting around for my phone.

Sunday, October 16, 2016

An html-minifier example

If you publish web pages, you really should employ a method of compressing, or "minifying" the HTML, CSS, and Javascript. The Node tool html-minifier does this job well, all from a single command.

The following example shows the minimal useful options, against a file named index.html:

html-minifier index.html --remove-comments --collapse-whitespace --minify-js --minify-css
Note that the above command will spit the output to STDOUT. So you will need to use the -o option to save the output to a file.

In fact most of my deploy scripts use some variation of the below commands, where I move the target file to a temporary file, compress, deploy, then move the intelligible file back in place so I can continue editing it:

mv index.html index.temp.html
html-minifier index.temp.html --remove-comments --collapse-whitespace --minify-js --minify-css -o index.html
# probably, deploy here, then
mv index.temp.html index.html
By doing the above, you can ensure that your deploys are always the most compressed possible, but you continue to edit the readable version, and since you should also be using revision control, your git repo remains sane.

Tuesday, October 4, 2016

Upload files from your command line to Dropbox on OSX

Note: this should also work, more-or-less, on Linux ...

Unfortunately, Dropbox doesn't allow you to upload from the command line directly using a simple username/password.

However, it has a REST API which means that it does work from the command-line, e.g. with curl, you just have to do some initial non-command-line work first.

The Dropbox-Uploader script does just this, by prompting initially to setup an "App" in the Dropbox interface, and then obtaining the token from that. You only have to do this non-command-line work once, and it saves the token. Here are the steps.

First, you should clone the repository from Github. Easiest is probably to open Terminal and do:

git clone ~/Dropbox-Uploader

(Pick whatever path you like for the last part there.)

Then, still in Terminal type:

cd ~/Dropbox-Uploader

This is what you should see (or something very close):

 This is the first time you run this script, please follow the instructions:

 1) Open the following URL in your Browser, and log in using your account:
 2) Click on "Create App", then select "Dropbox API app"
 3) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder
 4) Enter the "App Name" that you prefer (e.g. MyUploader378562511406)

 Now, click on the "Create App" button.

 When your new App is successfully created, please click on the Generate button
 under the 'Generated access token' section, then copy and paste the new access token here:

Copy the URL from step one above, and open it in your browser.

First click the Create App button:

You will be taken to the following screen. Since I want to be able to upload files to any location, I choose the Full dropbox permission. Name your app whatever you like (note, I don't think you can use the word Dropbox in the name, but anything else should work), and click Create App once more:

This is what we're finally after, generating an access token. Scroll down and click Generate access token ...

... and you should see something like this:

Copy that string and go back to Terminal where you'll enter it at the # Access token: prompt --

 # Access token: u3I1kQtf48EAAAAAAAAE9z4xYfLQ9YhC9LMlhdStWtvFZd6UmjVSleLCUlPH_Nd8

 > The access token is u3I1kQtf48EAAAAAAAAE9z4xYfLQ9YhC9LMlhdStWtvFZd6UmjVSleLCUlPH_Nd8. Looks ok? [y/N]: y
   The configuration has been saved.

Now what's happened is that Dropbox-Uploader has saved that token to a file in your home folder called .dropbox_uploader. You can see for yourself by doing:

cat ~/.dropbox_uploader

And that's it. You can now run script from the command-line without any other steps. Meaning you can script it, cron it, etc for automated backups or whatever:

./ upload /Users/me/Documents/somebackup.tar.gz /backups/somebackup.tar.gz
 > Uploading "/Users/me/Documents/somebackup.tar.gz" to "/backups/somebackup.tar.gz"... DONE

Now, you might wonder why even bother? Can't you just install Dropbox, and make scripts that copy into the ~/Dropbox folder? Well, yes, of course, but the problem with that is the general problem of sync: It's slow, makes copies of everything everywhere (especially terrible with big Dropbox stores and new systems), and generally just does the job of uploading and downloading without control and rather poorly. I say this with a clean conscience, having once been the most ardent of sync enthusiasts. In fact, my 100GB Dropbox account is now nothing more than a backup repository, and sometimes file/link sharing utility. I need it for very little else, so sync is more aggravation than it's worth, IMO.

Thursday, July 23, 2015

JavaScript function to time your code

As with any language, eventually you're going to want to know how fast a piece of code runs. In JavaScript it's no different.

Below is a simple function object that will get you basic timing:

var timer = function() {
    var start = new Date().getTime();
    this.elapsed = function() {
        var end = new Date().getTime();
        return end - start;

It's super easy to use:

var t = new timer();

setTimeout(function() { console.log(t.elapsed());}, 123);

The above very contrived example yields:

> 125
So in addition to the 123ms sleep, there was 2ms of "other" execution that occurred.

If you're using one of the *ahem* more capable browsers, you ought to take a look at

The linked page has a very good example, so I won't bother to duplicate it here. However, here are the advantages of using

Unlike other timing data available to JavaScript (for example, the timestamps returned by are not limited to one-millisecond resolution. Instead, they represent times as floating-point numbers with up to microsecond precision.

Also unlike, the values returned by always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP).

Thursday, July 2, 2015

How to find the OSX version number and other info from Terminal

If you want to know the version of OSX you're running, simply use the sw_vers command:
% sw_vers
ProductName: Mac OS X
ProductVersion: 10.10
BuildVersion: 14A388b
It gives additional information like BuildVersion as you can see above.

For a much more detailed (but quite a bit slower) description of what you're system is running, do

% system_profiler
... and be prepared to wait a while. :)

Note, system_profiler is analogous to msinfo32 on Windows.

Wednesday, July 1, 2015

Do an HTTP GET request with parameters in Groovy using HTTPBuilder

Ever need to do basic GET requests with a query string in Groovy? Something like
HTTPBuilder seems to be the de facto module for this.

However, I found it's usage a bit odd and the documentation very convoluted. I wanted it distilled down to the simplest form.

This is what I've come up with:


def doGET() {
    try {
        def http = new HTTPBuilder('')
        http.get( path : '/endpoint', query : [helowrld:'hi!'] )
        { resp ->
            return resp.entity.content.text
    catch( e)
        println e.toString()

println 'doGET(): ' + doGET()

Not quite as simple as I'd have liked, but it fits the Groovy idiom best, I guess. Anyway, of note:

The query string is passed as a map to the second parameter of the get() method. Add any number of key/value pairs to it as needed, e.g.

query : [helowrld:'hi!',param1:'something',param2:'somethingelse']
To actually get the response, you have to use a closure, which is this block:
{ resp ->
    return resp.entity.content.text
This returns the raw response, which in this case is HTML and looks like:
doGET(): <html><head><title>hi</title></head><body>hello</body></html>
The closure above is quite useful for doing other things as well, such as setting headers and more. Also, if the response is returned in something like JSON, the resp object automatically converts it into a map you can use immediately.

One last thing. You have to have the HTTPBuilder module installed. That was a pain. You need to get the appropriate .jar files in the lib/ folder of your groovy installation. I had some trouble finding that module with all its dependencies, but this archive seems to do the trick:

Extract and copy the .jar files into the folder above.