/dev/trouble
Eric Roller's Development Blog

General

General development topics.

Quite often I find myself in the Terminal and want to open Xcode with the project in the current directory:

% open CurrentProject.xcodeproj

This works just fine, of course, but with the following alias (added to ~/.cshrc), I don't need to know the name of the project, assuming there is always only one project in the current directory:

% alias xcode '\ls | grep xcodeproj | xargs open'
% xcode

N.B. The use of \ls ignores the alias that I have for 'ls'. Instead of ls *.xcodeproj, the pipe via grep avoids errors like "ls: No match.".

P.S. Knowing the name of the project is actually not the problem; since there are multiple items named "Current" or "CurrentProject" in the same folder, the issue is that I can't just type open SHIFT+C<TAB><CR> (where pressing TAB autocompletes the name); instead, I have to type: open SHIFT+C<TAB>SHIFT+P<TAB>.x<TAB><CR>.

Update

The Xcode text editor invocation tool xed can achieve much the same thing; just type:

% xed .

Similar to what I have been doing with subversion, I now also create backups for my git repositories.

Note that the git repositories on the backup host are local repositories (not cloned ones) and that they are named ".git".

From /etc/periodic/monthly/mine.monthly:

if [ -d /Library/Repositories ]; then
  cd /Library/Repositories
  for i in *.git ; do
    # Not a local git repository? Ignore it.
    if [ ! -f "$i/HEAD" ]; then
        continue
    fi
    line="/tmp/monthly-$i.out"
    # File: /Volumes/Backups/Attic/git-file-2016-08-10.bundle
    name="git-`basename $i`-`date +%F`.bundle"
    dump="/Volumes/Backups/Attic/$name"
    if [ ! -f "$dump" ]; then
      echo "Bundling git repository: $name"
      { cd "$i"; git bundle create "$dump" --all; } >& "$line"
      # Change the user for the dump file.
      chown gollum:gollum "$dump"
      # Only output the last stderr line then delete the file.
      tail -1 "$line"
      rm "$line"
    else
      echo "Warning: Dump already exists: $name"
    fi
  done
else
  echo "Error: Repositories directory not found."
fi

The weekly backups are much the same, except that only the three most recent repositories are backed up. Thus, the for loop in /etc/periodic/weekly/mine.weekly changes to:

  for i in `ls -1t *.git | sed '3 q'` ; do
    [...]
    line="/tmp/weekly-$i.out"
    [...]

N.B. I still use TrashLater to keep files for only 6 months in the Attic folder.

Blog Reboot: First Post

- Posted in General by

I have decided to switch my blog to a different backend; one that is easier to maintain and doesn't rely on MySQL (which I always found overkill and difficult to backup). Now I only need to extract my old blog posts from that last database dump file...

Update: Having worked through the dump file in a text editor, I found hundreds of duplicate entries marked as 'inherit' and 'nnn-revision-m'. These entries may be the result of multiple edits of a post. This is exactly the kind of data bloat that I strive to avoid. The current solution stores all entries in simple text files. Easy to back up. Easy to index. No duplication.

Just added a new section to /etc/periodic/monthly/mine.monthly in the attempt to create monthly backups of my subversion repositories, using compressed dump files:

[Update: Now using quoted variable names.] [Update: Now checking "format" file.]

if [ -d /Library/Repositories ]; then
  cd /Library/Repositories
  for i in * ; do
    # Not a subversion repository? Ignore it.
    if [ ! -f "$i/format" ]; then
        continue
    fi
    line="/tmp/monthly-$i.out"
    # File: /Volumes/Backups/Attic/svn-file-2014-01-17.dump.gz
    name="svn-$i-`date +%F`.dump.gz"
    dump="/Volumes/Backups/Attic/$name"
    if [ ! -f "$dump" ]; then
      echo "Dumping subversion repository: $name"
      # Dump into a gzipped file; "Dumped revision n" into temp file.
      { /usr/bin/svnadmin dump "$i" | gzip -c -9 > "$dump"; } >& "$line"
      # Change the user for the dump file.
      chown gollum:gollum "$dump"
      # Only output the last stderr line then delete the file.
      tail -1 "$line"
      rm "$line"
    else
      echo "Warning: Dump already exists: $name"
    fi
  done
else
  echo "Error: Repositories directory not found."
fi

N.B. I also use TrashLater to keep files for only 6 months in the Attic folder.

