iChat scripting...

Hello, obviously this is my first post, and I will also say this, I apologize if I sound like a complete idiot to you people. I have just started working with AppleScript recently and I’m just trying to learn at this point.

Right now, I’m trying to make an application that will send an away message in iChat whenever I get a message. Now I know, I could just use the away-message feature, but there are compatability issues between iChat and the actual AIM client in that my away message does not actually get sent to anyone, but they can read it if they access my profile. I would rather the message be sent to them, because I grow tired of having a bunch of windows open asking, “Where are you?”

I have a script that allows me to input a message and an account name and then it sends that message to the account name, however, I do not know how to get the account name from a recently opened window and store it.

Basically, I need a way to get the account name from the little “half window” that pops up when you recieve a message in iChat and then I need to send a message to that account and close the window afterwards. And it also needs to be continuously running, so it needs an idle command as well.

Is there any way for me to accomplish this task?

Model: PowerBook G4
AppleScript: I would assume the latest…
Browser: Safari 412.2
Operating System: Mac OS X (10.4)

Ok, so I just found out that there is a “Click at { x,y }” command in System Events…

I was thinking that I could have it click on the little box that appears whenever somebody initially sends you a message. Then, I could use the “Keystroke” command to type in the message followed by an & return/enter (i’m not sure which) and presto, it would be done. except, i would have to set it up so that it would then close that window, and it would probably have to click there every so often as well.

However, I don’t fully understand how to figure out what the X,Y coordinates are. I mean, I could guess around because I would imagine that they go by pixels or they are determined by the resolution setting, but I’m not sure. I also don’t know where the origin (0,0) would be…

Any help?

this is an adaptaion of a previous script of mine…

be forwarned, if the person has 2+ AIM handles that are logged in, it could send to the wrong one

Please let me know if you find this useful, and can I see the finished product, it sounds intriguing.

property test_to_send : "I am rubber, you are glue. Whatever you say bounces off of me and sticks to you."

property seconds_between_checks : 1 --second

property already_seen : {}

on idle

	set windas to (name of windows of application "iChat")
	repeat with i from 1 to number of items in windas
		set this_item to item i of windas
		if this_item contains "Chat with " then
			if already_seen does not contain this_item then
				set already_seen to already_seen & this_item
				set nom to (text items 11 thru (count of text items in this_item) of this_item)
				
			end if
		end if
	end repeat
	
	
	tell application "Address Book"
		set addBnoms to name of every person
		if addBnoms contains nom then
			set evpson to every person
			repeat with i from 1 to number of items in evpson
				set this_item to item i of evpson
				if name of this_item is nom then
					set sns to value of every AIM Handle of this_item
					exit repeat
				end if
			end repeat
		end if
	end tell
	
	tell application "iChat"
		repeat with i from 1 to number of items in sns
			set this_item to item i of sns
			try
				send test_to_send to account (this_item) of service 1
				exit repeat
			on error
				--nada
			end try
		end repeat
	end tell
	
	
	return seconds_between_checks
end idle

Well, the problem is that I haven’t even opend Address Book yet. So unless iChat adds names and contact information in to Address Book when I add them in to iChat, then I don’t think it would work… :frowning:

I did copy and paste your script in and gave it a try and nothing happened. I did get some great ideas from your script though which I am about to tinker around with. :slight_smile:

Once I get it working I will definitly post it so you can see what I did.

Thanks for the try and the ideas.

EDIT: I think I found the problem with the script that you submitted. In order for your script to work, I would need to click on the little window that appears before you click on it to chat with them. Otherwise, the words “Chat with…” never appear.

EDIT (again): Well, the part that really interested me was the initial block where it says:

set windas to (name of windows of application "iChat")
repeat with i from 1 to number of items in windas
	set this_item to item i of windas
end repeat

I thought I would play around with that, but either I screwed it up or it’s wrong (probably not the latter). It looks like the “click at…” and “Keystroke” commands are my only hope really.

Oh yeah. I forgot about people you haven’t added. My bad. And I am pretty sure anyone you add gets put in the address book.

try it now…

property test_to_send : "I am rubber, you are glue. Whatever you say bounces off of me and sticks to you."

property seconds_between_checks : 1 --second

property already_seen : {}

on idle
	
	set windas to (name of windows of application "iChat")
	repeat with i from 1 to number of items in windas
		set this_item to item i of windas
		if this_item contains "Chat with " then
			if already_seen does not contain this_item then
				set already_seen to already_seen & this_item
				set nom to (text items 11 thru (count of text items in this_item) of this_item)
				
			end if
		end if
	end repeat
	
	set found to false
	tell application "Address Book"
		set addBnoms to name of every person
		if addBnoms contains nom then
			set evpson to every person
			repeat with i from 1 to number of items in evpson
				set this_item to item i of evpson
				if name of this_item is nom then
					set sns to value of every AIM Handle of this_item
					set found to true
					exit repeat
				end if
			end repeat
		end if
	end tell
	
	if found then
		tell application "iChat"
			repeat with i from 1 to number of items in sns
				set this_item to item i of sns
				try
					send test_to_send to account (this_item) of service 1
					exit repeat
				on error
					--nada
				end try
			end repeat
		end tell
	else
		tell application "iChat"
			try
				send test_to_send to account (this_item) of service 1
			on error
				--nada (person went offline relly fast)
			end try
		end tell
	end if
	
	return seconds_between_checks
