Short source code examples

If you’ve never used AppleScript, here are two iTunes AppleScript examples to get you going. First, This one tells iTunes to play the playlist named “My Favorites”:

tell application "iTunes"
    play playlist "My Favorites"
end tell

That script starts playing a random song from that playlist. If you want to start by playing the first song of that playlist, this script will do the trick:

As a quick note, if you’re looking at a Drupal form and it says you can use the "Rewrite the output of this field" replacement patterns shown (somewhere) on this page — and you can’t find those replacement patterns on that page — you can find a complete list of them at this drupal.org url.

As an example, if you’re working with a Drupal Node, you can use replacement patterns like these:

[node:author:name]
[node:content-type]
[node:content-type:name]

As a quick CSS note, if you want to achieve a “zebra striping” style with even and odd CSS row selectors, CSS styles like this will get the job done:

.path-frontpage .content-inner-right .content-type-Text:nth-child(even) {
    /* yellow */
    background-color: #fdfdf6;
}

.path-frontpage .content-inner-right .content-type-Text:nth-child(odd) {
    /* blue */
    background-color: #f3fbff;
}

I use that CSS for the front page of this website, but if you want a simpler example, here you go:

SQL FAQ: How can I select every row from a database table where a column value is not unique?

I’m working on an problem today where a Drupal article can have many revisions, and the way Drupal works is that a table named node_revisions has a nid field for “node id,” and a vid field for “revision id.” If you have five revisions of an article (i.e., a blog post), there will be five records in this table, and each record will have the same nid value and a unique vid. If an article has no revisions, this table will have one entry with a unique nid and unique vid.

If you have a Drupal 8 website and your images are not showing up on your image/photo nodes, one possible problem (which I just learned) is that your Drupal 8 theme needs to refer to {{ content }} in node.html.twig and not {{ content.body }}.

I don't know why you have to do that, but after four hours of troubleshooting the problem with my own Drupal 8 website/theme, I can confirm that if you don't do it, your images/photos won't be shown on their nodes. (All I was trying to do was to separate content.body from content.comment, and when I did that my images no longer showed up at their nodes/URLs.)

As a quick note, if you need a Drupal 8 Twig template if/else/then structure where you test to see if a string value is in an array, code like this will work:

{% if node.getType not in ['photo', 'text']  %}
    <div class="similar">
        {{ similar_by_terms }}
    </div>
{% endif %}

That code can be read as, “If the node type is NOT ‘photo’ or ‘text,’ emit the HTML/Twig code shown.”

FWIW, it appears that you can drop all of the “migrate” database tables that end up in your Drupal 8 database after a migration, such as migrating from Drupal 6 to Drupal 8. I ended up with 178 of these migration tables in my Drupal 8 database, and deleted them like this:

As a quick note to self, here is some source code for a simple example PHP script that runs a SQL SELECT query against a MySQL database:

A cool feature of the Scala REPL is that you can reset/clear a REPL session. To do so, just issue the :reset command, like this:

scala> :reset
Resetting interpreter state.
Forgetting this session history:

Assuming that you already have at least a little history in your REPL session, the :reset command will show you everything that it dumps, so the full output looks more like this:

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:

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: