Showing posts with label os x. Show all posts
Showing posts with label os x. Show all posts

Monday, April 22, 2013

Keeping OS X Awake

Wake up and smell the coffee...

OS X 10.8 has quite an agressive sleep strategy. Quite often, when writing programs for the Enigmatic Code site, I need to stop my laptop sleeping while it runs a program to generate a solution to one of the problems.

I found out last year about the caffeinate command in OS X, which stops the system from sleeping while a command is running. So I often use something like this:
time caffeinate pypy enigma82.py 6

if I want to leave my laptop running something when I'm out, or overnight (and know how long it took).

But what happens if a program has been running for a while and you forgot caffeinate it before you started? You don't want to kill it off and lose all the work it's done, just so you can restart it under caffeinate.

Well the answer is simple, just write a quick Python¹ program to monitor the process you are interested in, and caffeinate that instead.

Here's my quick Python program - I called it pid-exists - it just monitors a process (using the pid specified as the command line argument), until it disappears, and then exits itself.
import sys
import os
import time

PID = int(sys.argv[1])

while True:
  try:
    os.kill(PID, 0)
  except OSError:
    break
  time.sleep(10)

Then you caffeinate that instead. For example:
caffeinate pid-exists 57905

will stop the laptop from sleeping while the process while pid 57905 is active. Once the process 57905 terminates then so will the pid-exists process and the parent caffeinate process and the laptop will go back to it's normal sleep strategy.

Note: On OS X 10.11 you can now just use:
caffeinate -w <pid>
to keep the computer awake while process <pid> is running.

───
¹ Other scripting languages are available.

Thursday, August 30, 2012

Mac to Mac AirPlay Mirroring

I've had my old laptop (currently a 2008 MacBook) plumbed into my TV for some time and use it mainly as a PVR by running EyeTV on it, but often I want to send media from my currently laptop to the TV and have never really found a satisfactory solution. [*]

Until now. Luckily the hardware on my MacBook Pro supports Intel's Quick Sync technology, so with the recent release of OS X 10.8 it is able to do AirPlay Mirroring to an AppleTV. Which would be great... if I had an AppleTV (and I've almost considered buying one just for this). But given that I've already got a MacBook plugged into my A/V set-up it would be more convenient to use that.

