📜 ⬆️ ⬇️

AppleScript vs iSync (MacOS X 10.4)

Prehistory


iSync - a program to synchronize your address book, calendar with your phone. I honestly got tired of opening iSync and synchronizing everything with the phone with my hands.

Task


write an application / script / workflow that would automatically synchronize. Ideal - sync after making changes to iCal / AddressBook

Decision


AppleScript was chosen for implementation. The algorithm works as follows:

Immediately there was a problem - how to keep track of changes in the calendar? since at first glance there are no simple tracking tools, I had to stop at closing the calendar application.
Problem number two - iSync does not have a command to synchronize with the phone, there is only a command to synchronize the databases. exit - simulate pressing a shortcut with the active application window.
Problem number three - it is not clear how to track the synchronization process (ended or not).
There was an option to write an Always Run application (some kind of AppleScript server) that would listen to iCal events and activate the synchronization function. upon first consideration, the varinant was dropped, since the construction
on quit application "iCal"
-- some code here
end quit

did not work correctly - intercepted event exit server application.
since everything is so bad, it was decided to write a workflow for Automator and embed its call in the Finder (the implementation claims to be super-perverted).
The code that calls iSync and simulates a button click:
tell application "iSync"
activate
end tell

tell application "System Events"
if UI elements enabled then
tell process "iSync"
set frontmost to true
end tell
-- iSync doesn't have a command to sync with device, so gettin' tricky with it
keystroke "t" using command down
keystroke return
end if
end tell

then Automator was opened, and a single block workflow was created - the execution of AppleScript, which was later integrated into the Finder's context menu.
Listing script in automator
on run {input, parameters}
-- routine that forces iSync to sync with cell phone
tell application "iSync"
(activate)
(synchronize)
end tell

tell application "System Events"
if UI elements enabled then
tell process "iSync"
set frontmost to true
end tell
-- iSync doesn't have a command to sync with device, so gettin' tricky with it
keystroke "t" using command down
keystroke return
end if
delay 30
quit application "iSync"
end tell
return input
end run


The task was solved, but not brilliantly. If you know a more elegant solution - write in the comments.

')

Source: https://habr.com/ru/post/48045/


All Articles