Skip to content

Instantly share code, notes, and snippets.

@jNizM
Last active May 16, 2019 17:32
Show Gist options
  • Save jNizM/b55fa713eba5fa6edde6 to your computer and use it in GitHub Desktop.
Save jNizM/b55fa713eba5fa6edde6 to your computer and use it in GitHub Desktop.
[AHK] Dropbox Class (GetPath / GetStatus / GetPID)
; License: Unlicense (http://unlicense.org/)
/*
* Function DropBox.GetPath()
*
* Parameters
* Account: can be "personal" or "business" (personal if omitted)
*
* Return Values
* if the function succeeds ==> PATH
* if the function fails ==> 0
*/
MsgBox % DropBox.GetPath()
MsgBox % DropBox.GetPath("business")
/*
* Function DropBox.GetStatus()
*
* Parameters
* ConvToTxt: can be 0 or 1 (0 if omitted)
*
* Return Values
* Folder is not in Dropbox ==> 0 [NOT_IN_DROPBOX]
* Dropbox is up to Date ==> 1 [UP_TO_DATE]
* Sync in Progress ==> 2 [SYNCHRONIZING]
* Dropbox is not running ==> 99 [NOT_RUNNING]
*/
MsgBox % DropBox.GetStatus()
MsgBox % DropBox.GetStatus(True)
/*
* Function DropBox.GetPID()
*
* Return Values
* if the function succeeds ==> PID
* if the function fails ==> 0
*/
MsgBox % DropBox.GetPID()
; ===============================================================================================================================
Class Dropbox
{
GetPath(account := "personal")
{
static DB_Path_A := A_AppData "\Dropbox\info.json"
static DB_Path_B := "C:\Users\" A_Username "\AppData\Local\Dropbox\info.json"
if !(IsObject(File := FileOpen(DB_Path_A, "r", "UTF-8"))) && !(IsObject(File := FileOpen(DB_Path_B, "r", "UTF-8")))
return False
line := File.ReadLine(), File.Close()
return (path := this.JSON(line)[account].path) ? path : False
}
GetStatus(ConvToTxt := False)
{
static DB_Status := { 0: "NOT_IN_DROPBOX", 1: "UP_TO_DATE", 2: "SYNCHRONIZING", 99: "NOT_RUNNING" }
static RequestInfo := 0x3048302, RequestType := 1
if !(this.GetPath())
return DB_Status[0]
if !(this.GetPID())
return DB_Status[99]
ProcessId := DllCall("GetCurrentProcessId"), ThreadId := DllCall("GetCurrentThreadId")
if !(DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId))
return "*ProcessIdToSessionId [" A_LatError "]"
PipeName := "\\.\pipe\DropboxPipe_" SessionId
SizeIn := VarSetCapacity(BuffIn, 16 + 524)
NumPut(RequestInfo, BuffIn, 0, "Int")
NumPut(ProcessId, BuffIn, 4, "Int")
NumPut(ThreadId, BuffIn, 8, "Int")
NumPut(RequestType, BuffIn, 12, "Int")
StrPut(this.GetPath(), &BuffIn + 16, 524//2, "UTF-16")
SizeOut := VarSetCapacity(BuffOut, 16382, 0)
if !(DllCall("CallNamedPipe", "Str", PipeName, "Ptr", &BuffIn, "UInt", SizeIn, "Ptr", &BuffOut, "UInt", SizeOut, "UInt*", BytesRead, "UInt", 1000))
return "*CallNamedPipe [" A_LatError "]"
return (ConvToTxt) ? DB_Status[StrGet(&BuffOut + 4, BytesRead - 5, "CP0")] : StrGet(&BuffOut + 4, BytesRead - 5, "CP0")
}
GetPID()
{
static DB_Proc := "Dropbox.exe"
Process, Exist, % DB_Proc
return (ErrorLevel) ? ErrorLevel : False
}
JSON(i) ; thx to Bentschi
{
if (RegExMatch(i, "s)^__chr(A|W):(.*)", m))
{
VarSetCapacity(b, 4, 0), NumPut(m2, b, 0, "Int")
return StrGet(&b, 1, (m1 = "A") ? "CP28591" : "UTF-16")
}
if (RegExMatch(i, "s)^__str:((\\""|[^""])*)", m))
{
str := m1
for p, r in {b:"`b", f:"`f", n:"`n", 0:"", r:"`r", t:"`t", v:"`v", "'":"'", """":"""", "/":"/"}
str := RegExReplace(str, "\\" p, r)
while (RegExMatch(str, "s)^(.*?)\\x([0-9a-fA-F]{2})(.*)", m))
str := m1 this.JSON("__chrA:0x" m2) m3
while (RegExMatch(str, "s)^(.*?)\\u([0-9a-fA-F]{4})(.*)", m))
str := m1 this.JSON("__chrW:0x" m2) m3
while (RegExMatch(str, "s)^(.*?)\\([0-9]{1,3})(.*)", m))
str := m1 this.JSON("__chrA:" m2) m3
return RegExReplace(str, "\\\\", "\")
}
str := [], obj := []
while (RegExMatch(i, "s)^(.*?[^\\])""((\\""|[^""])*?[^\\]|)""(.*)$", m))
str.Insert(this.JSON("__str:" m2)), i := m1 "__str<" str.maxIndex() ">" m4
while (RegExMatch(RegExReplace(i, "\s+", ""), "s)^(.*?)(\{|\[)([^\{\[\]\}]*?)(\}|\])(.*)$", m))
{
a := (m2 = "{") ? 0 : 1, c := m3, i := m1 "__obj<" ((obj.maxIndex() + 1) ? obj.maxIndex() + 1 : 1) ">" m5, tmp := []
while (RegExMatch(c, "^(.*?),(.*)$", m))
tmp.Insert(m1), c := m2
tmp.Insert(c), tmp2 := {}, obj.Insert(cobj := {})
for k, v in tmp
{
if (RegExMatch(v, "^(.*?):(.*)$", m))
tmp2[m1] := m2
else
tmp2.Insert(v)
}
for k, v in tmp2
{
for x, y in str
k := RegExReplace(k, "__str<" x ">", y), v := RegExReplace(v, "__str<" x ">", y)
for x, y in obj
v := RegExMatch(v, "^__obj<" x ">$") ? y : v
cobj[k] := v
}
}
return obj[obj.maxIndex()]
}
}
; ===============================================================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment