2718.us blog » applescript http://2718.us/blog Miscellaneous Technological Geekery Tue, 18 May 2010 02:42:55 +0000 en hourly 1 http://wordpress.org/?v=3.0.4 A New Trick for When the OS X Screensaver Password Window Hangs http://2718.us/blog/2009/05/19/a-new-trick-for-when-the-os-x-screensaver-password-window-hangs/ http://2718.us/blog/2009/05/19/a-new-trick-for-when-the-os-x-screensaver-password-window-hangs/#comments Wed, 20 May 2009 03:38:35 +0000 2718.us http://2718.us/blog/?p=148 I have multiple computers, so I can easily ssh into my desktop when the screensaver password is hanging and won’t let me log in.

The not-so-new trick has been to use AppleScript on the command line to not-so-harshly close some applications (run osascript, then tell application "program name" to quit, followed by ctrl-D (EOF).

The new trick is to sudo kill SecurityAgent, sudo run SecurityAgent, put the machine to sleep, then wake it back up (necessary to get SecurityAgent to put up a new password window).

]]>
http://2718.us/blog/2009/05/19/a-new-trick-for-when-the-os-x-screensaver-password-window-hangs/feed/ 0
XML-RPC and Mac Programming, Revisited http://2718.us/blog/2009/04/26/xml-rpc-and-mac-programming-revisited/ http://2718.us/blog/2009/04/26/xml-rpc-and-mac-programming-revisited/#comments Sun, 26 Apr 2009 05:59:02 +0000 2718.us http://2718.us/blog/?p=139 I might have been wrong, or at least not entirely right, when I said that AppleScript’s XML-RPC was doing something screwy with UTF8-encoded responses to XML-RPC requests.  I’m not sure if it’s LiveJournal (and other sites based on their code), or if it’s something inherent in XML-RPC, but whether I make the XML-RPC calls in AppleScript (with its built-in mechanism for calling XML-RPC), in Python (with xmlrpclib), or in Objective-C/Cocoa (using the XML-RPC framework from here), things that I was expecting to be UTF8 strings were instead coming through as binary data that needed to be decoded.

Beyond that point, however, AppleScript was severely lacking in that the form in which that data was stored made it entirely unusable–AppleScript couldn’t convert it, couldn’t pass it off to an Objective-C method, etc.  As suggested in my previous post, there was a way around it, and messy though it was, I went about implementing that fix and by and large it worked (though it exposed another minor bug elsewhere).  But it really bothered me.

So I went back to looking at trying to integrate Python code into my tangled web of AppleScript and Objective-C, since XML-RPC is fairly easy in Python, though not quite as easy as in AppleScript.  And, eventually, I succeeded in integrating a class written in Python into the program (documentation on using the PyObjC bridge in this direction is woefully inadequate), using a less ineligant means of fixing the binary UTF8 data—

unicode(theResult.data,'utf-8')

(and Python also allowed me to generically recurse through the entire return structure, which wasn’t possible in Applescript).  Unfortunately, this version was substantially slower than the broken-Unicode version and not particularly any faster (perhaps slower) than the AppleScript-fixed Unicode version.

This led me to look for a way to do the XML-RPC stuff in Objective-C.  Now, mind you, the single thing that enabled me to even think about writing a client for LJ for Mac was seeing just how easy AppleScript XML-RPC calls were.  While I didn’t particularly want to try Python, the XML-RPC calls there weren’t that much harder.  But going to Objective-C for XML-RPC…  that’s a fundamental change in the program.  At least, to me.

I did a lot of Googling and found that there are actually a few XML-RPC frameworks for Objective-C/Cocoa (the one I used by Eric Czarny, the one from Brent Simmons, the Mulle one, XMLRPCObjC, SOPE).  Supposedly, there’s a way to do it with Apple’s own Cocoa stuff, but the documentation is woefully inadequate (none of the frameworks have amazing and wonderful documentation, but Apple’s documentation is bad) and almost every mention of it that I found on mailing lists and discussion boards said it was broken.  In the end, my framework choice was largely dictated by licensing, though there were also some issues with usability and dependencies.  As with AppleScript and Python, the UTF8 strings weren’t coming through as strings, but as NSData objects, which are fairly easy to convert with

[[NSString alloc] initWithData:theObject encoding:NSUTF8StringEncoding]

Recursing through the entire returned structure wasn’t particularly any harder in Objective-C than in Python.

The best part is that the resulting client with Objective-C-based-XML-RPC feels faster than the non-Unicode AppleScript-based-XML-RPC client.  In vaguely-objective tests (determine a set of steps that constitute a test and record the total time for just the XML-RPC calls in those steps, run the test several times under each app, compare times), the new version is measurably faster than the old version.

