Posts Tagged ‘Scripting’

Automating iSync and Bluetooth

With some pointers from friends I’ve beaten applescript into submission and coded up a script to automate the bluetooth iSync syncing with my phone.

The script opens up System Preferences and enables Bluetooth, then launches iSync and instructs it to sync. It will then leave Bluetooth in the state that it found it in.

The code is below for anyone who might find it handy :) Oh and for the record this is written to run on Mac OS X Leopard.

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
display dialog "Sync?" buttons {"Ok", "Cancel"} default button 2
if the button returned of the result is "Cancel" then
    stop
end if
 
tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preferences.Bluetooth"
 
        tell application "System Events" to tell window "Bluetooth" of process "System Preferences"
            set was_on to the value of checkbox "On"
 
            if value of checkbox "On" is equal to 0 then
                tell checkbox "On" to click
            end if
        end tell
    end tell
 
    tell application "iSync"
        synchronize
 
        repeat while (syncing is true)
            delay 5
        end repeat
        set syncStatus to sync status
 
        if syncStatus is not 2 then
            if syncStatus = 3 then
                set syncStatus to "completed with warnings"
            else if syncStatus = 4 then
                set syncStatus to "completed with errors"
            else if syncStatus = 5 then
                set syncStatus to "last sync cancelled"
            else if syncStatus = 6 then
                set syncStatus to "last sync failed to complete"
            else if syncStatus = 7 then
                set syncStatus to "never synced"
            end if
        display dialog "syncStatus: " & syncStatus
    end if
    quit
end tell
 
if was_on is equal to 0 then
    tell application "System Preferences"
        set current pane to pane "com.apple.preferences.Bluetooth"
 
        tell application "System Events" to tell window "Bluetooth" of process "System Preferences"
            if value of checkbox "On" is 1 then
                tell checkbox "On" to click
            end if
        end tell
        quit
    end tell
end if
Posted: September 27th, 2009
Categories: Programming
Tags: , , , , , ,
Comments: No Comments.