Control System Prefs with AppleScript

I use TeamViewer to control the PC in the Hut, because it’s free, and it often works quite well.  The thing that drives me most nuts about it is how to right-click remotely.  I’m working from a Mac Book Pro with a trackpad.  I suppose I could actually use a two button mouse, but I haven’t used a mouse with a MBP forever.  I’m not even sure I have a two-button mouse… The solution is to go to the System Prefs Trackpad panel and set the Secondary Click checkbox for two-finger click.  trackpad-panel

That actually works great for TeamViewer remote sessions.  But it drives me nuts in MacLand, because I seem to use two hands on the trackpad and am constantly getting unwanted context menus.  So I wanted a handy way to turn on or off the two finger click trick programatically, which is to say, with AppleScript.

So how do you get your hooks into the Systems Preferences elements?

The key is to use System Events to get and set the window’s UI elements.  Here’s a simple script that collects the UI elements for the frontmost window of the application you want to peek at, in this case System Preferences:

tell application "System Preferences"
    --bring System Prefs to front
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        set windowsProperties to entire contents of front window
    end tell
end tell

return windowsProperties

The results window displays a lengthy list of elements that you’ll have to noodle over to figure out what you want.  In this case, I had opened System Preferences and clicked  to open the Trackpad panel, as in the image at top.  Here’s what the ScriptDebugger window looked like:  results of ui inspector

 

Here’s the pasted value (pasted into ScriptDebugger) from the results for “checkbox 2 of tab group 1 of window “Trackpad” of application process “System Preferences”:

tell application "System Events.app"
    tell application process "System Preferences"
        tell window "Trackpad"
            tell tab group 1
                tell checkbox 2
                    -- your code goes here
                end tell
            end tell
        end tell
    end tell
end tell

This nested tell set is clunky, but lets you easily understand exactly what’s going on, and you can just paste it into a script to get or set a value to a particular UI element.  I had to horse around to check or un-check the  secondary click checkbox.  “Normal” checkboxes have a value of “0” for unchecked and “1” for checked.  The script would let me get the value, “0” or “1”, but not let me set the value.  However, it did let me treat the checkbox like a button, and “click” it.  Go figure. That would be a problem if I really needed to set a particular value, but in this case, I’m happy to let it toggle:

--toggles "2 finger click" for secondary click on or off via trackpad pane of system Prefs
--needs "does trackpad panel exist?" 
--JJ McClintock 20130411

tell application "System Preferences"
    activate
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell window "System Preferences"
            tell scroll area 1
                --hopefully this will catch issues if no trackpad panel exists
                try
                    click button "Trackpad"
                    tell application "System Events"
                        tell application process "System Preferences"
                            tell window "Trackpad"
                                tell tab group 1
                                    ----set value doesn't seem to work
                                    --set value of checkbox 2 to 1
                                    ----get works fine
                                    --get value of checkbox 2
                                    --click works as toggle
                                    click checkbox 2
                                end tell
                            end tell
                        end tell
                    end tell
                end try
            end tell
        end tell
    end tell
end tell
--we want to quit System Prefs when we're done
tell application "System Preferences"
    quit
end tell

I saved the “toggle 2 finger secondary click” script as an application bundle and placed it in my script menu folder. Now I can turn on 2 finger click when I begin a TeamViewer session and turn it off the same way when I’m done.

MacBook Hibernation Weirdness

My new MBP (ML 10.8.2) has an unexpected morning behavior.  Instead of just popping the lid and getting started, I’m now treated to the “wake from hibernation” screen, and it takes a minute or two to get working.
Image of wake up progress bar
Apparently there’s some new power management thing going on,  so it’s time to poke around in the power management settings, if only to understand what’s going on.  You can see all your current power management settings from Terminal using “sudo pmset -g”. You will need to enter your admin password.

Here’s the pmset -g result on my MacBook Pro:
Active Profiles:
Battery Power        -1
AC Power        -1*
Currently in use:
standbydelay         4200
standby              0
womp                 1
sms                  1
hibernatefile        /var/vm/sleepimage
gpuswitch            2
halfdim              1
networkoversleep     0
disksleep            10
sleep                0 (sleep prevented by )
autopoweroffdelay    14400
hibernatemode        3
autopoweroff         1
ttyskeepawake        1
displaysleep         0
acwake               0
lidwake              1

