Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Skype Functions


  • Please log in to reply
3 replies to this topic
iPhilip
  • Members
  • 138 posts
  • Last active: Sep 12 2015 06:27 PM
  • Joined: 03 Jun 2010

Folks,

 

I have been using the following Skype functions for several months and wanted to share them with you in case you might find them useful. First, let me start with an example script:

InitSkypeAPI()
MsgBox % GetSkypeUserStatus()
SetSkypeUserStatus("DND")
MsgBox % GetSkypeUserStatus()
SetSkypeUserStatus("Online")
MsgBox % GetSkypeUserStatus()
ExitApp

#Include SkypeFunctions.ahk

Here are the functions:

;
; AutoHotkey: v1.1.11+
; Language:   English
; Platform:   Win7
; Author:     iPhilip
;
; Set of functions to interface with Skype
;
; InitSkypeAPI()                 - Initializes the Skype API
; TermSkypeAPI()                 - Terminates  the Skype API
; RestartSkypeAPI()              - Restarts    the Skype API
; GetSkypeUserStatus()           - Returns the Skype user status, e.g. DND
; SetSkypeUserStatus(UserStatus) - Sets the Skype user status, e.g. SetSkypeUserStatus("DND")
;
; These functions assume that Skype is running
; All functions return the following values for ErrorLevel:
;
;    0 - if the command is successful
;    1 - otherwise
;
; Global variables: WM_COPYDATA, hSKYPE_Gui, hSKYPE_API, SkypeString
;
; References: http://www.autohotkey.com/forum/topic36712.html
;             http://www.autohotkey.com/forum/topic42247.html
;             http://www.autohotkey.com/forum/topic93614.html
;             https://developer.skype.com/public-api-reference#COMMANDS
;
; --------------------------------------------------------------------------

;---------------------------------------------------------------------------
; This function initializes the Skype API
;---------------------------------------------------------------------------

InitSkypeAPI()
{
   Global WM_COPYDATA, hSKYPE_Gui, hSKYPE_API, SkypeString

   ;------------------------------------------------------------------------
   ; Define constant
   ;------------------------------------------------------------------------

   WM_COPYDATA := 0x4a

   ;------------------------------------------------------------------------
   ; Register Skype messages
   ;------------------------------------------------------------------------

   SkypeControlAPIDiscover := DllCall("RegisterWindowMessage", Str,"SkypeControlAPIDiscover")
   SkypeControlAPIAttach   := DllCall("RegisterWindowMessage", Str,"SkypeControlAPIAttach")

   ;------------------------------------------------------------------------
   ; Obtain id of a Gui window to receive API messages and then hide it
   ;------------------------------------------------------------------------

   Gui, SkypeAPI:Show, Hide   ; Setup an invisible Gui window
   Gui, SkypeAPI:+LastFound
   hSKYPE_Gui := WinExist()   ; Get its id

   ;------------------------------------------------------------------------
   ; Register functions for incoming messages
   ;------------------------------------------------------------------------

   OnMessage(SkypeControlAPIAttach, "SkypeAttach")
   OnMessage(WM_COPYDATA, "SkypeReceive")

   ;------------------------------------------------------------------------
   ; Connect Gui to Skype and wait for acknowledgment
   ;------------------------------------------------------------------------

   hSKYPE_API := 0
   SkypeString =
   SendMessage, SkypeControlAPIDiscover, hSKYPE_Gui,,, ahk_id 0xFFFF
   Loop 100
   {
      Loop, PARSE, SkypeString, `n, `r
         if (SubStr(A_LoopField, 1, 10) = "USERSTATUS")
         {
            ErrorLevel := 0
            Return
         }
      Sleep 100
   }
   ErrorLevel := 1
   Return
}


;---------------------------------------------------------------------------
; This function terminates the Skype API
;---------------------------------------------------------------------------

TermSkypeAPI()
{
   Gui, SkypeAPI:Destroy   ; Destroy the existing Gui window
   Return
}


;---------------------------------------------------------------------------
; This function restarts the Skype API
;---------------------------------------------------------------------------

RestartSkypeAPI()
{
   TermSkypeAPI()
   InitSkypeAPI()
   Return
}


;---------------------------------------------------------------------------
; This function gets the Skype user status
;---------------------------------------------------------------------------

GetSkypeUserStatus()
{
   Global hSKYPE_API, SkypeString

   ;------------------------------------------------------------------------
   ; If a connection with Skype was not made properly return
   ;------------------------------------------------------------------------

   if !hSKYPE_API
   {
      ErrorLevel := 1
      Return
   }

   ;------------------------------------------------------------------------
   ; Check for the current user status
   ;------------------------------------------------------------------------

   SkypeString =
   WriteApi("#AHK GET USERSTATUS")
   Loop 100
   {
      Loop, PARSE, SkypeString, `n, `r
         if (SubStr(A_LoopField, 1, 15) = "#AHK USERSTATUS")
         {
            ErrorLevel := 0
            Return SubStr(A_LoopField, 17)
         }
      Sleep 100
   }
   ErrorLevel := 1
   Return
}


;---------------------------------------------------------------------------
; This function sets the Skype user status
;---------------------------------------------------------------------------

