Sending Data to Windows


For reasons I won’t go into I had to get some data imported into a popular Window app. Sadly the app doesn’t have good import abilities. Were I a com hacker I might have been able to do something on Windows. However I’m primarily a Mac guy and all my data was in an Excel spreadsheet. Enter the hack of Python, GUI scripting and appscript. I had my Windows program open in Parallels.

Here’s a nice little template I used for this. Some of it might have been handled better using cut and paste. But for reasons I won’t go into I wanted it to appear to the Windows app as if I were typing all the data from my Excel spreadsheet into the Windows app.

I suspect this may come in handy for a few of you who run OSX and Windows simultaneously and need to be able to have some level of scripting in Windows. What’s nice about Windows (and sometimes frustrating about Macs) is that you can do almost all things via the keyboard. This means utilizing Applescript/Appscript you can script a lot in Windows from the Mac.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
 
# Types data from Excel into a Windows app.
 
import sys, string, time
from appscript import *
 
 
def typestring(s):
    # Types string s using GUI scripting
 
    for i in range(0,len(s)):
        c = s[i]
        if c in string.ascii_uppercase:
            app(u'System Events').keystroke(s[i], using=k.shift_down)
        else:
            app(u'System Events').keystroke(s[i])
        #print s[i]
 
def backtab():
    # Tab backwards
    app(u'System Events').keystroke("\t", using=k.shift_down)
 
def tab():
    # Tab forward
    app(u'System Events').keystroke("\t")
 
def cr():
    # Carriage Return
    app(u'System Events').keystroke(u'\r')
 
def main():
    MS = app('/Applications/Microsoft Office 2008/Microsoft Excel')
    PD = app(u'Parallels Desktop')
 
    PD.activate()
 
    # get the current row in Excel
    row = MS.selection.first_row_index.get()
 
    # date
    data = MS.cells[ "A" + str(row) ].value.get()
    s = data.strftime("%m/%d/%y")  # convert date to string
    typestring(s)
 
    tab() # to check number
 
    tab() # to payee
 
    data = MS.cells[ "C" + str(row) ].value.get()
    typestring(data)
 
    tab() # to payment
 
    data = MS.cells[ "B" + str(row) ].value.get()
    s = str(data)
    typestring(s)
 
    cr()
 
    # advance to next row in Excel
    MS.rows[row+1].select()
 
if __name__ == '__main__':
 
	main()
 
    # change to 0 for success, 1 for (partial) failure
	sys.exit(0)

The way I do this is to assign it to a key command using iKey. (Although there are lots of other ways to assign a script to a key command including using Automator and Services) The above assumes I have my cursor in the right place in Windows and then enters the data.

Related posts:

  1. Displaying a SalesForce Contact’s Emails in Mail
  2. Dual Pane Finder Windows
  3. Using Python to Cleanup Excel
  4. Address Book to Numbers
  5. Script New Document in Numbers
  6. The Many Versions of Windows
  7. Proximity Address Search Script Revisited
  8. Quit All Applications
  1. No comments yet.
(will not be published)