I tried a couple of free AirPlay server applications on the MacBook (both AirPlayer and BananaTV allowed media to be sent from Caroline's iPhone to the TV (BananaTV worked better with audio)) but they didn't allow AirPlay Mirroring from my laptop. But when I downloaded the paid AirServer app I found that it did exactly what I wanted. I'm less than a day into the week long free trial, but unless I find some serious flaws I'll be purchasing it.

Update 2012-09-24: There was a security update for my MacBook, which required a restart, which ended my free trial. But I purchased AirServer, and am happy with it. There are some problems with video/audio synchronisation when sharing the screen from the MacBook Pro to the MacBook (I found that setting the Audio Buffer Size to 2200 seemed to give the best synchronisation), but the best solution for streaming video is to do it from iTunes, and then the video and audio stay in sync.

[*] My previous best solutions were just to use a cable (3.5mm for just audio, HDMI for video/audio), although for audio only setting up AU Lab 2.1 to send the System Audio (intercepted by SoundFlower) to another instance of AU Lab running on the target system worked with the lowest latency. Setting up a reverse VNC connection worked well enough to allow displaying of photos to the computer plugged into the TV, but both of these are a bit fiddly compared to just plugging a cable in, or the wireless simplicity of AirPlay.

Sunday, June 24, 2012

Farewall appscript?

In preparing for Macaroni Penguins Gig #126 tonight I noticed that my newly updated iTunes 10.6.3 didn't seem to be playing with my py-appscript scripts.

A quick search brought up this posting entitled "The first nail in the coffin of Python appscript", which reveals that new security technologies coming along in Mountain Lion (OS X 10.8) might well stop existing appscript based scripts from working. And anyway appscript itself is no longer being developed or supported, so I fully expect all my useful little scripts to gradually decay into non-functionality over time.

For the time being I have managed to use appscript.terminology.dump() to make a static glue from iTunes 10.6.1 that I can use to allow my scripts to continue to run with iTunes 10.6.3, but it doesn't feel like a robust long-term solution.

Update 2012-09-12: I updated to iTunes 10.7 (under OS X 10.8) and my py-appscript scripts are working again. Hurrah!

Thursday, February 10, 2011

Top Time Machine Tip

Here's a Top Tip for those using Apple's Time Machine...

If you are backing up multiple computers to a single Time Capsule, and one of the machines decides there isn't enough space to complete a backup, try compacting the other backups - especially if they are quite large.

Open a Terminal and the following command:

# hdiutil compact /path/to/backup/sparse/bundle

I did this on the backup of my machine, when another laptop was deleting old backups trying to make space, and it freed up over 90GB of space. Plenty enough to allow the other machine to complete it's backup.


I turned off automatic backups while it was running - remember to re-enable them afterwards.

This is something that Time Machine will run itself if it's runs out of space, but it will only compact the backup for the machine that it is running on, so if there is a potentially large amount of free space in a backup for a different machine that won't get reclaimed. Instead the first machine will start sacrificing it's older backups to make space.

Note: I'm running Time Machine on OS X 10.6.6

Thursday, November 4, 2010

Setting Camino to be Colour Managed

I've wondered for a while why photos sometimes look a bit washed out in Camino.

It turns out it's because Camino is not observing the embedded colour profiles in the images.

And the way to switch on Colour Management in Camino is:
  1. Go to about:config.
  2. Set gfx.color_management.enabled to true.
For more information on Colour Management in browsers, see this post: Web Browser Color Management Tutorial.

Friday, July 31, 2009

Scripting OS X

One of the nice things about Mac OS X is that you can interact with many of the applications using external scripts. The main issue is what scripting language you use to write your scripts in. The obvious choice is, of course, AppleScript, but while it makes it easy to interact with the applications it isn't as functional at text or date manipulation as a traditional scripting language, such as Perl.

A few years ago I was pleased to find there was a Perl module available called Mac::Glue that would let you talk to applications in OS X without having to use AppleScript. I had downloaded the AppleScript guide from Apple and had tried to use it, honest. But having used Perl nearly every waking hour over the previous 7 years I had a fair amount of expertise locked up in Perl and in a short time, with the help of Mac::Glue, I was able to knock up a quick script to help me organise my iPhoto library, and later I added scripts that I used with other applications.

Perl, however, is not an officially supported scripting language for OS X, and in OS X 10.5 both Ruby and Python were supported by Apple for scripting OS X. I decided that Ruby was the cooler of the two languages to use as it's a bit like Perl rewritten by a Smalltalk geek (that and I have always had a reservation about Python's use of syntactic whitespace), so in 2008 I dabbled a bit by rewriting my iPhoto script in Ruby using RubyOSA and was pleasantly suprised to find that it ran quite a lot faster.

That was all fine and dandy, and I transitioned from OS X 10.4 on my PowerBook G4 to OS X 10.5 on my MacBook without a glitch. Until Apple released OS X 10.5.7, at which point my RubyOSA scripts stopped working, so I went back to using Perl and Mac::Glue.

Recently I have wanted to export playlists from iTunes to the memory stick from my phone (or sometimes to a USB stick or just to a directory), something that iTunes doesn't seem that keen on (unless optical media is involved). And I have also been looking for a suitable programming language for my 10 year old nephew (who has just got a netbook for his birthday - Hi, Matthew!) to learn. So, bolstered by a comment on Slashdot that had mentioned it only takes a couple of hours to learn, I put away my irrational dislike of Python and decided to give it a go. And sure enough I was able to knock up a script, very straightforwardly, that did exactly what I wanted.

If you're a UNIX command line geek and you'd like to give it a go you can download from the link below. Obviously you'll need the Python appscript library for it to work.
The script currently supports the following actions:
  • list-playlists [<pattern>]
    This lists the playlists in iTunes (that optionally match the specified pattern). The playlists listed include the names of the enclosing folders, and are prefixed by an integer index for easy reference (especially useful if you have multiple playlists with the same name). Also displayed is a track count, the duration of the playlist and the cumulative size of the files in the playlist.
  • export-playlist <playlist> [<dir>]
    This exports media from the specified playlist to specified directory (or the current directory if none is specified). You can specify the playlist either as the playlist name (along with enclosing folders) as, or the playlist index, both of which are printed out by list-playlists. The directory will be created, if it doesn't exist. The filenames for the exported media are generated from the track numbers in the playlist, along with the track name, and have punctuation and spaces removed or translated to make them more friendly.
  • export-current-playlist [<dir>]
    If you are currently listening to something in iTunes this will export the current playlist to the specified directory (or the current directory if none is specified).
  • help
    List the available actions, along with brief descriptions.