SetSkypeUserStatus(UserStatus)
{
   Global hSKYPE_API, SkypeString

   ;------------------------------------------------------------------------
   ; If a connection with Skype was not made properly return
   ;------------------------------------------------------------------------

   if !hSKYPE_API
   {
      ErrorLevel := 1
      Return
   }

   ;------------------------------------------------------------------------
   ; Check for the current user status
   ;------------------------------------------------------------------------

   CurrentUserStatus := GetSkypeUserStatus()
   if ErrorLevel
      Return
   if (UserStatus = CurrentUserStatus)
   {
      ErrorLevel := 0
      Return
   }

   ;------------------------------------------------------------------------
   ; Set the user status
   ;------------------------------------------------------------------------

   SkypeString =
   Result := WriteApi("#AHK SET USERSTATUS " UserStatus)
   if (Result = FAIL OR Result = 0)
   {
      ErrorLevel := 1
      Return
   }
   Loop 100
   {
      Loop, PARSE, SkypeString, `n, `r
         if (A_LoopField = "USERSTATUS " UserStatus)
         {
            ErrorLevel := 0
            Return
         }
      Sleep 100
   }
   ErrorLevel := 1
   Return
}


;---------------------------------------------------------------------------
; This function writes text to the Skype API by sending WM_COPYDATA message
;---------------------------------------------------------------------------

WriteApi(StringToSend)
{
   Global WM_COPYDATA, hSKYPE_Gui, hSKYPE_API

   ;------------------------------------------------------------------------
   ; Fill the CopyDataStruct structure to be sent by WM_COPYDATA
   ;------------------------------------------------------------------------

   If A_IsUnicode
   {
      VarSetCapacity(ANSI, StrLen(StringToSend)+1, 0)                 ; Create a variable to store the ANSI string
      StrPut(StringToSend, &ANSI, "CP0")
   }
   else
      ANSI := StringToSend
   VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)                     ; Set up the structure's memory area
   NumPut(StrLen(StringToSend)+1, CopyDataStruct, A_PtrSize, "Int")   ; OS requires that this be done
   NumPut(&ANSI, CopyDataStruct, 2*A_PtrSize, "Ptr")                  ; Set lpData to point to the ANSI string itself

   ;------------------------------------------------------------------------
   ; Issue WM_COPYDATA message to Skype API
   ;------------------------------------------------------------------------

   Prev_DetectHiddenWindows := A_DetectHiddenWindows
   Prev_TitleMatchMode := A_TitleMatchMode
   DetectHiddenWindows On
   SetTitleMatchMode 2
   SendMessage, WM_COPYDATA, hSKYPE_Gui, &CopyDataStruct,, ahk_id %hSKYPE_API%
   DetectHiddenWindows %Prev_DetectHiddenWindows%
   SetTitleMatchMode %Prev_TitleMatchMode%
   Return ErrorLevel
}


;---------------------------------------------------------------------------
; This function is invoked upon incoming SkypeControlAPIAttach message
;---------------------------------------------------------------------------

SkypeAttach(wParam, lParam, msg, hwnd)
{
   Global hSKYPE_API

   if (lParam = 0)
      hSKYPE_API := wParam
   Return
}


;---------------------------------------------------------------------------
; This function is invoked upon incoming WM_COPYDATA message
;---------------------------------------------------------------------------

SkypeReceive(wParam, lParam)
{
   Global SkypeString

   if ((DataSize := NumGet(lParam+0, A_PtrSize, "Int")) > 0    ; Make sure the size of data lpData points to is greater zero
   && (DataPtr := NumGet(lParam+0, 2*A_PtrSize, "Ptr")) > 0)   ; Make sure lpData is not NULL
      SkypeString .= StrGet(DataPtr, DataSize, "CP0") . "`n"   ; Append the new data to the current string
   Return true
}

Enjoy!

 

- iPhilip


Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)


i_Punish
  • Members
  • 8 posts
  • Last active: Nov 30 2013 09:56 AM
  • Joined: 16 Nov 2013

I tried the Set user status script and it isn't working for me. it says: 

Error: Call to nonexistent function.

Specifically: GetSkypeUserStatus()

 

Am I missing something?

 

Thanks.

 

Edit: Fixed it.



Hellnation
  • Members
  • 1 posts
  • Last active: Feb 11 2015 08:15 PM
  • Joined: 08 Feb 2015

Hi Philip,

 

Very cool library, I got this working to set my status message of my primary skype account. However, I run 2 copies of Skype, I'd like to be able to set the status of both copies running simulataneously. (I don't really need to make one "dnd" and the other "online", if that's easy to do I don't mind thought)

 

I run my second copy of skype with the /secondary swtich.

 

Any easy way I could do this? :)

 

Thanks!



iPhilip
  • Members
  • 138 posts
  • Last active: Sep 12 2015 06:27 PM
  • Joined: 03 Jun 2010

Hi Hellnation,

 

Interesting problem. Unfortunately, I cannot think of a way for a single app to control two instances of Skype. Btw, the link in the above code is no longer valid. This link contains an archived version of the Skype API.

 

- iPhilip


Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)