For /etc/periodic/weekly/mine.weekly I only dump the most recently modified repository since I don't normally work on multiple projects. This is done with a different foreach loop, where we get just one repository file name:

if [ -d /Library/Repositories ]; then
  cd /Library/Repositories
  # List the dir by last modified and get the first entry only.
  for i in `ls -1t | sed '1 q'` ; do
    line="/tmp/weekly-$i.out"
    # [...as above...]
  done
else
  echo "Error: Subversion repositories not found."
fi

phpMyAdmin

- Posted in General by

I see from my web-server log that people like to guess the path to phpMyAdmin in all different variations, colours, shapes and sizes. Now, this is exactly why I have not installed it in a fashion that makes it visible to anyone other than me. Better yet, come to think of it, I have not installed it at all and I don't think I ever will. But if I do, you should probably search for all variations, colours, shapes and sizes of phpMyNeatlyStashedAwayAndHiddenInstallationOfMyAdmin.

While it is convenient to add applications to the Login Items* list, it is also annoying when it takes ages for all of them to launch before you can do any meaningful work.

(*) In the Settings applications, select Accounts, your account and then the Login Items tab.

It occurred to me that one or the other application does not need to be available at login, but it would be nice to load it later. For instance, I don't use Skype during the day, but more in the evenings to contact my friends. So how about launching Skype at 5 p.m.?

The following launchd specification will do exactly that: launch Skype at 5 p.m. (or when not possible, whenever you log in next time):





        Label
        com.skype.skype
        ProgramArguments
        
                /usr/bin/open
                /Applications/Skype.app
        
        StartCalendarInterval
        
                Minute
                0
                Hour
                17
        


Save this file as ~/Library/LaunchAgents/com.skype.skype.plist (note that the Label must correspond to the file name) and wait for magic to happen when you log in next time...

After having spent half an hour looking for a drawing program for vector graphics on the Mac, it occurred to me that I already had one: Keynote .

Obviously, it is aimed at creating stunning presentations, but there is no reason why it cannot be used for any other kind of drawing. When finished, a drawing can be exported as an image file (JPEG, PNG, or TIFF) or simply grabbed from the screen with a screen shot (just use shift-cmd-4 and draw a rectangle).

Keynote is bundles with all new Macs but it is also available separately from the Mac App Store ($20). This price compares well to other graphics applications like OmniGraffle (standard $100, pro $200).

Google Search Tips

- Posted in General by

Added this blog post to get that piece of scrap paper off my desk and also to remember these tips by means of typing them in.

apples oranges Search for pages with both apples and oranges.

apples OR oranges -worms Either or, not others.

"candied apples" Exact phrase.

+fruit filetype:ppt Only the exact word, in PowerPoint presentations.

~fruit site:meals.com Include synonyms, but only on that site.

define:egoism Search after definitions.

BMW 318...328 Search for ranges, e.g. not the bad 316 model.

link:macworld.com Search after sites linking to macworld.com.

info:www.apple.com/se Show information about apple.se.

related:cdon.com Search for similar sites.

**mac*** All words beginning with mac, e.g. macworld, macintosh.

ARN BSL Search for routes between airports.

Harry Potter For books with harry potter, select the book icon.

stocks:AAPL Stock information.

weather Stockholm Search for weather information.

time Cupertino Get the current time for other places.

sunrise Kuusamo Get the sunrise times, try also "sunset".

5*9+(sqrt 10)^3= Evaluate calculations.

10.5 cm in inches Unit conversions.

66 GBP in EUR Currency conversions.

**Shakespeare wrote *** Let Google fill in the blanks.

P.S. Of course, Google has its own help page on this topic.

We can use iTunes to play music while we do circuit training, i.e. 40 seconds music while we work out, and 15 seconds pause while we rest or go to the next station:

  • save the following AppleScript in ~/Library/iTunes/Scripts; you will probably have to create the Scripts directory;
  • open iTunes with a suitable playlist; we recommend a good song and a Genius playlist;
  • select the first song;
  • launch the script from Scripts menu.

NB. This is an updated version of a previous script.

(* AppleScript for circuit training with iTunes.
The default circuits are 40 seconds each, with pauses of 15 seconds.
At the beginning, the script allows you to set different times.
After every second set, iTunes is asked to go to the middle section
of the next song.

version 2.1, ©2010, Eric Roller
*)

