Posted By

TALlama on 01/29/09


Tagged

library Skype messaging instantmessaging


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

steviraab


AppleScript Skype Library


 / Published in: AppleScript
 

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

  1. (* *************************************************************
  2. Some helpful handlers that deal with text
  3. *)
  4. (* Get all the words between the delimiters, as a list. The items do not include the delimiters
  5. Example: allWords of "I am the very model of a script object" between {" "}
  6. # => {"I", "am", "the", "very", "model", "of", "a", "script", "object"}
  7. *)
  8. to allWords of inString between delimiters
  9. set tid to text item delimiters
  10. set text item delimiters to delimiters
  11. set retval to get text items in inString
  12. set text item delimiters to tid
  13. retval
  14. end allWords
  15.  
  16. (* find all instances of "s" in "t" and replace them with "r"
  17. Example: switchText of "Example text" from "e" to "."
  18. # => ".xampl. t.xt"
  19. *)
  20. to switchText of t from s to r
  21. set d to text item delimiters
  22. set text item delimiters to s
  23. set t to t's text items
  24. set text item delimiters to r
  25. tell t to set t to item 1 & ({""} & rest)
  26. set text item delimiters to d
  27. t
  28. end switchText
  29.  
  30. (* Join the items in "stringArray" with "delimiter" and return.
  31. Example: join {"X", "Y", "Z"} between "."
  32. # => "X.Y.Z"
  33. *)
  34. to join of stringArray between delimiter
  35. set d to text item delimiters
  36. set text item delimiters to {} & delimiter
  37. tell stringArray to set retval to item 1 & ({""} & rest)
  38. set text item delimiters to d
  39. retval
  40. end join
  41.  
  42. (* *************************************************************
  43. The actual script
  44. *)
  45.  
  46. (* Get a script object to help with skype commands *)
  47. to makeSkypeHelper(withScriptName)
  48. script SkypeHelper
  49. property scriptName : withScriptName
  50.  
  51. (* Run the "cmd" and return the result, as a list of words (as delimited by spaces)
  52.  
  53. Example: cmd("GET CURRENTUSERHANDLE")
  54. # => {"CURRENTUSERHANDLE", "Skype.User"}
  55.  
  56. If there is an error, this method will detect it and throw a proper AppleScript error.
  57.  
  58. Example: cmd("INVALID CMD")
  59. # => throws error with number 2, message "ERROR 2 Unknown command"
  60. *)
  61. to cmd(cmd)
  62. tell application "Skype" to set response to send command cmd script name scriptName
  63. set retval to allWords of response between {" "}
  64. if first item of retval is equal to "ERROR" then
  65. error response number (item 2 of retval as number)
  66. end if
  67. retval
  68. end cmd
  69.  
  70. (* Create chat object from the results of a cmd("SEARCH xCHATS")
  71. Example: chats from cmd("SEARCH CHATS")
  72. # => {SkypeChat, SkypeChat... }
  73. *)
  74. to chats from response
  75. set response to items 2 thru -1 of response
  76. log response
  77. set chatsArray to {}
  78. repeat with chatid in response
  79. if chatid ends with "," then
  80. set chatid to (characters 1 thru -2 of chatid) as Unicode text
  81. end if
  82. copy makeChat(chatid) to the end of chatsArray
  83. end repeat
  84. chatsArray
  85. end chats
  86.  
  87. (* Get all the recent chats as chat objects
  88. Example: recentChats()
  89. # => {SkypeChat, SkypeChat... }
  90. *)
  91. on recentChats()
  92. chats from cmd("SEARCH RECENTCHATS")
  93. end recentChats
  94.  
  95. (* Find the first chat that has the specified topic, or is a 1:1 dialog with the person named.
  96. Example: findChat("Skype.User")
  97. # => SkypeChat
  98.  
  99. Example: findChat("Chat Topic")
  100. # => SkypeChat
  101.  
  102. If no appropriate chat can be found, returns missing value
  103. *)
  104. to findChat(topicOrSkypeLogin)
  105. set theChats to recentChats()
  106. repeat with chat in theChats
  107. set fn to chat's topic()
  108. if fn is equal to topicOrSkypeLogin then
  109. return chat
  110. end if
  111.  
  112. if chat's status() is equal to "DIALOG" and chat's isSubscribed() then
  113. set members to chat's activeMembers()
  114. if (allWords of members between {" "}) contains topicOrSkypeLogin then
  115. return chat
  116. end if
  117. end if
  118. end repeat
  119. return missing value
  120. end findChat
  121.  
  122. (* Send a chat message to the given target. The target can be a Chat Topic or a Skype user name.
  123. *)
  124. to sendMessage(target, message)
  125. set chat to findChat(target)
  126. if chat is equal to missing value then
  127. set chat to createChat(target)
  128. end if
  129. tell chat to sendMessage(message)
  130. end sendMessage
  131.  
  132. (* Create a new chat with the given target
  133. Example: createChat("skype.user")
  134. # => SkypeChat
  135. *)
  136. to createChat(target)
  137. set response to cmd("CHAT CREATE " & target)
  138. set chatid to item 2 of response
  139. makeChat(chatid)
  140. end createChat
  141.  
  142. (* Make a chat object for the already-existing Skype chat
  143.  
  144. Example: makeChat("#initiating.user/$called.user;123456789abcdef")
  145. # => SkypeChat
  146. *)
  147. to makeChat(withChatID)
  148. script SkypeChat
  149. property parent : SkypeHelper
  150. property chatid : withChatID
  151.  
  152. (* Get the value of the named property
  153. Example: skypeProperty("TOPIC")
  154. # => "Product Roadmap"
  155. *)
  156. on skypeProperty(propertyName)
  157. set response to cmd("GET CHAT " & chatid & " " & propertyName)
  158. set retval to join of (items 4 thru -1 of response) between {" "}
  159. retval
  160. end skypeProperty
  161.  
  162. (* Get the topic of this chat
  163. Example: topic()
  164. # => "Product Roadmap"
  165. *)
  166. on topic()
  167. skypeProperty("TOPIC")
  168. end topic
  169.  
  170. (* Get the status of this chat
  171. Example: status()
  172. # => "Dialog"
  173. *)
  174. on status()
  175. skypeProperty("STATUS")
  176. end status
  177.  
  178. (* Find out if the current user is currently subscribed to this chat.
  179. When subscribed, the user can send messages to the chat and will get messages sent by others.
  180.  
  181. Example: isSubscribed()
  182. # => true
  183. *)
  184. on isSubscribed()
  185. skypeProperty("MYSTATUS") is equal to "SUBSCRIBED"
  186. end isSubscribed
  187.  
  188. (* Get a space-delimited list of all the active members of this chat
  189. Example: activeMembers()
  190. # => "jim.kirk bones.mccoy"
  191. *)
  192. on activeMembers()
  193. skypeProperty("ACTIVEMEMBERS")
  194. end activeMembers
  195.  
  196. (* Send a message to this chat
  197. Example: sendMessage("Hello, world!")
  198. # => "MESSAGE 1234 STATUS SENDING"
  199. *)
  200. on sendMessage(message)
  201. cmd("CHATMESSAGE " & chatid & " " & message)
  202. end sendMessage
  203. end script
  204. end makeChat
  205. end script
  206. return SkypeHelper
  207. end makeSkypeHelper
  208.  
  209. tell makeSkypeHelper("X")
  210. sendMessage("UserName", "Hello, user!")
  211. sendMessage("ChatName", "Hello, chatroom!")
  212. end tell

Report this snippet  

You need to login to post a comment.