This is all very interesting and worth reading through the man pages (“man pmset”). For my situation, I believe the issue is “autopoweroff” (the man page for pmset is silent on autopoweroff and autopoweroffdelay), which apparently sends the Mac into hibernation, after saving the contents of RAM, after the “autopoweroffdelay”.   The default autopoweroffdelay is 14400 (seconds), which is four hours.  Setting autopoweroff to 0 would turn it off, which I do not think would be desirable.  You want the system to hibernate after a reasonable interval in order to maximize battery power.  But I want more than 4 hours before hibernation, since it takes rather a long time to wake up, especially if you have a bunch of things open when the system sleeps…
open apps
I changed my autopoweroffdelay to 12 hours (43200 seconds), so the system will not be in hibernation after I wake up from my own personal slumber, which is somewhat less than 12 hours.  If the system is inactive for more than 12 hours, it will still save the contents of RAM to the hard disc, hibernate, and save battery power. The command from Terminal is “sudo pmset -a autopoweroffdelay 43200”  I tested this with shorter settings for autopoweroffdelay, and confirmed that this addresses the problem. The -a option for pmset applies the settings to both ac and battery operation; you could be more granular if you like.

Remote power controls for the Hut

Some time ago I got a Web Power Switch 4 for the hut to remote control power for the scope, cameras, and PC and future stuff like roof control and a light to illuminate a webcam.  I believe this is now replaced by the Web Power Switch 5, but they seem to be pretty similar.  It’s connected to the network via Ethernet and has 8 outlets than can be powered on or off remotely using a web interface. Also, there are 2 “always on” outlets.

Web Power Switch 4

The gadget works fine with Macs, but there doesn’t seem to be any documentation at digital-loggers.com for Mac setup.  So here’s a stab at that.

For your Mac to communicate with your Web Power Switch (WPS), you need to be on the same subnet.  First, you need to find your your IP address.  From the Apple menu, open System Preferences.  Select the Network pane.

Network Pane

In this case, the IP address is 192.168.0.34. So 192.168.0 is your subnet.  This subnet should work happily with the power switch as is.  The example below assumes your network uses a different subnet.

Connect the web switch to your network, and try the default admin address, http://192.168.0.100 in your browser.  If it loads, you’ll see a user name and password field, and you’re good to go.  But chances are you have a different network setup, and the page will fail to load.

To get the switch and the Mac to talk, you need to connect the Mac to the switch with an ethernet cable (does not need to be a cross-over cable), change the Mac’s network settings to the WPS’s subnet so they can communicate.  Then, in the switch’s admin page, you can modify the switch’s settings to match your network.  When that’s accomplished, disconnect the cable between the mac and WPS.  Re-connect the WPS to your network, and reconnect the Mac to your network.  Restore the Mac’s original network settings.  Now you should be able to open the WPS admin page using the new settings.

Here’s more detail.
Collect your current network settings from the Mac System Preferences – Network panel.

If you are currently using Wifi, you can just turn off Wifi off, and select the Ethernet settings.  You are going to note your current settings so you can restore them to get back on your network.

jj mbp network settings

Select the advanced button.
Note your current settings:
IPv4 Address  192.168.1.146
Subnet  mask 255.255.255.0
Router  192.168.1.1

Connect an ethernet cable between your Web Power Switch and  your Mac.  With a modern Mac, it doesn’t need to be a crossover cable.

Turn your wifi off, if on,  from the Network settings or from the menu icon.
On your Ethernet settings TCP/IP tab:
Select “Configure iPv4 Manually”
Set IPv4 address to an address on your Web Power Switch subnet eg, 192.168.0.10
Subnet:  255.255.255.0
Router: blank is ok
Set Configure iPv6  to link-local only
Click OK
Click Apply

Open a browser and connect to to the Web Switch admin at 192.168.0.100.  Login with the default user name and password.
Click the Setup link on the left.
Change your password;  it’s recommended, if only to eliminate the annoying beep on startup.  You also may not want strangers to turn your stuff off.
Change network settings to an address on your subnet, for example
192.168.1.100
Subnet 255.255.255.0
router  192.168.1.1
WPS settings after change
You decide if you want the “Same subnet access only” box checked, depending on your needs.
Click Submit, this will update your settings and disconnect you.