property wCount : 40
property tWork : 40
property tRest : 20
property tFudge : 1.0 -- use this to adjust the rest times

on run
    -- begin by asking for the workout and delay times
    set wCount to askUser("Number of Workouts", wCount)
    set tWork to askUser("Workout Time in Seconds", tWork)
    set tRest to askUser("Rest Time in Seconds", tRest)
   
    -- whether to skip to the next song, but only every other time
    set skip to false
    set remaining to wCount
   
    try
        with timeout of 6000 seconds
            -- enter the workout loop
            repeat while (remaining > 0)
                announceCount(remaining, wCount)
                -- wait, play the music, wait and then stop it again
                sleepFor(tRest, "Rest")
                tell application "iTunes" to play
                sleepFor(tWork, "Workout")
                tell application "iTunes" to pause

                if skip is true then
                    -- every other time, change the track
                    tell application "iTunes" to next track

                    -- skip to the middle section of the next track
                    set tForward to trackSeconds() / 2 - tWork
                    tell application "iTunes" to ¬
                        set the player position to tForward
                end if
                set skip to not skip
                set remaining to remaining - 1
            end repeat
        end timeout
    on error errText number errNum
        if errNum is not -128 then ¬
            display dialog "Error: " & errNum & return & errText
    end try
   
    -- finish by pausing iTunes
    tell application "iTunes" to stop

    -- work out how many iterations we did
    set remaining to wCount - remaining
    if remaining is greater than 0 then ¬
        say "Well done! You completed " & (remaining as text) ¬
        & " workouts."
end run

-- small helper to ask the user for an integer
on askUser(aText, aDefault)
    set anInt to the text returned of ¬
        (display dialog "Set the " & aText default answer aDefault)
    return (anInt as integer)
end askUser

-- helper to announce how many more workouts there are
on announceCount(aCount, maxCount)
    if aCount = maxCount then
        set message to "Starting " & (aCount as text) & " workouts."
    else if aCount = 1 then
        set message to "Last one."
    else if ((2 * aCount) as integer = maxCount) then
        set message to "Half time."
    else if (random number from -1.0 to 1.0) > 0.0 then
        set message to (aCount as text) & " more"
    else
        set message to (aCount as text) & " to go"
    end if
    delay (tFudge / 2)
    say message without waiting until completion
end announceCount

-- helper to wait for a given amount of seconds
on sleepFor(tDelay, workType)
    set tOut to tDelay
    -- apply a fudge factor for the rest times
    if workType is "Rest" then set tOut to tDelay - tFudge
    -- we use a dialog message with a delay
    set answer to display dialog (tDelay as text) & " Seconds " ¬
        & workType buttons "Stop" with title "Circuits Loop"  ¬
        giving up after tOut
    -- trigger an error if we want to stop
    if the button returned of the answer is "Stop" then ¬
        error "User canceled." number -128
end sleepFor

-- helper to determing the length of the current track in seconds
on trackSeconds()
    -- ask iTunes for the track length (MM:SS)
    tell application "iTunes" to get the time of the current track
    set the tLength to the result

    -- split the length into minutes and seconds
    set saveDeli to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {":"}
    set tMinutes to the first text item of the tLength
    set tSeconds to the last text item of the tLength
    set AppleScript's text item delimiters to saveDeli

    -- add up the total length in seconds
    return (tSeconds + 60 * tMinutes)
end trackSeconds

Symptoms: When 'mounting' the external disk, shared over the AirPort Extreme WiFi network, it is not added to the Finder's sidebar; instead, you get an error like this:

The alias can't be opened because the original item can't be found.

Also, dragging the disk icon from the SHARED section in Finder's sidebar to the DEVICES section is not possibe; the Finder just displays a "can't drop it here" icon.

Searching online, I found a discussion on Apple's forums which contained the solution.

Fixing permissions with Disk Utility was not an option since a network-shared disk is not accessible within the app.

Apparently, the disk acquired an "alias" attribute by mistake. This can be checked in the Terminal, where the alias attribute is the capital 'A':

% /usr/bin/GetFileInfo /Volumes/AirDisk
directory: "/Volumes/AirDisk"
attributes: Avbstclinmedz

You can use the following command to turn the alias attribute off (then check again with the above GetFileInfo command and look for a lowercase 'a'):

% /usr/bin/SetFile -a a /Volumes/AirDisk

After that, the mounting problem disappeared.