Bottom lines: (1) expect a new version of asLJ in the next few days, as soon as I get feedback from my early testers; (2) expect another post or two about other things I’ve learned in rewriting the XML-RPC aspect of asLJ in Objective-C.

]]>
http://2718.us/blog/2009/04/26/xml-rpc-and-mac-programming-revisited/feed/ 0
AppleScript’s XML-RPC Doesn’t Get Along with UTF8 http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/ http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/#comments Sun, 15 Feb 2009 00:53:02 +0000 2718.us http://2718.us/blog/?p=131 While the ease of making XML-RPC calls in AppleScript is wonderful for, say, writing a LiveJournal Client in mostly AppleScript Studio, it seems to be doing something really messed up with UTF8 strings returned by the server—they come into AppleScript as raw data objects, which it seems can’t be cast into any other type and can’t be passed easily into a Cocoa method to convert them. The easiest way to properly decode them seems to be the following:

  1. if class of theReturnedValue is "data" then
  2.  try
  3.   (* this will fail on a data object and then we will pull the (hex) bytes out as text
  4.   and bring them back as a utf8 string object *)
  5.   theReturnedValue as text
  6.  on error errmess – extract the data from the error message
  7.   set bytesString to text ((offset of "«" in errmess) + 10) thru ((offset of "»" in errmess) - 1) of errmess
  8.   set theReturnedValue to (run script "«data utf8" & bytesString & "»")
  9.  end try
  10. end if

This checks the class of the returned value and, if it’s a raw data object, attempts to cast it as text which raises an error, then extracts the string of hexadecimal values from the error message and puts it into a proper UTF8 object, making everything happy again.

If anyone wants to tell me I’m wrong and there’s a simpler fix, I’d love to hear it, since this is essentially unworkable.

]]>
http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/feed/ 1
Dynamic URLs for XML-RPC Calls in AppleScript http://2718.us/blog/2009/02/12/dynamic-urls-for-xml-rpc-calls-in-applescript/ http://2718.us/blog/2009/02/12/dynamic-urls-for-xml-rpc-calls-in-applescript/#comments Thu, 12 Feb 2009 16:42:38 +0000 2718.us http://2718.us/blog/?p=123 I started working on asLJ after I came across this. One of the problems that I quickly ran into was that the URLs in the
tell application "<url>" to call xmlrpc ...

bits had to be hard-coded. That is, AppleScript didn’t like it when I tried to assemble the URL string on the fly. It took me a while to come up with a workaround, which should slightly impact the speed of the call, but doesn’t seem to make a noticeable difference. Here’s my generic handler for making LJ-based server XML-RPC calls:

– make a LiveJournal-type XML-RPC call to serverString for the method methodName with the parameters in parameterArray
  1. on callLJraw(serverString, methodName, parameterArray)
  2.     run script "on run {paramArray}
  3.                 tell application \"http://" & serverString & "/interface/xmlrpc\" to call xmlrpc ¬
  4.                     {method name:\"LJ.XMLRPC.\" & \"" & methodName & "\", parameters:{paramArray}}
  5.             end run" with parameters {parameterArray}
  6.     return result
  7. end callLJraw
]]>
http://2718.us/blog/2009/02/12/dynamic-urls-for-xml-rpc-calls-in-applescript/feed/ 0
asLJ: a Mac OS X 10.5+ LiveJournal Client http://2718.us/blog/2009/02/09/aslj/ http://2718.us/blog/2009/02/09/aslj/#comments Mon, 09 Feb 2009 08:24:18 +0000 2718.us http://2718.us/blog/?p=121 asLJ is a new client for Macs running Leopard that easily handles multiple accounts on LiveJournal and other LJ-based sites and facilitates cross-posting across accounts. Release notes and download link are in [info]aslj_client. The community for users is [info]aslj_users.

(As it is very LJ-centric, most of the information about it will be over at LJ, in the two places linked above, but there is a page for it here, as well.)

]]>
http://2718.us/blog/2009/02/09/aslj/feed/ 0
SSH Tunneling on a Mac http://2718.us/blog/2008/06/13/ssh-tunneling-on-a-mac/ http://2718.us/blog/2008/06/13/ssh-tunneling-on-a-mac/#comments Fri, 13 Jun 2008 22:55:27 +0000 2718.us http://2718.us/blog/?p=45 Since my employer’s wireless network is unencrypted and since I use other open WiFi networks with some frequency, I’ve gotten in the habbit of tunneling everything through SSH, using the SOCKS5 proxy mechanism built in to SSH.  In WinXP, there’s a nice little program called Tunnelier that makes the setup of the tunnel simple and it reconnects automatically, so the tunneling part is virtually automatic (even though proxy setup is still tricky and/or annoying).

On the Mac, however, I have tried several programs and never really been happy.  So I wrote a little AppleScript that not only sets up the SSH tunnel, but also deals with switching my location to set the system’s network settings to use the proxy (the code is after the cut).  Combine this with System Proxy for Firefox and all my application traffic goes through the SSH tunnel.  Note also that if you’re using a SOCKS5 proxy with Firefox, you probably want to set it to do DNS lookups through the proxy.