So you can use it like this:
% itunes list-playlists never
[282] CDs > Nirvana > Nevermind (12trk 42m31s 61.4MB)
[547] Compilations > 16. Never Give In (2008) (28trk 1h33m15s 127.4MB)
% itunes export-playlist 547 /Volumes/JIM\'S\ W380I/music/compilations/never_give_in
Exporting: "Compilations > 16. Never Give In (2008)" -> /Volumes/JIM'S W380I/music/compilations/never_give_in
Creating directory: /Volumes/JIM'S W380I/music/compilations/never_give_in
[ 1] "Changed Daily" -> 01-changed_daily.mp3
[ 2] "It's A Fine Day" -> 02-its_a_fine_day.mp3
[ 3] "Road To Nowhere" -> 03-road_to_nowhere.mp3
[ 4] "To Get Down" -> 04-to_get_down.mp3
[ 5] "Teenage Dirtbag" -> 05-teenage_dirtbag.mp3
[ 6] "Gay Bar" -> 06-gay_bar.mp3
[ 7] "Voodoo Child" -> 07-voodoo_child.mp3
[ 8] "Games Without Frontiers" -> 08-games_without_frontiers.mp3
[ 9] "Overload (Original Edit)" -> 09-overload.mp3
[10] "Best Of You" -> 10-best_of_you.mp3
[11] "19-2000 (Soulchild Remix)" -> 11-19-2000.mp3
[12] "Come On Home" -> 12-come_on_home.mp3
[13] "National Express" -> 13-national_express.mp3
[14] "Some Girls" -> 14-some_girls.mp3
[15] "Turning Japanese" -> 15-turning_japanese.mp3
[16] "All The Small Things" -> 16-all_the_small_things.mp3
[17] "Flagpole Sitta" -> 17-flagpole_sitta.mp3
[18] "Sk8er Boi" -> 18-sk8er_boi.mp3
[19] "Get Over It" -> 19-get_over_it.mp3
[20] "Thrillseeker" -> 20-thrillseeker.mp3
[21] "Single Girl" -> 21-single_girl.mp3
[22] "Sale Of The Century" -> 22-sale_of_the_century.mp3
[23] "P.V.C." -> 23-pvc.mp3
[24] "Take Me Out" -> 24-take_me_out.mp3
[25] "Scream" -> 25-scream.mp3
[26] "Underwater Love" -> 26-underwater_love.mp3
[27] "Green Bird" -> 27-green_bird.mp3
[28] "Changed Pandimensionally" -> 28-changed_pandimensionally.mp3

Note that I have a symlink to itunes from itunes.py, so I can point the itunes command to whichever implementation of the script is currently in favour.

If you don't like the " > " separator used to indicate playlist folders you can change the SEP variable in the script to whatever you prefer.

One day I may learn to do GUI scripting in OS X and put an interface on to it.

The only problem I did have was with Unicode characters. Although my terminal is set to use UTF-8 and the $LANG variable is set accordingly, Python kept blowing up when it encountered playlists or folders with non-ASCII characters in. So I did a bit of jiggery-pokery in the script that seemed to sort it out for me. When I am more familiar with Python I may come up with a better solution.

Feel free to use this script, and also to pass any comments on to me, although it works for me as I intended so I'm unlikely to put a great deal of effort into maintenance. Bear in mind it is my first Python script (and I happen to like 2 space indents). I'm hoping that it will continue to work when OS X 10.6 (Snow Leopard) is released in September (and even OS X 10.5.8 which is likely to come even sooner).

Tuesday, June 30, 2009

Using a Garmin Foretrex 101 with an Apple MacBook

In my previous post I wrote about the process that led to my purchase of a Garmin Foretrex 101 handheld GPS unit (or wearable personal navigator as it says on the box). This post provides some more detail of how I am using it with an Apple MacBook.

The first problem is how to connect the GPS to the MacBook. It would be nice if the GPS unit had Bluetooth capability so that you could wirelessly transfer data direct to the MacBook. But it doesn't. Instead it has a serial connection - which is absent from every computer I've bought this decade. After some poking around on the Internet I found that there were several serial/USB converter cables available. Garmin list one as an accessory for the Foretrex 101, but it gives a list price of £33.99. Instead I plumped for a cable from Amazon, supplied by iBox Ltd, with postage it came to the grand total of £4.70.