Now back in Mac System Prefs network settings, restore your network settings to their original state and click OK and Apply.
Turn on Wifi, if that’s your normal network.
Close and reopen your browser, then test your settings on an external website, then test the Web Power Switch admin page, now at http://192.168.1.100.

How to save man pages from Terminal

One would expect it should be easy to save a man page from terminal for review, and it is, but it doesn’t work the way you expect.

I would expect that “man col > col.txt” should save a nice text file in the home directory, but the results are strange:

COL(1)              BSD General Commands Manual            COL(1)

NNAAMMEE
ccooll — filter reverse line feeds from input

SSYYNNOOPPSSIISS
ccooll [--bbffhhppxx] [--ll _n_u_m]

DDEESSCCRRIIPPTTIIOONN
The ccooll utility filters out reverse (and half reverse) line feeds so that
the output is in the correct order with only forward and half forward
line feeds, and replaces white-space characters with tabs where possible.

Similarly, trying to open the man pages using “man col | open -f” dutifully opens the mangled file in TextEdit.

The repeated characters are fossil behaviors from the days when bolding was accomplished by repeatedly backspacing and overprinting characters.  The col utility is the workaround for this behavior. The man for col says:

The col utility filters out reverse (and half reverse) line feeds so that
the output is in the correct order with only forward and half forward
line feeds, and replaces white-space characters with tabs where possible.

And we need to use the -b option :
-b      Do not output any backspaces, printing only the last character
written to each column position.

So to redirect the man file to a text file, use

man col | col -b > col.txt

which pipes ( | ) the man pages to the col utility, without backspaces, and then redirects it to a text file.  To open the man for col in TextEdit, use

man col | col -b | open -f

(The -f option for open sends to the default text editor.)

If you want the man pages as PDF, try:

man -t [your_command_name_here] | open -f -a Preview

This provides a nice looking output that respects the bolding.  The -t option for man formats the output as PostScript; the -a option for open lets you specify the application (Preview).

Why I love Google, sometimes.

I could not for the life of me think of what QR codes are called.  After a few abortive searches, I gave in and ran the following:

“what are those picture things you aim your phone at and get a link to a web page?”

This totally lame search returned Wikipedia’s QR Code page at the top of the search!

QR Code sample

Xsan 2.3 client won’t mount

I’m having a problem with clients dropping their  xsan volumes, usually on a restart of the client.  The fix I used is below, but  I’d live to have abetter method.  Here’s what I know:
The MDCs are running 10.7.4, as are the clients. 
There is a ghost directory that I have deleted, but it returns from the dead, still x’ed out.
There’s been no change to fiber cabling. 
The wwnn’s on the client match those on the qlogic switch for the client’s zone
All LUN’s are visible on the client via Disk Utility
Mount san volume fails from xsan admin and from Xsan in user prefs.
I’ve restarted the client
I’ve removed the client from the san and then re-added it
I’ve disabled the san pref and re-enabled it.

Both Xsan Admin and cvadmin act strangely.  (Note that I try not to run cvadmin and Xsan admin at the same time, and I don’t intentionally leave them running on both MDCs at the same time.  This has always been a recipe for squirrely behavior.) I believe there is a bug related to a disconnect between cvadmin and the Xsan Admin GUI.

In the Mount list, Xsan Admin shows all clients as “not mounted”

Here’s a terminal session showing cvadmin on the primary MDC:

> 
ohaephqxs002:~ aepadmin$ sudo cvadmin 
Password: 
Xsan Administrator 

Enter command(s) 
For command help, enter “help” or “?”. 

List FSS 

File System Services (* indicates service is in control of FS): 
 1> Video[0]             located on ohaephqxs001.aepsc.meta:49257 (pid 232) 
 2>*Video[1]             located on ohaephqxs002.aepsc.meta:55017 (pid 35048) 

Select FSM “Video” 

Admin Tap Connection to FSM failed: [errno 54]: Connection reset by peer 
FSM may have too many connections active. 

Cannot select FSS “Video” 
> 

What fixes it is to force a failover of the MDCs.  This immediately mounts the problem client and  restores cvadmin to normal behavior; also Xsan Admin correctly shows clients as mounted.