Posted by & filed under Computers & Internet.

I use VirtualBox for testing a couple of web applications. But I found myself using the same image for two different projects, but switching the memory back and forth between 512mb and 2gb. 512mb was more than enough for one of the projects; but the other, running a Dockerized version of Canvas; would use every bit of the 2gb.

First task was to come up with a way to quickly switch between 512mb and 2gb without going through the menus in VirtualBox.

The other tweak I wanted to make was running the VM headless. Yes, I can right click on the VM in VirtualBox and start it up headless. But I tended to forget.

Enter VBoxManage and batch files.

Here’s the batch file that starts up the 512mb VM.

It calls the VBoxManage file to set the memory, starts the VM, waits for a keypress, and then turns off the VM. That lets me just have a small console window open instead of the full VirtualBox app.

And here’s the one for the 2gb VM.

Same thing except the numbers change in a couple of places.

Now, I just SSH into the box and everything is slick. And as a bonus, my computer doesn’t seem to slow down as much.

Posted by & filed under Coding, WordPress.

A few months ago I needed a way to cross post from a WordPress blog to a Pinterest board. Yes, there are plugins that claim to do this. But the few free ones that I tried never actually posted, and I didn’t want to spend money on a paid plugin that might not work. So I came up with my own solution.

One note though. This isn’t a WordPress plugin, although I may write one eventually. This is a script that runs from a cron job on your server. You can setup cron jobs through control panels like cPanel or directly from the command line. Either way, most people will find it a bit more complicated than just activating a plugin.

What we’re building

The idea is that I didn’t really care that new posts were immediately posted to Pinterest as long as they got there relatively quickly and I didn’t have to do it manually.

It’s also important that posts only go to Pinterest once, so there needs to be a way to keep track of what’s already been posted.

What I ended up with is a script that runs every 30 minutes through a cron job, although you could change the timing to whatever you want. It pulls the most recent published post from WordPress and posts the link to a Pinterest board.

One caveat. Since Pinterest is visual and requires images, this script skips any posts that don’t have a featured image.

With that, let’s get it actually working. Read more »

Posted by & filed under Coding.

A couple of weeks ago I needed a way to convert a number of seconds to a more user friendly format for a project I’m working on. I’ve seen these called “ago” functions. They take a raw number of seconds and convert it to something friendly like “3 seconds ago” or “5 minutes ago.” Not quite what I wanted though. I was looking for something that counted seconds up to a minute and then minutes and seconds after that. For what I need, it wouldn’t ever go past minutes.

Read more »

Posted by & filed under WordPress.

I’m using bbPress on a couple of support forums. But a few days ago I went to share a thread on Facebook and noticed that the description coming from WordPress with Yoast SEO installed is showing the first part of the page content. In my case it was something like “This thread contains 2 voice, 3 posts” which is not what I want.

What I wanted was the text of the original post.

With a little filtering it wasn’t too tough.

I put this in my functions.php file since it was theme specific, but could be a really simple plugin as well.

Now it pulls the text from the original post and puts it into the meta description along with the open graph description fields.

Posted by & filed under Coding.

Earlier this week I was working on a JUnit test for one of the AP free response questions from 2016 where you’re given an array of Strings and need to implement a class that randomly pulls one out using a getNext method. You’re basically writing an iterator without calling it that.

But since it was random I needed a way to check that the right values were there, even though they probably weren’t going to be in the same order. So I needed to check that one String array was a permutation of another. And I really didn’t want to have to go through every permutation of one to see if one matched. That seemed horribly inefficient.

Google brought me to this page of interview questions with a cool solution for int arrays. The TL;DR version is that you get the sum and product of both arrays, and if they match they’re permutations of each other.

And here’s a bit of Java code to do that.

First check is to make sure the two arrays are the same length. If they’re not, there’s no reason to go on. Then it’s just a loop to get all the values and do a bit of math.

But what about Strings

