I'd like to access Mac application data (Address Book, Mail) from Ruby for a Rails application.
Everything on this answer appears to be dead. I tried installing RubyOSA , which I presume fails because of the wrong Ruby version (I'm coming over from Python and my knowledge is rudimentary). appscript is no longer supported. Likewise RubyCocoa. In any event, I don't want to create cocoa applications ( RubyMotion ).
Can anyone advise how I fire up eg a mac Address Book client in Ruby, and read and write to it?
Many thanks.
As you say, many of the options are outdated and will not work anymore. However, doing this in pure Ruby will be hard I think. I'll rather suggest you make your ruby code able to execute some AppleScripts you could add to your project. Like for example the first AppleScript code example I've added bellow. The script will dump the Address Book data to a file. vCard format ( https://en.wikipedia.org/wiki/VCard ).
Then look into this gem, which then again can read the file created by the AppleScript in your ruby code. https://github.com/sam-github/vpim
Making ruby execute AppleScript files, which again dumps or read data to and from a temporary file is not a optimal solution, but it should do the work. The other options would be far more technical and would most likely include ruby extension writing or worse.
If you need to edit data in Address Book, I suggest you look at the second AppleScript code example bellow. Probably the best way to solve this is to make templates of edit scripts, and make ruby change the desired values in it and then execute the script. You can also see more advanced examples here: http://www.macosxtips.co.uk/index_files/bulk-edit-address-book-contacts.php
To execute the scripts, you use the "osacript" command. Either use a file as a argument like "osacript example.scpt". Or you can do it inline:
osascript -e 'tell application "Contacts" to quit'
Dump data from Address Book:
set myBackupName to "AddressBook.vcf"
-- Add timestamp and Documents path
set myTimeStamp to (year of (current date)) & (month of (current date) as number) & (day of (current date))
set myBackupPath to the (path to the documents folder as string) & myTimeStamp & "-" & myBackupName as string
-- Remove any existing back up file created today
tell application "Finder"
if exists (file myBackupPath) then
delete file myBackupPath -- move to trash
end if
end tell
-- To work on Mac OS X 10.4 - 10.7, change "Contacts" to "Address Book".
tell application "Contacts"
-- Create an empty file
set myBackupFile to (open for access file myBackupPath with write permission)
repeat with per in people
write (vcard of per as text) to myBackupFile
end repeat
-- Close the file
close access myBackupFile
end tell
Change data in Address Book:
tell application "Address Book"
repeat with i from 1 to (count every person)
set phoneProperties to properties of phones of person i
repeat with j from 1 to (count of phoneProperties)
if value of item j of phoneProperties contains "020 6704 3205" then
set value of item j of phones of person i to "020 1523 6843"
end if
end repeat
end repeat
save
end tell
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.