Short source code examples

Note: This code is currently a work in progress. I know of possible approaches, but I don’t know of a perfect working solution yet.

I’m currently trying to find the right way to find the current monitor size, when you’re writing a Java Swing application to work in a multiple-monitor configuration. I always use three monitors, so I can test this pretty easily.

If you need some source code for a Java FileFilter for image files, this code can get you started:

As a quick note, if you ever need to use a Java TimerTask, you can define one like this:

class BrightnessTimerTask extends TimerTask {
    @Override
    public void run() {
        // your custom code here ...
    }        
}

and you can then instantiate it, create a Timer, and schedule the task like this:

Until a little while ago I don’t think I had ever thought about intentionally casting a null value in Java, but then I ran into a problem and realized that the solution was to cast a null value, like this:

FileDialog d = new FileDialog((java.awt.Frame) null);

You have to do that in this case because FileDialog has several one-argument constructors, including one that takes a JFrame and another that takes a JDialog. If you just put null in the constructor the Java compiler or your favorite IDE will complain, so you have to cast the null value to one of those specific types, and this syntax shows how to do this. (My app uses multiple frames, and at the moment I’d rather put null in the FileDialog constructor than try to determine which frame is currently in the foreground.)

As a quick note, I was just reminded that when you have a Scala tuple instance, you can assign the tuple fields to Scala values. This tends to be more readable than accessing the tuple elements using the underscore syntax.

For example, if you have a Scala function like this that returns a tuple:

def getUserInfo = {
    // do some stuff here, then return a tuple (a Tuple3 in this case)
    ("Al", 42, 200.0)
}

you can assign the tuple fields to Scala val fields like this:

I may explain this more in the future, but for now, here’s some source code for an example of how to use Quicklens in a Scala functional programming project.

Given some model/ADT definitions like this:

To configure Intellij IDEA to show Scala data types when you hover over a field, you have to go into the Intellij settings to enable this feature. (On Mac OS X you can also use the [Ctrl][Shift][P] keystroke when you’re on a field whose type you want to know/confirm.)

To enable this hover-over feature, follow these steps:

If you happen to need something like this, here’s some source code for a Java method that converts seconds in time to a String formatted as minutes and seconds:

I don’t remember the exact error message, but if you’re trying to use FreeMarker with Android and you get an error that says something like “Freemarker - NoClassDefFoundError TextBlock”, the root cause of the problem seems to be that FreeMarker uses the java.beans library under the hood, and the Android version of Java does not implement this. As a result, FreeMarker won’t work with Android, at least not without some modifications, as of January, 2016.

Android FAQ: How can I create a Color from a hexadecimal color string in Android?

The Android Color.parseColor method

Solution: Use the Android Color.parseColor method, like this:

int color = Color.parseColor("#519c3f");

I just used it like this in my code, where I directly set the Paint color:

To see which services are configured to run at startup on a Rasperry Pi (Raspian) — and also see their current startup status — issue this Linux service command:

sudo service --status-all

The service man page describes what this command does:

service --status-all runs all init scripts, in alphabetical order, with the status command

On my Raspberry Pi (RPI) that command produces a long list of output that looks like this:

AppleScript FAQ: How can I size or resize an application window using AppleScript?

To resize an application window with AppleScript, use a command like this:

tell application "Safari"
    set bounds of front window to {300, 30, 1200, 900}
end tell

That command tells the Safari browser to have the following size and location properties:

This little example shows the syntax of how to run some AppleScript/osascript from a Unix shell script on a Mac OS X system:

#!/bin/sh

osascript <<EOF
tell application "Safari"
  close window 1
end tell
EOF

Just put your AppleScript in between the EOF parts, save it to a file, make the file executable, and this gives you a nice way to run a multiline AppleScript script from a shell script.

Here’s another example of what this looks like:

If you ever need to close a Mac OS X application (gracefully) from the Mac Terminal command line or from a shell script, I can confirm that this command works:

osascript -e 'quit app "Safari"'

I use that command from a Mac/Unix shell script to close the Safari browser in an automation script I’m writing, and it works fine. Just replace Safari in that command with the name of the application you want to close.

I’m currently trying to automate a GUI task, and as a part of that, one thing I need to do is move the mouse cursor.

In short, the solution I came up with was to write a Scala shell script that uses the Java Robot class to move the mouse. Here’s the source code for my script, which I named MoveMouse.sh:

I’m currently doing something completely different, and writing a little custom web browser using JavaFX and its WebView component. I’m using it so I can easily look at stock quotes and charts. I just started on it, and the current UI looks like this:

A custom JavaFX WebView web browser

As a quick note today, the following Scala function is more or less the same as the Unix cat command:

I was curious about how the “input redirection” program on page 170 of Learn You a Haskell for Great Good (LYAH) worked, so I typed it into a file named do1.hs, with one additional line:

import Data.Char

main = do
    contents <- getContents
    putStrLn "dude"
    putStr $ map toUpper contents

The line I added is the putStrLn "dude" line.

I compiled it like this:

Without much discussion, here is some source code for a Scalaz 7 “Hello, world” example, using the Scalaz putStrLn function:

This short article shows how to write a command-line timer in Scala. There are simple ways to do this as just a regular Unix/Linux shell script, but I wanted to use Scala, in part so I could try to remember how to use the Java sound libraries. (I also initially thought about writing this is a GUI app, but decided to just make it a command-line app.)

I wanted a simple command-line timer, so I wrote one in Scala.