This script also stores the info for creating the tunnel (server, login, pw) in the keychain.

Here’s the “library” script where I put the functions to do the underlying work (since I have various different tunnels I use):

  1. on startTunnel(targetServer) –returns PID of ssh for killing later
  2.  tell application "Keychain Scripting"
  3.  
  4.   set sshTunnelKeys to every Internet key of current keychain whose (name is "autoSSHTunnel") and (server is targetServer)
  5.  
  6.   if sshTunnelKeys is {} then
  7.    set sshKey to my makeSSHKeyWithServer(targetServer)
  8.   else
  9.    set sshKey to item 1 of sshTunnelKeys
  10.   end if
  11.   set user to account of sshKey
  12.   set passwd to password of sshKey as string
  13.   set sshHost to server of sshKey as string
  14.  end tell
  15.  
  16.  set sshCommand to "ssh -fND 9999 " & user & "@" & sshHost
  17.  
  18.  set expectScript to "spawn " & sshCommand & "
  19. expect assword
  20. send \"" & passwd & "\\n\"
  21. sleep 1"
  22.  
  23.  do shell script "/usr/bin/expect -c '" & expectScript & "' &>/dev/null &"
  24.  
  25.  set tries to 0
  26.  repeat
  27.   set tries to tries + 1
  28.   try
  29.    set sshPIDstring to (do shell script "sleep 1;bash -c 'ps ax -o pid,tt,command | grep \"??\" | grep \"" & sshCommand & "\" | grep -v grep | grep -v expect'")
  30.    set sshPID to first word of sshPIDstring
  31.    set gotPID to true
  32.   on error
  33.    set gotPID to false
  34.   end try
  35.   if gotPID then
  36.    exit repeat
  37.   end if
  38.   if tries > 10 then
  39.    exit repeat
  40.   end if
  41.  end repeat
  42.  if gotPID then
  43.   return sshPID
  44.  else
  45.   return false
  46.  end if
  47. end startTunnel
  48.  
  49. on setUseTunnel()
  50.  do shell script "scselect 'Use SOCKS5 Proxy on localhost:9999'"
  51. end setUseTunnel
  52.  
  53. on clearUseTunnel()
  54.  do shell script "scselect 'Automatic'"
  55. end clearUseTunnel
  56.  
  57. on stopTunnel(pid)
  58.  do shell script "kill " & pid
  59. end stopTunnel
  60.  
  61. on makeSSHKeyWithServer(targetServer)
  62.  tell application "Keychain Scripting"
  63.   repeat
  64.    set acctBox to display dialog "Enter your SSH login for host " & targetServer & ":" default answer "" buttons {"Cancel", "OK"} default button 2
  65.    set myAcct to the text returned of acctBox
  66.    set myButton to the button returned of acctBox
  67.    if myButton is "Cancel" then
  68.     –quit
  69.    else
  70.     if myAcct is not "" then
  71.      exit repeat
  72.     else
  73.      display dialog "bad login"
  74.     end if
  75.    end if
  76.   end repeat
  77.   repeat
  78.    set passBox to display dialog "Enter your password:" default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
  79.    set myPass to the text returned of passBox
  80.    set myButton to the button returned of passBox
  81.    if myButton is "Cancel" then
  82.     –quit
  83.    else
  84.     if myPass is not "" then
  85.      exit repeat
  86.     else
  87.      display dialog "can't use blank passwd"
  88.     end if
  89.    end if
  90.   end repeat
  91.   set newSSHKey to make new Internet key with properties {name:"autoSSHTunnel", account:myAcct, password:myPass, server:targetServer, authentication:default, protocol:SSH}
  92.  end tell
  93.  return newSSHKey
  94. end makeSSHKeyWithServer

And here’s the actual script I run.

  1. property Lib : (path to scripts folder from user domain as text) & "Script Library:"
  2. property sshTunnelLib : load script Lib & "ssh_tunnel.scpt" as alias
  3.  
  4. sshTunnelLib's setUseTunnel()
  5.  
  6. set sshPID to sshTunnelLib's startTunnel("fqdn.of.your.server")
  7. if sshPID is not false then
  8.  set noPIDtxt to ""
  9.  set buttonTxt to "Kill SSH and Exit"
  10. else
  11.  set noPIDtxt to " (but couldn't get PID)"
  12.  set buttonTxt to "Exit"
  13. end if
  14.  
  15. display dialog "SSH-tunneled SOCKS5 proxy running on localhost:9999" & noPIDtxt buttons {buttonTxt}
  16.  
  17. if sshPID is not false then
  18.  sshTunnelLib's stopTunnel(sshPID)
  19. end if
  20.  
  21. sshTunnelLib's clearUseTunnel()
]]>
http://2718.us/blog/2008/06/13/ssh-tunneling-on-a-mac/feed/ 0