The converter cable arrived after a couple of days - well ahead of the main Amazon order which included the GPS unit itself. The cable itself was slightly different from the one in the photo on the Amazon page, in that although the actual 9-pin serial connector itself was male, it had screws to secure the connection instead of hexagonal sockets. Fortunately I was able to liberate a pair of hexagonal nuts from an old serial extension cable to make sure it stayed firmly connected to the serial plug.

A driver is needed to get the converter cable to work with Mac OS X (I'm running OS X 10.5.7 on my MacBook), and a mini-CD was provided with the cable, although you can't use it in a slot loading drive like the MacBook has. The driver needed for OS X is in /Prolific/PL2303X/MacOS-X/PL2303_1.2.1.dmg. Although I went to the Prolific web site and downloaded the slightly newer 1.2.1r2 driver.

You can check that the converter cable uses the Prolific PL2303X chipset by plugging it in and going to System Profiler and checking Hardware > USB section for a device called USB-Serial Controller. It should have a Vendor ID of 0x067b (Prolific Technology, Inc) and a Product ID of 0x2303. Once the driver is installed and the cable is connected there is a new serial device called usbserial, which shows up as /dev/cu.usbserial and /dev/tty.usbserial.

As I was still waiting for the GPS to arrive I tested that the cable was working by plugging in a serial trackball that I had and running od /dev/tty.usbserial in a Terminal. Output appeared when I moved the trackball or clicked a button, so it was looking promising.

The actual GPS unit itself arrived a couple of days later, along with the serial cable. Although the cable is listed on the Amazon site as "Garmin PC Cable (Forerunner 201 & Foretrex 201)" it works fine with the Foretrex 101 too.

At first I didn't much care for the idea of it being wrist mounted. Some of the reviews I had read online claimed the Foretrex 101 was no bigger than a watch. It is a lot bigger than any watch I've ever had, but it is surprisingly light. With batteries installed it weighs in at just under 100g, whereas my watch weighs 80g. And it does seem to function well on your wrist, and if you really want to know the time (and date) it's there in a tiny font on top of the Main Menu page. Or you can select it as one of the readouts on the useful Trip Computer page. After a couple of experiments of putting it in my pocket I attached the wrist strap and now I always use it on my wrist.

Loading data from the GPS to the MacBook is quite straightforward using a GPS utility such as LoadMyTracks. First you need to plug the 2.5mm plug of the serial cable into the GPS. The port is hidden behind a little weatherproof rubber flap. Make sure the plug is pushed in all the way. Then plug the USB cable into the MacBook, turn the GPS unit on and fire up LoadMyTracks. You should select "Garmin Serial" from the dropdown and "usbserial" as the serial port to use, then make sure the GPS unit is turned on and click "Acquire". The application will prompt you for a location to save the data in and will show a progress bar as it transfers the data. The GPS unit will beep and display "Transfer Complete" once the process is done. The data can be saved as a KML file to use with Google Earth, or a GPX file for a variety of uses. I am currently loading GPX files into TrailRunner to keep a log of my GPS tracks.

The GPX data can also be used for geotagging photos using a utility such as GPSPhotoLinker. The best way I have found to geotag photos for use with Apple iPhoto '08 is do this is as follows:
  • Synchronise the clock of your digital camera with the GPS before you start.
  • Take your GPS with you when you take photos.
  • When you get back - before you load them into iPhoto - first make sure the clock on the camera is still synchronised to the GPS. If it isn't just take a photo of the clock on the GPS screen, and then you can use the time difference when tagging the photos.
  • Plug the memory card with the photos on into the Mac, and quit iPhoto (if it starts automatically).
  • Use GPSPhotoLinker to geotag the photos on the memory card. You can use the application to timeshift the photos (if you forgot to sync the camera in the first place), and to check the location you are tagging with before you actually tag the photo.
  • Unmount the card from Finder to make sure the tags are written out to the media.
  • Re-insert the card to the Mac and then import the photos into iPhoto.
Note that with iPhoto '08 you need to geotag the photos before importing them, but once they are imported you should be able to see the location data by bringing up the "Photo Info" window (the GPS data is in the "Exposure" section). You can also select "Show File" to bring up the image file in Finder, open the file with Preview, and then use "Tools > Inspector" to display the GPS data (and then you can click on the "Locate" button to view the location in Google Maps).

For general use the Foretrex should be set to "Garmin" on the Settings > Set Interface > I/O Format page, although you can get it to stream GPS data direct to a Mac utility such as GPSUtility by setting it to "NMEA". (Although I have yet to find a use for this).