That was close, but I needed to do the same with a String array. Enter hashCode.

And it worked, at least for the short arrays I threw at it. I assume it’d fail with either long strings or longer arrays since eventually multiplying a bunch of ints together is going to overflow a long. But it worked well enough for what I needed.

Posted by & filed under Computers & Internet.

A few months ago I bought an Epson DS-510 scanner to try and keep up with the ever growing mound of paper that you deal with. And overall it’s been a great scanner. It’s fast. Scans duplex. And the scans look good. But there’s been one nagging issue and that’s a Cannot Communicate with the Scanner error message.

If my computer went to sleep or I unplugged the scanner to move my notebook, the next time I plugged it in I’d get the dreaded Cannot Communicate with the Scanner message and would be unable to scan until I restarted my computer.


Cannot Communicate with the scanner Message

I tried all the fixes that Google found for me. Swapped out cables. Tried different ports. Reinstalled drivers. Nothing seemed to work.

Then I came across a site that had the fix. And I’d love to link you there, but I forgot to bookmark it.

And it’s a pretty easy fix. What looks like was happening is that the Windows Image Acquisition (WIA) service was hanging up. In the services list it showed as paused, which was a little weird.

The Fix

So, on to the fix. First go to the Task Manager. Easiest way to get there is press Ctrl-Alt-Delete and pick Task Manager from the menu that comes up.

Find the Windows Image Acquisition (WIA) process and select End Process. You’ll get a nasty warning from Windows. I’ve just ignored it and haven’t had any issues. Can’t promise you’ll have the same luck.

Windows Task Manager with Windows Imaging Acquisition service selected

And, you may be able to skip ending the task. But I had to do this for the next step to work.

Now you want to go to the Services list. Easiest way to get there is bring up the start menu and start typing Services. You can also get there through the Administrative Tools.

Once there you’re looking for the same Windows Image Acquisition service that you just ended. It’ll probably show Stopped. (It shows running here because I had already restarted it)

Windows Services List

Right click and select Start.

And, assuming it comes back up, that’s it. I’ve done this a dozen or more times by now and it’s worked every time.

Posted by & filed under Coding.

Working on a small weekend project I needed an easy way to validate that a string contained a valid Twitter username. Not necessarily an active account, but that could be an account.

Found the rules on Twitter’s site that a username can be between 1 and 15 characters and can contain only the characters A-Z, a-z, 0-9, or an underscore. Sounds like a perfect use for regex. So here it is, both in JavaScript and PHP since I needed both.

Posted by & filed under Code Tips.

For a Moodle plugin I’m writing I needed a way to check if a String exists. Normally, if a string doesn’t exist then Moodle just outputs [[missing_string]] (replace missing_string with whatever you’re calling the string) and moves on. But, in developer or debug mode it also displays a stack trace.

What I needed was a way to check if a string was set, and if not display a different string which I knew was set. This is what I came up with.

function get_string_check($identifier) {
	if (!get_string_manager()->string_exists($identifier, 'plugin_name')) {
		return get_string('a_known_string', 'plugin_name');
	return get_string($identifier, 'plugin_name'); 

The reason I needed this was that I was building strings based on error constants. So there might be a string err_1, err_2, err_3, etc. But I needed a way to not display an error if I requested err_99 without there actually being that string.

The only catch is that the you must have $string['a_known_string'], or whatever you want to call it, in the language file for your plugin. For me, it just says Unexpected Error.

Posted by & filed under Code Tips.

Not really code, but still something I need often enough to write it down.

This command will list all files in a Linux folder sorted by date with the most recently updated files at the bottom.

ls -ltrh

From the ls man page, here’s what each of the choices do.

-l – Uses a Long format which shows more information on each file

-t – Sorts by modification time

-r – Reverse order. Without this the most recently updated files would be at the top of the list instead of the bottom.

-h – Human friendly file sizes. Shows files in KB, MB, or GB instead of just bytes.