1 (изменено: Dworkin, 2016-02-11 07:24:18)

Тема: AHK: Отправка на почту мешает обновлять текстовый документ

#Persistent
WaitUrl := "http://www.mts.ua/ua/online-services/send-sms"
WaitUrl2 := "http://www.mts.ua/ua/online-services/send-sms/"

SetTimer, waitsendsms, 500
SetTimer, whenmatch, off
return

waitsendsms:
	sURL := GetActiveBrowserURL()
	If (sURL != "")

	if (WaitUrl = sURL or WaitUrl2 = sURL)
      {
          FileAppend, 1111`n, log.txt
          SetTimer, whenmatch, 500
          SetTimer, waitsendsms, off
      }
  else
      {

      }
return


whenmatch:
	sURL2 := GetActiveBrowserURL()
	If (sURL2 != "")

	if (WaitUrl = sURL2 or WaitUrl2 = sURL2)
      {

      }
   else
      {
          sFrom     := "...@gmail.com"
          sTo       := "...@gmail.com"
          sSubject  := "пытаюсь прикрепить"
          sBody     := "Привет. Вышло или нет?"
          sAttach   := "Тут путь к текстовому файлу указывать полный иначе не сработает"

          sServer   := "smtp.gmail.com" ; specify your SMTP server
          nPort     := 465 ; 25
          bTLS      := True ; False
          nSend     := 2   ; cdoSendUsingPort
          nAuth     := 1   ; cdoBasic
          sUsername := "...@gmail.com"
          sPassword := "..."
          pmsg :=   ComObjCreate("CDO.Message")
          pcfg :=   pmsg.Configuration
          pfld :=   pcfg.Fields

          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") := nSend
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") := 60
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") := sServer
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") := nPort
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") := bTLS
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") := nAuth
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") := sUsername
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") := sPassword
          pfld.Update()

          pmsg.From := sFrom
          pmsg.To := sTo
          pmsg.Subject := sSubject
          pmsg.TextBody := sBody
          Loop, Parse, sAttach, |, %A_Space%%A_Tab%
            pmsg.AddAttachment(A_LoopField)
          pmsg.Send()



          SetTimer, waitsendsms, 500
          SetTimer, whenmatch, off
   }
return






 
GetActiveBrowserURL() {
	WinGetClass, sClass, A
	If sClass In Chrome_WidgetWin_1,Chrome_WidgetWin_0,Maxthon3Cls_MainFrm
		Return GetBrowserURL_ACC(sClass)
	Else
		Return GetBrowserURL_DDE(sClass) ; empty string if DDE not supported (or not a browser)
}
 
; "GetBrowserURL_DDE" adapted from DDE code by Sean, (AHK_L version by maraskan_user)
; Found at http://autohotkey.com/board/topic/17633-/?p=434518
 
GetBrowserURL_DDE(sClass) {
	WinGet, sServer, ProcessName, % "ahk_class " sClass
	StringTrimRight, sServer, sServer, 4
	iCodePage := A_IsUnicode ? 0x04B0 : 0x03EC ; 0x04B0 = CP_WINUNICODE, 0x03EC = CP_WINANSI
	DllCall("DdeInitialize", "UPtrP", idInst, "Uint", 0, "Uint", 0, "Uint", 0)
	hServer := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", sServer, "int", iCodePage)
	hTopic := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "WWW_GetWindowInfo", "int", iCodePage)
	hItem := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "0xFFFFFFFF", "int", iCodePage)
	hConv := DllCall("DdeConnect", "UPtr", idInst, "UPtr", hServer, "UPtr", hTopic, "Uint", 0)
	hData := DllCall("DdeClientTransaction", "Uint", 0, "Uint", 0, "UPtr", hConv, "UPtr", hItem, "UInt", 1, "Uint", 0x20B0, 

"Uint", 10000, "UPtrP", nResult) ; 0x20B0 = XTYP_REQUEST, 10000 = 10s timeout
	sData := DllCall("DdeAccessData", "Uint", hData, "Uint", 0, "Str")
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hServer)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hTopic)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hItem)
	DllCall("DdeUnaccessData", "UPtr", hData)
	DllCall("DdeFreeDataHandle", "UPtr", hData)
	DllCall("DdeDisconnect", "UPtr", hConv)
	DllCall("DdeUninitialize", "UPtr", idInst)
	csvWindowInfo := StrGet(&sData, "CP0")
	StringSplit, sWindowInfo, csvWindowInfo, `" ; " ; this comment is here just to fix a syntax highlighting bug
	Return sWindowInfo2
}
 
GetBrowserURL_ACC(sClass) {
	global nWindow, accAddressBar
	If (nWindow != WinExist("ahk_class " sClass)) ; reuses accAddressBar if it's the same window
	{
		nWindow := WinExist("ahk_class " sClass)
		accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindow))
	}
	Try sURL := accAddressBar.accValue(0)
	If (sURL == "") {
		WinGet, nWindows, List, % "ahk_class " sClass ; In case of a nested browser window as in CoolNovo
		If (nWindows > 1) {
			accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindows2))
			Try sURL := accAddressBar.accValue(0)
		}
	}
	If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Chromium-based browsers omit "http://"
		sURL := "http://" sURL
	Return sURL
}
 
