Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I logout iMessage with AppleScript?

Hi guys, How can I logout or login my iMessage accounts with AppleScript? I need to swap accouts quickly.


this script not work. the logout finished event is not happen, and the account is not be logout.


tell application "Messages"

set serviceidtogetidoffirstservice

set serviceName to get name of first service

log out of serviceserviceName

end tell

on logout finishedforserviceName

log in service "E:xxxx@gmail.com"

end logout finished

OS X Mountain Lion (10.8.1)

Posted on Jul 1, 2013 8:32 AM

Reply
8 replies

Jul 1, 2013 1:56 PM in response to Forest_Lin

HI,


So you have several Apple IDs that you want to use separately in Messages as the iMessage ID.


The Manual way is to open Messages Menu > Preferences > Accounts and go to the iMessage one.

Once the use the Sign Out button.

Then you re-enter an Apple ID to institute the change.


If you open the Messages Directory in AppleScript Editor you will find that "Service" only refers to ones that contain "Buddies" and "Chats" (I.e. not iMessages)


service n [inh. item] : A service that can be logged in from this system

elements

contains buddies, chats; contained by application.

properties

id (text, r/o) : A guid identifier for this service.

name (text) : The name of this service as defined in Account preferences description field

enabled (boolean) : Is the service enabled?

connection status (disconnecting/connected/connecting/disconnected, r/o) : The connection status for this account.

status (away/available/offline/invisible) : My status on this service.

status message (text) : My status message, visible to other people on this service while I am online.

service type (AIM/Bonjour/Jabber/iMessage, r/o) : The type of protocol for this service

responds to

log out, log in, send.


You will see it does name iMessages as a service one line from the bottom though.

Having said that I am not aware of where this info is pulled from.

The ID (the guid number) of Buddy List type accounts are held in com.apple.ichat.plist and the various service .plist (as in com.apple.ichat.AIM.plist for example)

I am not aware of any one .plist that hold both the Buddy List type ones and the iMessages one.


r/o is read only.


I notice at the top of the Messages Suite that Log In and Log Out are separated by spaces as I have written them here.

Having said that Message Event Handlers list them as one word like you have them.


Have you tried running the Script with the Event option highlighted in the bottom section so you can see the process as it runs ?



User uploaded file
9:56 PM Monday; July 1, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

Jul 1, 2013 2:13 PM in response to Ralph-Johns-UK

Addition.



It does not seem to like the work Finished in the Logout Finished part.


Removing that section and Running the rest reports in Apple Script that the Apple ID have been Logged out of Service 1 (which happens to be my iMessages service)

However nothing happens in Messages.


The Events look a little off as wellUser uploaded file


I was expecting it to get the Name/Description of the account as it appears in the list in the Accounts pane in Preferences.


As per (from the Above quote)

name (text) : The name of this service as defined in Account preferences description field

I understand the iMessages account must be the top of the list for this to work.


I do not know enough about AppleScript to say why it is reporting on the ID or Name of the Logged in account rather than the Description name the Dictionary suggests it should be.




User uploaded file
10:13 PM Monday; July 1, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

Jul 6, 2013 7:38 AM in response to Ralph-Johns-UK

Thanks for replay!

I just want to change my apple id, but the log in and log out does't work. so finally, I use UI Event, just simulate human click ui, and that works!


But here is another problem:

I want to send a message to a buddy, use this script.

tell application "Messages"

set serviceName to name of 1st service whose service type is iMessage

set theBuddy to buddyhisAppleID of serviceserviceName


sendstrMsgtotheBuddy

endtell


if the buddy is in my buddy list ,this code works fine. But if hisAppleID is a stranger to me, it will be error: can't get buddy id "xxxxxxxxxxxxxxxxxxxxxx". if you type the stranger's apple id in Messages UI, and send him a message, that works fine.


My question is, how to add a stranger to my buddy list with AppleScript?

Jul 6, 2013 8:58 AM in response to Forest_Lin

Forest_Lin wrote:


Hi guys, How can I logout or login my iMessage accounts with AppleScript? I need to swap accouts quickly.


this script not work. the logout finished event is not happen, and the account is not be logout.


tell application "Messages"

set serviceidtogetidoffirstservice

set serviceName to get name of first service

log out of serviceserviceName

end tell

on logout finishedforserviceName

log in service "E:xxxx@gmail.com"

end logout finished

Nothing ever calls logout finished. The script is finished before it ever sees the event.

Jul 6, 2013 9:09 AM in response to Forest_Lin

Forest_Lin wrote:


Thanks for replay!

I just want to change my apple id, but the log in and log out does't work. so finally, I use UI Event, just simulate human click ui, and that works!


But here is another problem:

I want to send a message to a buddy, use this script.

tell application "Messages"

set serviceName to name of 1st service whose service type is iMessage

set theBuddy to buddyhisAppleID of serviceserviceName


sendstrMsgtotheBuddy

endtell


if the buddy is in my buddy list ,this code works fine. But if hisAppleID is a stranger to me, it will be error: can't get buddy id "xxxxxxxxxxxxxxxxxxxxxx". if you type the stranger's apple id in Messages UI, and send him a message, that works fine.


My question is, how to add a stranger to my buddy list with AppleScript?

You are just getting a "buddy" that doesn't exist. You have to "make new" to create an object. However, all of the properties for a buddy are read only. So, I don't see a way to create a new buddy in Applescript.


You might want to ask all of these questions in the Mac OS X Technologies forum or at MacScripter.net.


Also, take a look here: http://www.brownbatterystudios.com/sixthings/2013/05/28/os-x-messages-auto-recon nect-script/

It's not what you want to do, but it shows how you handle those events. Those events will only be seen by the script that is set in the logout events of Messages Preferences.

Jul 6, 2013 11:19 AM in response to Forest_Lin

Hi Forest_Lin,


I am going to leave you in Barney's hands as he has a better understanding of AppleScript than I do.


Your latest Script though, takes ideas for the iChat "side" of Messages and tries to apply them to the iMessages account from what I can see.


Messages can have 5 account types.

In order that they have been added to the App

Bonjour (was Rendezvous in early versions). This account is always there even if not active

AIM - the original External Service that iChat 1 could do.

Jabber - added a t iChat 3

Yahoo - Added at iChat 6

iMessages added at Mountain Lion.


Bonjour has a Buddy list but cannot add Buddies.

The Buddies are the Users on other Macs on your LAN that have the Bonjour account Active in their Messages or iChat and it pulls their Address Card (My Card) and "broadcasts" this as their Buddy Name.


AIM, Jabber (plus their variants) and Yahoo also have Buddy lists that you can add Buddies to.


The iMessages Account uses the names in your Contact App when a Phone Number or email is listed (there are then checks to see if that is an iMessages registered ID).

There is no Buddy list.


I can't see from the current Dictionary for Messages that much has been added to facilitate the inclusion of iMessages.


However in the Interface (Messages) window you can type an Apple ID or iPhone number directly into the "To" spot and then use the items as a Drop down to select the "From" to make it an iMessages conversation.

(You can SMS to certain U.S. Phones using an AIM account this way)

As Barney says, the interface may be simpler.




User uploaded file
7:13 PM Saturday; July 6, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously


Message was edited by: Ralph Johns (UK)

How can I logout iMessage with AppleScript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.