end idle

there is a hidden window that still says that. Trust me.

alright, here’s the code that I have so far: it reiles on the fact that the Chat with… window is open and I have sent them a message though…

display dialog "My Away Message" default answer ""
set AwayMessage to text returned of result


tell application "iChat" to activate
tell application "System Events" to keystroke AwayMessage & return

as you can see, it’s a very very simple code. nothing complex at all. The only problem I face now is actually “opening” that Chat with… window.

EDIT: and the buddy list window has to be closed… which isn’t really a problem because it’s probably possible to make an if… then loop that will close that window if it’s open.

send me an IM: chrisjshull

finally works…

property test_to_send : "I am rubber, you are glue. Whatever you say bounces off of me and sticks to you."

property seconds_between_checks : 1 --second

global already_seens

on run
	set already_seens to {}
end run

on idle
	try
		set noms to {}
		set windas to (name of windows of application "iChat")
		repeat with i from 1 to number of items in windas
			set this_item to item i of windas
			if this_item contains "Chat with " then
				if already_seens does not contain this_item then
					set already_seens to already_seens & this_item
					set nomx to (text items 11 thru (count of text items in this_item) of this_item) as text
					
					set end of noms to nomx
					
				end if
			end if
		end repeat
		if noms is {} then
			--display dialog "none"
			error
		end if
		
		repeat with i from 1 to number of items in noms
			set nom to item i of noms
			
			tell application "iChat"
				try
					send test_to_send to account (nom) of service 1
				on error
					--nada (person went offline relly fast)
				end try
			end tell
			
		end repeat
	on error
		--nada
	end try
	return seconds_between_checks
end idle

alright, so i have the script all written and everything, and it works fine when i run it as a script, but for some reason i can’t get the on idle thing to work…

display dialog "My Away Message" default answer ""
set AwayMessage to text returned of result

tell application "iChat" to activate
set windas to (name of windows of application "iChat")
repeat with i from 1 to number of items in windas
	set this_item to item i of windas
	if this_item contains "Chat with " then
		set ScreenName to (text items 11 thru (count of text items in this_item) of this_item) as string
		tell application "iChat" to send AwayMessage to account ScreenName
	end if
end repeat

that script works perfectly. however, when i put it in a “on idle” loop, like this:

property seconds_between_checks : 1
on run
	display dialog "My Away Message" default answer ""
	set AwayMessage to text returned of result
end run

on idle
	try
		tell application "iChat" to activate
		set windas to (name of windows of application "iChat")
		repeat with i from 1 to number of items in windas
			set this_item to item i of windas
			if this_item contains "Chat with " then
				set ScreenName to (text items 11 thru (count of text items in this_item) of this_item) as string
			end if
		end repeat
		
		tell application "iChat" to send AwayMessage to account ScreenName
		tell application "iChat" to close window ("Chat with " & ScreenName)
	end try
	return seconds_between_checks
end idle

it doesn’t work. anyone got any ideas?

Change it to…

property seconds_between_checks : 1
global AwayMessage
on run
	display dialog "My Away Message" default answer ""
	set AwayMessage to text returned of result
end run

on idle
	try
		tell application "iChat" to activate
		set windas to (name of windows of application "iChat")
		repeat with i from 1 to number of items in windas
			set this_item to item i of windas
			if this_item contains "Chat with " then
				set ScreenName to (text items 11 thru (count of text items in this_item) of this_item) as string
			end if
		end repeat
		
		tell application "iChat" to send AwayMessage to account ScreenName
		tell application "iChat" to close window ("Chat with " & ScreenName)
	end try
	return seconds_between_checks
end idle

alright, i’m done with this script, thanks to the help of themacgeek. i will post the script here in case anyone here has any need of it, and also because i’m rather proud of it myself as it’s my first script. :smiley:

anyways, here it is:

property seconds_between_checks : 1
on run
	display dialog "My Away Message" default answer ""
	set AwayMessage to text returned of result
end run

on reopen
	display dialog "My New Away Message" default answer ""
	set AwayMessage to text returned of result
end reopen

on idle
	try
		tell application "iChat" to activate
		set windas to (name of windows of application "iChat")
		repeat with i from 1 to number of items in windas
			set this_item to item i of windas
			if this_item contains "Chat with " then
				set ScreenName to (text items 11 thru (count of text items in this_item) of this_item) as string
			end if
		end repeat
		
		tell application "iChat" to send AwayMessage to account ScreenName
		tell application "iChat" to close window ("Chat with " & ScreenName)
	end try
	return seconds_between_checks
end idle

you’ll notice that i also added an “on reopen” statement. this allows you to change the away message without quitting and restarting the application. again, thanks themacgeek for all your help.