; "GetAddressBar" based in code by uname
; Found at http://autohotkey.com/board/topic/103178-/?p=637687
 
GetAddressBar(accObj) {
	Try If ((accObj.accName(0) != "") and IsURL(accObj.accValue(0)))
		Return accObj
	Try If ((accObj.accName(0) != "") and IsURL("http://" accObj.accValue(0))) ; Chromium omits "http://"
		Return accObj
	For nChild, accChild in Acc_Children(accObj)
		If IsObject(accAddressBar := GetAddressBar(accChild))
			Return accAddressBar
}
 
IsURL(sURL) {
	Return RegExMatch(sURL, "^(?<Protocol>https?|ftp)://(?<Domain>(?:[\w-]+\.)+\w\w+)(?::(?<Port>\d+))?/?(?<Path>(?:[^:/?# 

]*/?)+)(?:\?(?<Query>[^#]+)?)?(?:\#(?<Hash>.+)?)?$")
}
 
; The code below is part of the Acc.ahk Standard Library by Sean (updated by jethrow)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
 
Acc_Init()
{
	static h
	If Not h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromWindow(hWnd, idObject = 0)
{
	Acc_Init()
	If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity

(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?

0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
	Return ComObjEnwrap(9,pacc,1)
}
Acc_Query(Acc) {
	Try Return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
Acc_Children(Acc) {
	If ComObjType(Acc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	Else {
		Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
		If DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "Int",0, "Int",cChildren, "Ptr",VarSetCapacity

(varChildren,cChildren*(8+2*A_PtrSize),0)*0+&varChildren, "Int*",cChildren)=0 {
			Loop %cChildren%
				i:=(A_Index-1)*(A_PtrSize*2+8)+8, child:=NumGet(varChildren,i), Children.Insert(NumGet

(varChildren,i-8)=9?Acc_Query(child):child), NumGet(varChildren,i-8)=9?ObjRelease(child):
			Return Children.MaxIndex()?Children:
		} Else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
}

Помогите пожалуйста.
Запускаю скрипт. Скрипт ждет когда зайдут на определенный сайт(указан в самом верху кода). Когда захожу на сайт то создается текстовый документ. Затем когда закрываю сайт или он не активен(перешел на другую вкладку) то текстовый файл посылается на почту.
Далее опять когда захожу на сайт в текстовый документ должен дописаться текст, но этого не происходит. Не могу понять почему.
Притом если убрать отправку файла на почту то все работает нормально.

2

Re: AHK: Отправка на почту мешает обновлять текстовый документ

Добавил в код что бы перед отправкой на почту скопировать созданный текстовый документ в другое место и только что скопированный документ отправлять.
В итоге в оригинале текстового документа текст добавляется, а в копии(которую отправляю) нет.
После отправки документа на почту попытался вручную скопировать и  не получилось, выбило ошибку что папка уже используется...

#Persistent
WaitUrl := "http://www.mts.ua/ua/online-services/send-sms"
WaitUrl2 := "http://www.mts.ua/ua/online-services/send-sms/"

SetTimer, waitsendsms, 500
SetTimer, whenmatch, off
return

waitsendsms:
	sURL := GetActiveBrowserURL()
	If (sURL != "")

	if (WaitUrl = sURL or WaitUrl2 = sURL)
      {
          FileAppend, 1111`n, log.txt
          SetTimer, whenmatch, 500
          SetTimer, waitsendsms, off
      }
  else
      {

      }
return


whenmatch:
	sURL2 := GetActiveBrowserURL()
	If (sURL2 != "")

	if (WaitUrl = sURL2 or WaitUrl2 = sURL2)
      {

      }
   else
      {
          FileCopy, D:\к\Недоделаные\firefox\log.txt, D:\Games\, 1


          sFrom     := "...@gmail.com"
          sTo       := "...@gmail.com"
          sSubject  := "пытаюсь прикрепить"
          sBody     := "Привет. Вышло или нет?"
          sAttach   := "Тут путь к скопированому текстовому документу"

          sServer   := "smtp.gmail.com" ; specify your SMTP server
          nPort     := 465 ; 25
          bTLS      := True ; False
          nSend     := 2   ; cdoSendUsingPort
          nAuth     := 1   ; cdoBasic
          sUsername := "...@gmail.com"
          sPassword := "..."
          pmsg :=   ComObjCreate("CDO.Message")
          pcfg :=   pmsg.Configuration
          pfld :=   pcfg.Fields

          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") := nSend
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") := 60
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") := sServer
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") := nPort
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") := bTLS
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") := nAuth
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") := sUsername
          pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") := sPassword
          pfld.Update()

          pmsg.From := sFrom
          pmsg.To := sTo
          pmsg.Subject := sSubject
          pmsg.TextBody := sBody
          Loop, Parse, sAttach, |, %A_Space%%A_Tab%
            pmsg.AddAttachment(A_LoopField)
          pmsg.Send()



          SetTimer, waitsendsms, 500
          SetTimer, whenmatch, off
   }
return






 
GetActiveBrowserURL() {
	WinGetClass, sClass, A
	If sClass In Chrome_WidgetWin_1,Chrome_WidgetWin_0,Maxthon3Cls_MainFrm
		Return GetBrowserURL_ACC(sClass)
	Else
		Return GetBrowserURL_DDE(sClass) ; empty string if DDE not supported (or not a browser)
}
 
; "GetBrowserURL_DDE" adapted from DDE code by Sean, (AHK_L version by maraskan_user)
; Found at http://autohotkey.com/board/topic/17633-/?p=434518
 
GetBrowserURL_DDE(sClass) {
	WinGet, sServer, ProcessName, % "ahk_class " sClass
	StringTrimRight, sServer, sServer, 4
	iCodePage := A_IsUnicode ? 0x04B0 : 0x03EC ; 0x04B0 = CP_WINUNICODE, 0x03EC = CP_WINANSI
	DllCall("DdeInitialize", "UPtrP", idInst, "Uint", 0, "Uint", 0, "Uint", 0)
	hServer := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", sServer, "int", iCodePage)
	hTopic := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "WWW_GetWindowInfo", "int", iCodePage)
	hItem := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "0xFFFFFFFF", "int", iCodePage)
	hConv := DllCall("DdeConnect", "UPtr", idInst, "UPtr", hServer, "UPtr", hTopic, "Uint", 0)
	hData := DllCall("DdeClientTransaction", "Uint", 0, "Uint", 0, "UPtr", hConv, "UPtr", hItem, "UInt", 1, "Uint", 0x20B0, 

"Uint", 10000, "UPtrP", nResult) ; 0x20B0 = XTYP_REQUEST, 10000 = 10s timeout
	sData := DllCall("DdeAccessData", "Uint", hData, "Uint", 0, "Str")
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hServer)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hTopic)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hItem)
	DllCall("DdeUnaccessData", "UPtr", hData)
	DllCall("DdeFreeDataHandle", "UPtr", hData)
	DllCall("DdeDisconnect", "UPtr", hConv)
	DllCall("DdeUninitialize", "UPtr", idInst)
	csvWindowInfo := StrGet(&sData, "CP0")
	StringSplit, sWindowInfo, csvWindowInfo, `" ; " ; this comment is here just to fix a syntax highlighting bug
	Return sWindowInfo2
}
 
GetBrowserURL_ACC(sClass) {
	global nWindow, accAddressBar
	If (nWindow != WinExist("ahk_class " sClass)) ; reuses accAddressBar if it's the same window
	{
		nWindow := WinExist("ahk_class " sClass)
		accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindow))
	}
	Try sURL := accAddressBar.accValue(0)
	If (sURL == "") {
		WinGet, nWindows, List, % "ahk_class " sClass ; In case of a nested browser window as in CoolNovo
		If (nWindows > 1) {
			accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindows2))
			Try sURL := accAddressBar.accValue(0)
		}
	}
	If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Chromium-based browsers omit "http://"
		sURL := "http://" sURL
	Return sURL
}
 
; "GetAddressBar" based in code by uname
; Found at http://autohotkey.com/board/topic/103178-/?p=637687
 
GetAddressBar(accObj) {
	Try If ((accObj.accName(0) != "") and IsURL(accObj.accValue(0)))
		Return accObj
	Try If ((accObj.accName(0) != "") and IsURL("http://" accObj.accValue(0))) ; Chromium omits "http://"
		Return accObj
	For nChild, accChild in Acc_Children(accObj)
		If IsObject(accAddressBar := GetAddressBar(accChild))
			Return accAddressBar
}
 
IsURL(sURL) {
	Return RegExMatch(sURL, "^(?<Protocol>https?|ftp)://(?<Domain>(?:[\w-]+\.)+\w\w+)(?::(?<Port>\d+))?/?(?<Path>(?:[^:/?# 

]*/?)+)(?:\?(?<Query>[^#]+)?)?(?:\#(?<Hash>.+)?)?$")
}
 
; The code below is part of the Acc.ahk Standard Library by Sean (updated by jethrow)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
 
Acc_Init()
{
	static h
	If Not h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromWindow(hWnd, idObject = 0)
{
	Acc_Init()
	If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity

(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?

0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
	Return ComObjEnwrap(9,pacc,1)
}
Acc_Query(Acc) {
	Try Return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
Acc_Children(Acc) {
	If ComObjType(Acc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	Else {
		Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
		If DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "Int",0, "Int",cChildren, "Ptr",VarSetCapacity

(varChildren,cChildren*(8+2*A_PtrSize),0)*0+&varChildren, "Int*",cChildren)=0 {
			Loop %cChildren%
				i:=(A_Index-1)*(A_PtrSize*2+8)+8, child:=NumGet(varChildren,i), Children.Insert(NumGet

(varChildren,i-8)=9?Acc_Query(child):child), NumGet(varChildren,i-8)=9?ObjRelease(child):
			Return Children.MaxIndex()?Children:
		} Else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
}

3

Re: AHK: Отправка на почту мешает обновлять текстовый документ

Решил проблему. Просто добавил после отправки на почту вот это

pmsg :=  ""

Короче освободил объект