Short source code examples

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


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

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

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.

I just used this ImageMagick code in a Unix shell script to add a 20% black border to every image in the current directory:

To install the Python pip command on Debian Linux, a Raspberry Pi in my case, use this command:

sudo apt-get install python-pip

That command works with Python 2. For Python 3 install information see this link.

Note: I used that command so I could install the Python feedparser libary with this pip command:

sudo pip install feedparser

Java FAQ: How do I initialize/populate a static List (ArrayList, LinkedList) in Java?

If you need to have multiple types extend a base type in Haskell, while using the data keyword, and while using Haskell’s record syntax, this approach seems to work:

I’m currently trying to cram Haskell into my brain, and in an effort to do so, I’ve been writing a fake POS (point of sales) application. It has a command-line UI that prompts the user to add Pizzas and a Customer to an Order.

I need to re-organize the code, but this is what about 50% of the code looks like right now. The other 50% is in a module that defines what a Pizza is, and also defines its helper functions.

I’m relatively new to Haskell, so I don’t know if there are better ways to do this, but if you need to define a function that takes no input parameters and returns a value, this function type signature works:

foo :: [Char]
foo = "foo"

I don’t know a good reason for using that specific approach, but I have found in test code that I want to do something with input/output, and so I needed a function that returned an IO String, but didn’t take any input parameters, and that function definition looks like this:

To configure your GHCI prompt, create a Haskell/GHCI configuration file named .ghci, which should be in your home directory. Assuming that you are first creating it, put content like this in it:

:set prompt "ghci> "
:set +t

The first command shown sets the GHCI prompt, and the second command tells GHCI to show the type of each expression you type.

As an example of the file location, my home directory is /Users/Al, so I created my file as /Users/Al/.ghci.

There may be other ways to do this, but if you need to determine the type (type signature) of an object in a Haskell program, the following approach worked for me.

First, add this import statement to your program:

import Data.Typeable

What that does is give you access to a function named typeOf, which you can then use like this:

I was working on a Haskell factorial function (which turned out to be easy) and decided I wanted to write a program to prompt a user at the command line for an Int, and then do the factorial of that Int.

I quickly learned that this required a String to Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a String to Maybe Int conversion, as you can see in this code:

This is some test code I wrote. It shows how to read a text file in Scala.

This first program shows how to read the entire file into memory and print out the “byte values” of each byte/character in the file:

FWIW, this is the source code for a sed script I use on my Mac OS X system to convert HTML output generated by MacDown into a format I need. MacDown generates some extra “cruft” that I don’t need, so I use these sed commands to clean up that HTML output:

If you need to turn off (disable) java.util.logging logging, adding this line of code to your main method seems to work: