Return to Snippet

Revision: 11268
at January 29, 2009 13:59 by TALlama


Initial Code
(* *************************************************************
	Some helpful handlers that deal with text 
*)
(* Get all the words between the delimiters, as a list. The items do not include the delimiters
	Example: allWords of "I am the very model of a script object" between {" "}
			# => {"I", "am", "the", "very", "model", "of", "a", "script", "object"}
*)
to allWords of inString between delimiters
	set tid to text item delimiters
	set text item delimiters to delimiters
	set retval to get text items in inString
	set text item delimiters to tid
	retval
end allWords

(* find all instances of "s" in "t" and replace them with "r"
	Example: switchText of "Example text" from "e" to "."
			# => ".xampl. t.xt"
*)
to switchText of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to item 1 & ({""} & rest)
	set text item delimiters to d
	t
end switchText

(* Join the items in "stringArray" with "delimiter" and return. 
	Example: join {"X", "Y", "Z"} between "."
			# => "X.Y.Z"
*)
to join of stringArray between delimiter
	set d to text item delimiters
	set text item delimiters to {} & delimiter
	tell stringArray to set retval to item 1 & ({""} & rest)
	set text item delimiters to d
	retval
end join

(* *************************************************************
	The actual script
*)

(* Get a script object to help with skype commands *)
to makeSkypeHelper(withScriptName)
	script SkypeHelper
		property scriptName : withScriptName
		
		(* Run the "cmd" and return the result, as a list of words (as delimited by spaces)
			
			Example: cmd("GET CURRENTUSERHANDLE")
					# => {"CURRENTUSERHANDLE", "Skype.User"}
			
			If there is an error, this method will detect it and throw a proper AppleScript error.
			
			Example: cmd("INVALID CMD")
					# => throws error with number 2, message "ERROR 2 Unknown command"
		*)
		to cmd(cmd)
			tell application "Skype" to set response to send command cmd script name scriptName
			set retval to allWords of response between {" "}
			if first item of retval is equal to "ERROR" then
				error response number (item 2 of retval as number)
			end if
			retval
		end cmd
		
		(* Create chat object from the results of a cmd("SEARCH xCHATS")
			Example: chats from cmd("SEARCH CHATS")
					# => {SkypeChat, SkypeChat... }
		*)
		to chats from response
			set response to items 2 thru -1 of response
			log response
			set chatsArray to {}
			repeat with chatid in response
				if chatid ends with "," then
					set chatid to (characters 1 thru -2 of chatid) as Unicode text
				end if
				copy makeChat(chatid) to the end of chatsArray
			end repeat
			chatsArray
		end chats
		
		(* Get all the recent chats as chat objects
			Example: recentChats()
					# => {SkypeChat, SkypeChat... }
		*)
		on recentChats()
			chats from cmd("SEARCH RECENTCHATS")
		end recentChats
		
		(* Find the first chat that has the specified topic, or is a 1:1 dialog with the person named.
			Example: findChat("Skype.User")
					# => SkypeChat
			
			Example: findChat("Chat Topic")
					# => SkypeChat
			
			If no appropriate chat can be found, returns missing value		
		*)
		to findChat(topicOrSkypeLogin)
			set theChats to recentChats()
			repeat with chat in theChats
				set fn to chat's topic()
				if fn is equal to topicOrSkypeLogin then
					return chat
				end if
				
				if chat's status() is equal to "DIALOG" and chat's isSubscribed() then
					set members to chat's activeMembers()
					if (allWords of members between {" "}) contains topicOrSkypeLogin then
						return chat
					end if
				end if
			end repeat
			return missing value
		end findChat
		
		(* Send a chat message to the given target. The target can be a Chat Topic or a Skype user name.
		*)
		to sendMessage(target, message)
			set chat to findChat(target)
			if chat is equal to missing value then
				set chat to createChat(target)
			end if
			tell chat to sendMessage(message)
		end sendMessage
		
		(* Create a new chat with the given target
			Example: createChat("skype.user")
					# => SkypeChat
		*)
		to createChat(target)
			set response to cmd("CHAT CREATE " & target)
			set chatid to item 2 of response
			makeChat(chatid)
		end createChat
		
		(* Make a chat object for the already-existing Skype chat
		
			Example: makeChat("#initiating.user/$called.user;123456789abcdef")
					# => SkypeChat
		*)
		to makeChat(withChatID)
			script SkypeChat
				property parent : SkypeHelper
				property chatid : withChatID
				
				(* Get the value of the named property
					Example: skypeProperty("TOPIC")
							# => "Product Roadmap"
				*)
				on skypeProperty(propertyName)
					set response to cmd("GET CHAT " & chatid & " " & propertyName)
					set retval to join of (items 4 thru -1 of response) between {" "}
					retval
				end skypeProperty
				
				(* Get the topic of this chat
					Example: topic()
							# => "Product Roadmap"
				*)
				on topic()
					skypeProperty("TOPIC")
				end topic
				
				(* Get the status of this chat
					Example: status()
							# => "Dialog"
				*)
				on status()
					skypeProperty("STATUS")
				end status
				
				(* Find out if the current user is currently subscribed to this chat.
					When subscribed, the user can send messages to the chat and will get messages sent by others.
					
					Example: isSubscribed()
							# => true
				*)
				on isSubscribed()
					skypeProperty("MYSTATUS") is equal to "SUBSCRIBED"
				end isSubscribed
				
				(* Get a space-delimited list of all the active members of this chat
					Example: activeMembers()
							# => "jim.kirk bones.mccoy"
				*)
				on activeMembers()
					skypeProperty("ACTIVEMEMBERS")
				end activeMembers
				
				(* Send a message to this chat
					Example: sendMessage("Hello, world!")
						# => "MESSAGE 1234 STATUS SENDING"
				*)
				on sendMessage(message)
					cmd("CHATMESSAGE " & chatid & " " & message)
				end sendMessage
			end script
		end makeChat
	end script
	return SkypeHelper
end makeSkypeHelper

tell makeSkypeHelper("X")
	sendMessage("UserName", "Hello, user!")
	sendMessage("ChatName", "Hello, chatroom!")
end tell

Initial URL


Initial Description
A script library to make dealing with Skype's IM features easier.

Initial Title
AppleScript Skype Library

Initial Tags


Initial Language
AppleScript