11

I work on a MacBook Pro and frequently switch between using just the laptop screen, or with an additional 30" screen connected at work. The problem here is that I can't position my Geeklets in such a way that they suit both screens all the time.

In order to fix this, I wrote an AppleScript that determines the size of my primary screen and then repositions all the Geeklets accordingly.

The AppleScript is as follows, although I have shortened it to just one example (the full script repositions a whole stack of different Geeklets).

tell application "Finder"
    set Display to bounds of window of desktop
    set DispW to item 3 of Display
    set DispH to item 4 of Display
end tell

tell application "System Events" to tell process "Dock"
    set DockSize to size in list 1
    set DockW to item 1 of DockSize
    set DockH to item 2 of DockSize
end tell

tell application "GeekTool Helper"

    --Weather
    set WeatherImg to first item of (geeklets whose name is "Weather Image")
    set WeatherTmp to first item of (geeklets whose name is "Temperature")
    set WeatherCty to first item of (geeklets whose name is "City Name")
    set x position of WeatherImg to DispW - 290
    set y position of WeatherImg to 30
    set x position of WeatherTmp to (x position of WeatherImg) - 82
    set y position of WeatherTmp to (y position of WeatherImg) + 71
    set x position of WeatherCty to (x position of WeatherImg) - 82
    set y position of WeatherCty to (y position of WeatherImg) + 45

end tell

This all works fine, and it means I can quickly reposition all the Geeklets by just executing a single script, which to start with I saved as an application. However, I thought it would be really nice to make it all happen automatically, so I played around a bit more.

One of my Geeklets displays network throughput, and this is set to refresh every 3 seconds, which I thought would be a reasonable frequency to quickly check if the screen configuration had changed at all, and automatically run the script if it had.

By adding a couple of extra lines to the Geeklet, I ask the system how many monitors are active; compare that to the value from last time around (which I write and read to a temporary file) and then if there has been a change the AppleScript is called. The additional code is as follows:

DispNew=`osascript -e "tell application \"Image Events\" to return count displays"`
DispOld=`grep -s . $HOME/Documents/Geeklets/Displays.txt`
if [ $DispNew != $DispOld ]; then
    osascript $HOME/Library/Scripts/Geeklets.scpt
    echo $DispNew > $HOME/Documents/Geeklets/Displays.txt
fi

Note that this reads and writes a temporary file to the folder /Documents/Geeklets/ which can be changed as required.

Comments

Log in to comment or register here.