Jump to content

Script randomly stops


Recommended Posts

Hello,
 
I had a programmer write up a script for me for photoshop.
 
The workflow is as follows:
 
I select RAW pictures in bridge with labels which creates XMP files and add a number to the end of the shoot folder.
 
I run the script, it reads the folder. Opens the Selected pictures in Photoshop and runs an action over the top of them and then saves as JPEG. Then cuts the saved pictures to another folder.
 
It worked great until we upgraded cameras and the string it was looking for in the XMP was changed by adobe. I then corrected that. But now it seems to run for a while then randomly stop on the Camera RAW window and not progress any further. As a note it always seems to stop around when the program reaches 2.6gb of system memory however I have allocated Photoshop with 12gb out of the 16 GB available.
 
Any advise or help greatly appreciated.

Apologies the code is such length!

#Region
    #AutoIt3Wrapper_Icon=..\app.ico
    #AutoIt3Wrapper_Compression=3
    #AutoIt3Wrapper_Res_Description=PhotoShop Automation etc.: RAW processing
    #AutoIt3Wrapper_Res_Fileversion=0.0.0.1
#EndRegion
#Region Header
#EndRegion Header
#Region Local Variables and Constants
    Dim $msgid[1][6] = [[0, 0, 100, DllCallbackRegister("_queue", "none", ""), 0, "lib10rsZd"]]
    Dim $msgqueue[1][2] = [[0]]
    Const $msg_wm_copydata = 74
    Local $onmessagesexit = OnAutoItExitRegister("OnMessagesExit")
    Local $wmint = 0
    Local $qeint = 0
#EndRegion Local Variables and Constants
#Region Public Functions

    Func _msgreceiverlist()
        Local $wlist = WinList(), $lenght = StringLen($msgid[0][5])
        Dim $rlist[1] = [0]
        For $i = 1 To $wlist[0][0]
            If StringRight($wlist[$i][0], $lenght) = $msgid[0][5] Then
                ReDim $rlist[$rlist[0] + 2]
                $rlist[0] += 1
                $rlist[$rlist[0]] = StringTrimRight($wlist[$i][0], $lenght)
            EndIf
        Next
        Return $rlist
    EndFunc

    Func _msgregister($sidentifier, $sfunction)
        Local $id, $title
        Local $i, $j = 0, $k, $l, $b, $t
        If (NOT IsString($sidentifier)) OR (NOT IsString($sfunction)) OR ($msgid[0][3] = 0) OR (StringStripWS($sidentifier, 8) = "") Then
            Return SetError(1, 0, 0)
        EndIf
        $sfunction = StringStripWS($sfunction, 3)
        $t = StringLower($sidentifier)
        For $i = 1 To $msgid[0][0]
            If StringLower($msgid[$i][1]) = $t Then
                $j = $i
                ExitLoop
            EndIf
        Next
        If $j = 0 Then
            $title = $sidentifier & $msgid[0][5]
            If ($sfunction = "") OR (IsHWnd(_winhandle($title))) Then
                Return SetError(0, 0, 1)
            EndIf
            $id = 1
            Do
                $b = 1
                For $i = 1 To $msgid[0][0]
                    If $msgid[$i][0] = $id Then
                        $id += 1
                        $b = 0
                        ExitLoop
                    EndIf
                Next
            Until $b
            If $msgid[0][0] = 0 Then
                _start()
                If @error Then
                    Return 0
                EndIf
            EndIf
            ReDim $msgid[$msgid[0][0] + 2][6]
            $msgid[$msgid[0][0] + 1][0] = $id
            $msgid[$msgid[0][0] + 1][1] = $sidentifier
            $msgid[$msgid[0][0] + 1][2] = $sfunction
            $msgid[$msgid[0][0] + 1][3] = GUICreate($title)
            $msgid[$msgid[0][0] + 1][4] = 0
            $msgid[$msgid[0][0] + 1][5] = 0
            $msgid[0][0] += 1
            If $msgid[0][0] = 1 Then
                GUIRegisterMsg($msg_wm_copydata, "_WM_COPYDATA")
            EndIf
            Return SetError(0, 0, $id)
        EndIf
        If $sfunction > "" Then
            $msgid[$j][2] = $sfunction
            $id = $msgid[$j][0]
        Else
            $wmint = 1
            $k = 1
            $t = StringLower($msgid[$j][2])
            While $k <= $msgqueue[0][0]
                If StringLower($msgqueue[$k][0]) = $t Then
                    For $i = $k To $msgqueue[0][0] - 1
                        For $l = 0 To 1
                            $msgqueue[$i][$l] = $msgqueue[$i + 1][$l]
                        Next
                    Next
                    ReDim $msgqueue[$msgqueue[0][0]][2]
                    $msgqueue[0][0] -= 1
                    ContinueLoop
                EndIf
                $k += 1
            WEnd
            If $msgid[0][0] = 1 Then
                GUIRegisterMsg($msg_wm_copydata, "")
                _stop()
            EndIf
            GUIDelete($msgid[$j][3])
            For $i = $j To $msgid[0][0] - 1
                For $l = 0 To 5
                    $msgid[$i][$l] = $msgid[$i + 1][$l]
                Next
            Next
            ReDim $msgid[$msgid[0][0]][6]
            $msgid[0][0] -= 1
            $id = 0
            $wmint = 0
        EndIf
        Return SetError(0, 0, $id)
    EndFunc

    Func _msgrelease()
        $wmint = 1
        ReDim $msgqueue[1][2]
        $msgqueue[0][0] = 0
        GUIRegisterMsg($msg_wm_copydata, "")
        For $i = 1 To $msgid[0][0]
            GUIDelete($msgid[$i][3])
        Next
        ReDim $msgid[1][6]
        $msgid[0][0] = 0
        _stop()
        $wmint = 0
        Return SetError(@error, 0, (NOT @error))
    EndFunc

    Func _msgsend($sidentifier, $smessage)
        Local $hwnd, $senderr = False, $aret, $tmessage, $tcopydata
        If (NOT IsString($sidentifier)) OR (NOT IsString($smessage)) OR (StringStripWS($sidentifier, 8) = "") Then
            Return SetError(1, 0, 0)
        EndIf
        $hwnd = _winhandle($sidentifier & $msgid[0][5])
        If $hwnd = 0 Then
            Return SetError(1, 2, 0)
        EndIf
        $tmessage = DllStructCreate("char[" & StringLen($smessage) + 1 & "]")
        DllStructSetData($tmessage, 1, $smessage)
        $tcopydata = DllStructCreate("dword;dword;ptr")
        DllStructSetData($tcopydata, 2, StringLen($smessage) + 1)
        DllStructSetData($tcopydata, 3, DllStructGetPtr($tmessage))
        $aret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hwnd, "int", $msg_wm_copydata, "wparam", 0, "lparam", DllStructGetPtr($tcopydata))
        If @error Then
            $senderr = 1
        EndIf
        $tcopydata = 0
        $tmessage = 0
        If $senderr Then
            Return SetError(1, 0, 0)
        EndIf
        If $aret[0] = -1 Then
            Return SetError(1, -1, 0)
        EndIf
        Return SetError(0, 0, 1)
    EndFunc

    Func _msgtimerinterval($itimerinterval)
        If NOT IsInt($itimerinterval) Then
            Return SetError(1, 0, $msgid[0][2])
        EndIf
        If $itimerinterval = 0 Then
            Return SetError(0, 0, $msgid[0][2])
        EndIf
        If $itimerinterval < 50 Then
            $itimerinterval = 50
        EndIf
        _stop()
        If @error Then
            Return SetError(1, 0, $msgid[0][2])
        EndIf
        $msgid[0][2] = $itimerinterval
        _start()
        If @error Then
            GUIRegisterMsg($msg_wm_copydata, "")
            Return SetError(1, 0, $msgid[0][2])
        EndIf
        Return $msgid[0][2]
    EndFunc

    Func _msgwindowhandle($controlid)
        If NOT IsInt($controlid) Then
            Return 0
        EndIf
        For $i = 1 To $msgid[0][0]
            If $msgid[$i][0] = $msgid Then
                Return $msgid[$i][3]
            EndIf
        Next
        Return 0
    EndFunc

    Func _isreceiver($sidentifier)
        If (NOT IsString($sidentifier)) OR (_winhandle($sidentifier & $msgid[0][5]) = 0) Then
            Return 0
        EndIf
        Return 1
    EndFunc

#EndRegion Public Functions
#Region Internal Functions

    Func _function($hwnd)
        For $i = 0 To $msgid[0][0]
            If $msgid[$i][3] = $hwnd Then
                Return $msgid[$i][2]
            EndIf
        Next
        Return 0
    EndFunc

    Func _message($sfunction, $smessage)
        ReDim $msgqueue[$msgqueue[0][0] + 2][2]
        $msgqueue[$msgqueue[0][0] + 1][0] = $sfunction
        $msgqueue[$msgqueue[0][0] + 1][1] = $smessage
        $msgqueue[0][0] += 1
    EndFunc

    Func _queue()
        If ($wmint = 1) OR ($qeint = 1) OR ($msgqueue[0][0] = 0) Then
            Return
        EndIf
        $qeint = 1
        Local $ret = Call($msgqueue[1][0], $msgqueue[1][1])
        If (@error = 57005) AND (@extended = 48879) Then
        Else
            Local $lenght = $msgqueue[0][0] - 1
            Switch $ret
                Case 0
                    For $i = 1 To $lenght
                        For $j = 0 To 1
                            $msgqueue[$i][$j] = $msgqueue[$i + 1][$j]
                        Next
                    Next
                    ReDim $msgqueue[$lenght + 1][2]
                    $msgqueue[0][0] = $lenght
                Case Else
                    If $lenght > 1 Then
                        _swap(1, 2)
                    EndIf
            EndSwitch
        EndIf
        $qeint = 0
    EndFunc

    Func _start()
        Local $aret
        If $msgid[0][4] = 0 Then
            $aret = DllCall("user32.dll", "int", "SetTimer", "hwnd", 0, "int", 0, "int", $msgid[0][2], "ptr", DllCallbackGetPtr($msgid[0][3]))
            If (@error) OR ($aret[0] = 0) Then
                Return SetError(1, 0, 0)
            EndIf
            $msgid[0][4] = $aret[0]
        EndIf
        Return SetError(0, 0, 1)
    EndFunc

    Func _stop()
        Local $aret
        If $msgid[0][4] > 0 Then
            $aret = DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "int", $msgid[0][4])
            If (@error) OR ($aret[0] = 0) Then
                Return SetError(1, 0, 0)
            EndIf
            $msgid[0][4] = 0
        EndIf
        Return SetError(0, 0, 1)
    EndFunc

    Func _swap($index1, $index2)
        Local $tmp
        For $i = 0 To 1
            $tmp = $msgqueue[$index1][$i]
            $msgqueue[$index1][$i] = $msgqueue[$index2][$i]
            $msgqueue[$index2][$i] = $tmp
        Next
    EndFunc

    Func _winhandle($stitle)
        Local $wlist = WinList()
        $stitle = StringLower($stitle)
        For $i = 1 To $wlist[0][0]
            If StringLower($wlist[$i][0]) = $stitle Then
                Return $wlist[$i][1]
            EndIf
        Next
        Return 0
    EndFunc

    Func _wm_copydata($hwnd, $msgid, $wparam, $lparam)
        If ($wmint = 1) Then
            Return -1
        EndIf
        Local $function = _function($hwnd)
        If $function > "" Then
            Local $tcopydata = DllStructCreate("dword;dword;ptr", $lparam)
            Local $tmsg = DllStructCreate("char[" & DllStructGetData($tcopydata, 2) & "]", DllStructGetData($tcopydata, 3))
            _message($function, DllStructGetData($tmsg, 1))
            Return 0
        EndIf
        Return "GUI_RUNDEFMSG"
    EndFunc

    Func onmessagesexit()
        GUIRegisterMsg($msg_wm_copydata, "")
        _stop()
        DllCallbackFree($msgid[0][3])
        Call($onmessagesexit)
    EndFunc

#EndRegion Internal Functions
Global Const $fw_dontcare = 0
Global Const $fw_thin = 100
Global Const $fw_extralight = 200
Global Const $fw_ultralight = 200
Global Const $fw_light = 300
Global Const $fw_normal = 400
Global Const $fw_regular = 400
Global Const $fw_medium = 500
Global Const $fw_semibold = 600
Global Const $fw_demibold = 600
Global Const $fw_bold = 700
Global Const $fw_extrabold = 800
Global Const $fw_ultrabold = 800
Global Const $fw_heavy = 900
Global Const $fw_black = 900
Global Const $cf_effects = 256
Global Const $cf_printerfonts = 2
Global Const $cf_screenfonts = 1
Global Const $cf_noscriptsel = 8388608
Global Const $cf_inittologfontstruct = 64
Global Const $logpixelsx = 88
Global Const $logpixelsy = 90
Global Const $ansi_charset = 0
Global Const $baltic_charset = 186
Global Const $chinesebig5_charset = 136
Global Const $default_charset = 1
Global Const $easteurope_charset = 238
Global Const $gb2312_charset = 134
Global Const $greek_charset = 161
Global Const $hangeul_charset = 129
Global Const $mac_charset = 77
Global Const $oem_charset = 255
Global Const $russian_charset = 204
Global Const $shiftjis_charset = 128
Global Const $symbol_charset = 2
Global Const $turkish_charset = 162
Global Const $vietnamese_charset = 163
Global Const $out_character_precis = 2
Global Const $out_default_precis = 0
Global Const $out_device_precis = 5
Global Const $out_outline_precis = 8
Global Const $out_ps_only_precis = 10
Global Const $out_raster_precis = 6
Global Const $out_string_precis = 1
Global Const $out_stroke_precis = 3
Global Const $out_tt_only_precis = 7
Global Const $out_tt_precis = 4
Global Const $clip_character_precis = 1
Global Const $clip_default_precis = 0
Global Const $clip_embedded = 128
Global Const $clip_lh_angles = 16
Global Const $clip_mask = 15
Global Const $clip_stroke_precis = 2
Global Const $clip_tt_always = 32
Global Const $antialiased_quality = 4
Global Const $default_quality = 0
Global Const $draft_quality = 1
Global Const $nonantialiased_quality = 3
Global Const $proof_quality = 2
Global Const $default_pitch = 0
Global Const $fixed_pitch = 1
Global Const $variable_pitch = 2
Global Const $ff_decorative = 80
Global Const $ff_dontcare = 0
Global Const $ff_modern = 48
Global Const $ff_roman = 16
Global Const $ff_script = 64
Global Const $ff_swiss = 32
Global Const $tagpoint = "long X;long Y"
Global Const $tagrect = "long Left;long Top;long Right;long Bottom"
Global Const $tagsize = "long X;long Y"
Global Const $tagmargins = "int cxLeftWidth;int cxRightWidth;int cyTopHeight;int cyBottomHeight"
Global Const $tagfiletime = "dword Lo;dword Hi"
Global Const $tagsystemtime = "word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds"
Global Const $tagtime_zone_information = "long Bias;wchar StdName[32];word StdDate[8];long StdBias;wchar DayName[32];word DayDate[8];long DayBias"
Global Const $tagnmhdr = "hwnd hWndFrom;uint_ptr IDFrom;INT Code"
Global Const $tagcomboboxexitem = "uint Mask;int_ptr Item;ptr Text;int TextMax;int Image;int SelectedImage;int OverlayImage;" & "int Indent;lparam Param"
Global Const $tagnmcbedragbegin = $tagnmhdr & ";int ItemID;ptr szText"
Global Const $tagnmcbeendedit = $tagnmhdr & ";bool fChanged;int NewSelection;ptr szText;int Why"
Global Const $tagnmcomboboxex = $tagnmhdr & ";uint Mask;int_ptr Item;ptr Text;int TextMax;int Image;" & "int SelectedImage;int OverlayImage;int Indent;lparam Param"
Global Const $tagdtprange = "word MinYear;word MinMonth;word MinDOW;word MinDay;word MinHour;word MinMinute;" & "word MinSecond;word MinMSecond;word MaxYear;word MaxMonth;word MaxDOW;word MaxDay;word MaxHour;" & "word MaxMinute;word MaxSecond;word MaxMSecond;bool MinValid;bool MaxValid"
Global Const $tagnmdatetimechange = $tagnmhdr & ";dword Flag;" & $tagsystemtime
Global Const $tagnmdatetimeformat = $tagnmhdr & ";ptr Format;" & $tagsystemtime & ";ptr pDisplay;wchar Display[64]"
Global Const $tagnmdatetimeformatquery = $tagnmhdr & ";ptr Format;long SizeX;long SizeY"
Global Const $tagnmdatetimekeydown = $tagnmhdr & ";int VirtKey;ptr Format;" & $tagsystemtime
Global Const $tagnmdatetimestring = $tagnmhdr & ";ptr UserString;" & $tagsystemtime & ";dword Flags"
Global Const $tageventlogrecord = "dword Length;dword Reserved;dword RecordNumber;dword TimeGenerated;dword TimeWritten;dword EventID;" & "word EventType;word NumStrings;word EventCategory;word ReservedFlags;dword ClosingRecordNumber;dword StringOffset;" & "dword UserSidLength;dword UserSidOffset;dword DataLength;dword DataOffset"
Global Const $taggdipbitmapdata = "uint Width;uint Height;int Stride;int Format;ptr Scan0;uint_ptr Reserved"
Global Const $taggdipencoderparam = "byte GUID[16];dword Count;dword Type;ptr Values"
Global Const $taggdipencoderparams = "dword Count;byte Params[0]"
Global Const $taggdiprectf = "float X;float Y;float Width;float Height"
Global Const $taggdipstartupinput = "uint Version;ptr Callback;bool NoThread;bool NoCodecs"
Global Const $taggdipstartupoutput = "ptr HookProc;ptr UnhookProc"
Global Const $taggdipimagecodecinfo = "byte CLSID[16];byte FormatID[16];ptr CodecName;ptr DllName;ptr FormatDesc;ptr FileExt;" & "ptr MimeType;dword Flags;dword Version;dword SigCount;dword SigSize;ptr SigPattern;ptr SigMask"
Global Const $taggdippencoderparams = "dword Count;byte Params[0]"
Global Const $taghditem = "uint Mask;int XY;ptr Text;handle hBMP;int TextMax;int Fmt;lparam Param;int Image;int Order;uint Type;ptr pFilter;uint State"
Global Const $tagnmhddispinfo = $tagnmhdr & ";int Item;uint Mask;ptr Text;int TextMax;int Image;lparam lParam"
Global Const $tagnmhdfilterbtnclick = $tagnmhdr & ";int Item;" & $tagrect
Global Const $tagnmheader = $tagnmhdr & ";int Item;int Button;ptr pItem"
Global Const $taggetipaddress = "byte Field4;byte Field3;byte Field2;byte Field1"
Global Const $tagnmipaddress = $tagnmhdr & ";int Field;int Value"
Global Const $taglvfindinfo = "uint Flags;ptr Text;lparam Param;" & $tagpoint & ";uint Direction"
Global Const $taglvhittestinfo = $tagpoint & ";uint Flags;int Item;int SubItem"
Global Const $taglvitem = "uint Mask;int Item;int SubItem;uint State;uint StateMask;ptr Text;int TextMax;int Image;lparam Param;" & "int Indent;int GroupID;uint Columns;ptr pColumns"
Global Const $tagnmlistview = $tagnmhdr & ";int Item;int SubItem;uint NewState;uint OldState;uint Changed;" & "long ActionX;long ActionY;lparam Param"
Global Const $tagnmlvcustomdraw = $tagnmhdr & ";dword dwDrawStage;handle hdc;long Left;long Top;long Right;long Bottom;" & "dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam" & ";dword clrText;dword clrTextBk;int iSubItem;dword dwItemType;dword clrFace;int iIconEffect;" & "int iIconPhase;int iPartId;int iStateId;long TextLeft;long TextTop;long TextRight;long TextBottom;uint uAlign"
Global Const $tagnmlvdispinfo = $tagnmhdr & ";" & $taglvitem
Global Const $tagnmlvfinditem = $tagnmhdr & ";" & $taglvfindinfo
Global Const $tagnmlvgetinfotip = $tagnmhdr & ";dword Flags;ptr Text;int TextMax;int Item;int SubItem;lparam lParam"
Global Const $tagnmitemactivate = $tagnmhdr & ";int Index;int SubItem;uint NewState;uint OldState;uint Changed;" & $tagpoint & ";lparam lParam;uint KeyFlags"
Global Const $tagnmlvkeydown = $tagnmhdr & ";align 1;word VKey;uint Flags"
Global Const $tagnmlvscroll = $tagnmhdr & ";int DX;int DY"
Global Const $tagmchittestinfo = "uint Size;" & $tagpoint & ";uint Hit;" & $tagsystemtime
Global Const $tagmcmonthrange = "word MinYear;word MinMonth;word MinDOW;word MinDay;word MinHour;word MinMinute;word MinSecond;" & "word MinMSeconds;word MaxYear;word MaxMonth;word MaxDOW;word MaxDay;word MaxHour;word MaxMinute;word MaxSecond;" & "word MaxMSeconds;short Span"
Global Const $tagmcrange = "word MinYear;word MinMonth;word MinDOW;word MinDay;word MinHour;word MinMinute;word MinSecond;" & "word MinMSeconds;word MaxYear;word MaxMonth;word MaxDOW;word MaxDay;word MaxHour;word MaxMinute;word MaxSecond;" & "word MaxMSeconds;short MinSet;short MaxSet"
Global Const $tagmcselrange = "word MinYear;word MinMonth;word MinDOW;word MinDay;word MinHour;word MinMinute;word MinSecond;" & "word MinMSeconds;word MaxYear;word MaxMonth;word MaxDOW;word MaxDay;word MaxHour;word MaxMinute;word MaxSecond;" & "word MaxMSeconds"
Global Const $tagnmdaystate = $tagnmhdr & ";" & $tagsystemtime & ";int DayState;ptr pDayState"
Global Const $tagnmselchange = $tagnmhdr & ";word BegYear;word BegMonth;word BegDOW;word BegDay;" & "word BegHour;word BegMinute;word BegSecond;word BegMSeconds;word EndYear;word EndMonth;word EndDOW;" & "word EndDay;word EndHour;word EndMinute;word EndSecond;word EndMSeconds"
Global Const $tagnmobjectnotify = $tagnmhdr & ";int Item;ptr piid;ptr pObject;long Result"
Global Const $tagnmtckeydown = $tagnmhdr & ";word VKey;uint Flags"
Global Const $tagtvitem = "uint Mask;handle hItem;uint State;uint StateMask;ptr Text;int TextMax;int Image;int SelectedImage;" & "int Children;lparam Param"
Global Const $tagtvitemex = $tagtvitem & ";int Integral"
Global Const $tagnmtreeview = $tagnmhdr & ";uint Action;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;uint NewMask;handle NewhItem;" & "uint NewState;uint NewStateMask;ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;" & "lparam NewParam;long PointX;long PointY"
Global Const $tagnmtvcustomdraw = $tagnmhdr & ";dword DrawStage;handle HDC;long Left;long Top;long Right;long Bottom;" & "dword_ptr ItemSpec;uint ItemState;lparam ItemParam;dword ClrText;dword ClrTextBk;int Level"
Global Const $tagnmtvdispinfo = $tagnmhdr & ";" & $tagtvitem
Global Const $tagnmtvgetinfotip = $tagnmhdr & ";ptr Text;int TextMax;handle hItem;lparam lParam"
Global Const $tagtvhittestinfo = $tagpoint & ";uint Flags;handle Item"
Global Const $tagnmtvkeydown = $tagnmhdr & ";word VKey;uint Flags"
Global Const $tagnmmouse = $tagnmhdr & ";dword_ptr ItemSpec;dword_ptr ItemData;" & $tagpoint & ";lparam HitInfo"
Global Const $tagtoken_privileges = "dword Count;int64 LUID;dword Attributes"
Global Const $tagimageinfo = "handle hBitmap;handle hMask;int Unused1;int Unused2;" & $tagrect
Global Const $tagmenuinfo = "dword Size;INT Mask;dword Style;uint YMax;handle hBack;dword ContextHelpID;ulong_ptr MenuData"
Global Const $tagmenuiteminfo = "uint Size;uint Mask;uint Type;uint State;uint ID;handle SubMenu;handle BmpChecked;handle BmpUnchecked;" & "ulong_ptr ItemData;ptr TypeData;uint CCH;handle BmpItem"
Global Const $tagrebarbandinfo = "uint cbSize;uint fMask;uint fStyle;dword clrFore;dword clrBack;ptr lpText;uint cch;" & "int iImage;hwnd hwndChild;uint cxMinChild;uint cyMinChild;uint cx;handle hbmBack;uint wID;uint cyChild;uint cyMaxChild;" & "uint cyIntegral;uint cxIdeal;lparam lParam;uint cxHeader"
Global Const $tagnmrebarautobreak = $tagnmhdr & ";uint uBand;uint wID;lparam lParam;uint uMsg;uint fStyleCurrent;bool fAutoBreak"
Global Const $tagnmrbautosize = $tagnmhdr & ";bool fChanged;long TargetLeft;long TargetTop;long TargetRight;long TargetBottom;" & "long ActualLeft;long ActualTop;long ActualRight;long ActualBottom"
Global Const $tagnmrebar = $tagnmhdr & ";dword dwMask;uint uBand;uint fStyle;uint wID;laram lParam"
Global Const $tagnmrebarchevron = $tagnmhdr & ";uint uBand;uint wID;lparam lParam;" & $tagrect & ";lparam lParamNM"
Global Const $tagnmrebarchildsize = $tagnmhdr & ";uint uBand;uint wID;long CLeft;long CTop;long CRight;long CBottom;" & "long BLeft;long BTop;long BRight;long BBottom"
Global Const $tagcolorscheme = "dword Size;dword BtnHighlight;dword BtnShadow"
Global Const $tagnmtoolbar = $tagnmhdr & ";int iItem;" & "int iBitmap;int idCommand;byte fsState;byte fsStyle;align;dword_ptr dwData;int_ptr iString" & ";int cchText;ptr pszText;" & $tagrect
Global Const $tagnmtbhotitem = $tagnmhdr & ";int idOld;int idNew;dword dwFlags"
Global Const $tagtbbutton = "int Bitmap;int Command;byte State;byte Style;align;dword_ptr Param;int_ptr String"
Global Const $tagtbbuttoninfo = "uint Size;dword Mask;int Command;int Image;byte State;byte Style;word CX;dword_ptr Param;ptr Text;int TextMax"
Global Const $tagnetresource = "dword Scope;dword Type;dword DisplayType;dword Usage;ptr LocalName;ptr RemoteName;ptr Comment;ptr Provider"
Global Const $tagoverlapped = "ulong_ptr Internal;ulong_ptr InternalHigh;dword Offset;dword OffsetHigh;handle hEvent"
Global Const $tagopenfilename = "dword StructSize;hwnd hwndOwner;handle hInstance;ptr lpstrFilter;ptr lpstrCustomFilter;" & "dword nMaxCustFilter;dword nFilterIndex;ptr lpstrFile;dword nMaxFile;ptr lpstrFileTitle;dword nMaxFileTitle;" & "ptr lpstrInitialDir;ptr lpstrTitle;dword Flags;word nFileOffset;word nFileExtension;ptr lpstrDefExt;lparam lCustData;" & "ptr lpfnHook;ptr lpTemplateName;ptr pvReserved;dword dwReserved;dword FlagsEx"
Global Const $tagbitmapinfo = "dword Size;long Width;long Height;word Planes;word BitCount;dword Compression;dword SizeImage;" & "long XPelsPerMeter;long YPelsPerMeter;dword ClrUsed;dword ClrImportant;dword RGBQuad"
Global Const $tagblendfunction = "byte Op;byte Flags;byte Alpha;byte Format"
Global Const $tagguid = "dword Data1;word Data2;word Data3;byte Data4[8]"
Global Const $tagwindowplacement = "uint length; uint flags;uint showCmd;long ptMinPosition[2];long ptMaxPosition[2];long rcNormalPosition[4]"
Global Const $tagwindowpos = "hwnd hWnd;hwnd InsertAfter;int X;int Y;int CX;int CY;uint Flags"
Global Const $tagscrollinfo = "uint cbSize;uint fMask;int  nMin;int  nMax;uint nPage;int  nPos;int  nTrackPos"
Global Const $tagscrollbarinfo = "dword cbSize;" & $tagrect & ";int dxyLineButton;int xyThumbTop;" & "int xyThumbBottom;int reserved;dword rgstate[6]"
Global Const $taglogfont = "long Height;long Width;long Escapement;long Orientation;long Weight;byte Italic;byte Underline;" & "byte Strikeout;byte CharSet;byte OutPrecision;byte ClipPrecision;byte Quality;byte PitchAndFamily;wchar FaceName[32]"
Global Const $tagkbdllhookstruct = "dword vkCode;dword scanCode;dword flags;dword time;ulong_ptr dwExtraInfo"
Global Const $tagprocess_information = "handle hProcess;handle hThread;dword ProcessID;dword ThreadID"
Global Const $tagstartupinfo = "dword Size;ptr Reserved1;ptr Desktop;ptr Title;dword X;dword Y;dword XSize;dword YSize;dword XCountChars;" & "dword YCountChars;dword FillAttribute;dword Flags;word ShowWindow;word Reserved2;ptr Reserved3;handle StdInput;" & "handle StdOutput;handle StdError"
Global Const $tagsecurity_attributes = "dword Length;ptr Descriptor;bool InheritHandle"
Global Const $tagwin32_find_data = "dword dwFileAttributes; dword ftCreationTime[2]; dword ftLastAccessTime[2]; dword ftLastWriteTime[2]; dword nFileSizeHigh; dword nFileSizeLow; dword dwReserved0; dword dwReserved1; wchar cFileName[260]; wchar cAlternateFileName[14]"

Func _winapi_getlasterror($curerr = @error, $curext = @extended)
    Local $aresult = DllCall("kernel32.dll", "dword", "GetLastError")
    Return SetError($curerr, $curext, $aresult[0])
EndFunc

Func _winapi_setlasterror($ierrcode, $curerr = @error, $curext = @extended)
    DllCall("kernel32.dll", "none", "SetLastError", "dword", $ierrcode)
    Return SetError($curerr, $curext)
EndFunc

Global Const $__miscconstant_cc_anycolor = 256
Global Const $__miscconstant_cc_fullopen = 2
Global Const $__miscconstant_cc_rgbinit = 1
Global Const $tagchoosecolor = "dword Size;hwnd hWndOwnder;handle hInstance;dword rgbResult;ptr CustColors;dword Flags;lparam lCustData;" & "ptr lpfnHook;ptr lpTemplateName"
Global Const $tagchoosefont = "dword Size;hwnd hWndOwner;handle hDC;ptr LogFont;int PointSize;dword Flags;dword rgbColors;lparam CustData;" & "ptr fnHook;ptr TemplateName;handle hInstance;ptr szStyle;word FontType;int SizeMin;int SizeMax"

Func _choosecolor($ireturntype = 0, $icolorref = 0, $ireftype = 0, $hwndownder = 0)
    Local $custcolors = "dword[16]"
    Local $tchoose = DllStructCreate($tagchoosecolor)
    Local $tcc = DllStructCreate($custcolors)
    If $ireftype = 1 Then
        $icolorref = Int($icolorref)
    ElseIf $ireftype = 2 Then
        $icolorref = Hex(String($icolorref), 6)
        $icolorref = "0x" & StringMid($icolorref, 5, 2) & StringMid($icolorref, 3, 2) & StringMid($icolorref, 1, 2)
    EndIf
    DllStructSetData($tchoose, "Size", DllStructGetSize($tchoose))
    DllStructSetData($tchoose, "hWndOwnder", $hwndownder)
    DllStructSetData($tchoose, "rgbResult", $icolorref)
    DllStructSetData($tchoose, "CustColors", DllStructGetPtr($tcc))
    DllStructSetData($tchoose, "Flags", BitOR($__miscconstant_cc_anycolor, $__miscconstant_cc_fullopen, $__miscconstant_cc_rgbinit))
    Local $aresult = DllCall("comdlg32.dll", "bool", "ChooseColor", "ptr", DllStructGetPtr($tchoose))
    If @error Then Return SetError(@error, @extended, -1)
    If $aresult[0] = 0 Then Return SetError(-3, -3, -1)
    Local $color_picked = DllStructGetData($tchoose, "rgbResult")
    If $ireturntype = 1 Then
        Return "0x" & Hex(String($color_picked), 6)
    ElseIf $ireturntype = 2 Then
        $color_picked = Hex(String($color_picked), 6)
        Return "0x" & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2)
    ElseIf $ireturntype = 0 Then
        Return $color_picked
    Else
        Return SetError(-4, -4, -1)
    EndIf
EndFunc

Func _choosefont($sfontname = "Courier New", $ipointsize = 10, $icolorref = 0, $ifontweight = 0, $iitalic = False, $iunderline = False, $istrikethru = False, $hwndowner = 0)
    Local $italic = 0, $underline = 0, $strikeout = 0
    Local $lngdc = __misc_getdc(0)
    Local $lfheight = Round(($ipointsize * __misc_getdevicecaps($lngdc, $logpixelsx)) / 72, 0)
    __misc_releasedc(0, $lngdc)
    Local $tchoosefont = DllStructCreate($tagchoosefont)
    Local $tlogfont = DllStructCreate($taglogfont)
    DllStructSetData($tchoosefont, "Size", DllStructGetSize($tchoosefont))
    DllStructSetData($tchoosefont, "hWndOwner", $hwndowner)
    DllStructSetData($tchoosefont, "LogFont", DllStructGetPtr($tlogfont))
    DllStructSetData($tchoosefont, "PointSize", $ipointsize)
    DllStructSetData($tchoosefont, "Flags", BitOR($cf_screenfonts, $cf_printerfonts, $cf_effects, $cf_inittologfontstruct, $cf_noscriptsel))
    DllStructSetData($tchoosefont, "rgbColors", $icolorref)
    DllStructSetData($tchoosefont, "FontType", 0)
    DllStructSetData($tlogfont, "Height", $lfheight)
    DllStructSetData($tlogfont, "Weight", $ifontweight)
    DllStructSetData($tlogfont, "Italic", $iitalic)
    DllStructSetData($tlogfont, "Underline", $iunderline)
    DllStructSetData($tlogfont, "Strikeout", $istrikethru)
    DllStructSetData($tlogfont, "FaceName", $sfontname)
    Local $aresult = DllCall("comdlg32.dll", "bool", "ChooseFontW", "ptr", DllStructGetPtr($tchoosefont))
    If @error Then Return SetError(@error, @extended, -1)
    If $aresult[0] = 0 Then Return SetError(-3, -3, -1)
    Local $fontname = DllStructGetData($tlogfont, "FaceName")
    If StringLen($fontname) = 0 AND StringLen($sfontname) > 0 Then $fontname = $sfontname
    If DllStructGetData($tlogfont, "Italic") Then $italic = 2
    If DllStructGetData($tlogfont, "Underline") Then $underline = 4
    If DllStructGetData($tlogfont, "Strikeout") Then $strikeout = 8
    Local $attributes = BitOR($italic, $underline, $strikeout)
    Local $size = DllStructGetData($tchoosefont, "PointSize") / 10
    Local $colorref = DllStructGetData($tchoosefont, "rgbColors")
    Local $weight = DllStructGetData($tlogfont, "Weight")
    Local $color_picked = Hex(String($colorref), 6)
    Return StringSplit($attributes & "," & $fontname & "," & $size & "," & $weight & "," & $colorref & "," & "0x" & $color_picked & "," & "0x" & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2), ",")
EndFunc

Func _clipputfile($sfile, $sseparator = "|")
    Local Const $gmem_moveable = 2, $cf_hdrop = 15
    $sfile &= $sseparator & $sseparator
    Local $nglobmemsize = (StringLen($sfile) + 20)
    Local $aresult = DllCall("user32.dll", "bool", "OpenClipboard", "hwnd", 0)
    If @error OR $aresult[0] = 0 Then Return SetError(1, _winapi_getlasterror(), False)
    Local $ierror = 0, $ilasterror = 0
    $aresult = DllCall("user32.dll", "bool", "EmptyClipboard")
    If @error OR NOT $aresult[0] Then
        $ierror = 2
        $ilasterror = _winapi_getlasterror()
    Else
        $aresult = DllCall("kernel32.dll", "handle", "GlobalAlloc", "uint", $gmem_moveable, "ulong_ptr", $nglobmemsize)
        If @error OR NOT $aresult[0] Then
            $ierror = 3
            $ilasterror = _winapi_getlasterror()
        Else
            Local $hglobal = $aresult[0]
            $aresult = DllCall("kernel32.dll", "ptr", "GlobalLock", "handle", $hglobal)
            If @error OR NOT $aresult[0] Then
                $ierror = 4
                $ilasterror = _winapi_getlasterror()
            Else
                Local $hlock = $aresult[0]
                Local $dropfiles = DllStructCreate("dword;ptr;int;int;int;char[" & StringLen($sfile) + 1 & "]", $hlock)
                If @error Then Return SetError(5, 6, False)
                Local $tempstruct = DllStructCreate("dword;ptr;int;int;int")
                DllStructSetData($dropfiles, 1, DllStructGetSize($tempstruct))
                DllStructSetData($dropfiles, 2, 0)
                DllStructSetData($dropfiles, 3, 0)
                DllStructSetData($dropfiles, 4, 0)
                DllStructSetData($dropfiles, 5, 0)
                DllStructSetData($dropfiles, 6, $sfile)
                For $i = 1 To StringLen($sfile)
                    If DllStructGetData($dropfiles, 6, $i) = $sseparator Then DllStructSetData($dropfiles, 6, Chr(0), $i)
                Next
                $aresult = DllCall("user32.dll", "handle", "SetClipboardData", "uint", $cf_hdrop, "handle", $hglobal)
                If @error OR NOT $aresult[0] Then
                    $ierror = 6
                    $ilasterror = _winapi_getlasterror()
                EndIf
                $aresult = DllCall("kernel32.dll", "bool", "GlobalUnlock", "handle", $hglobal)
                If (@error OR NOT $aresult[0]) AND NOT $ierror AND _winapi_getlasterror() Then
                    $ierror = 8
                    $ilasterror = _winapi_getlasterror()
                EndIf
            EndIf
            $aresult = DllCall("kernel32.dll", "ptr", "GlobalFree", "handle", $hglobal)
            If (@error OR NOT $aresult[0]) AND NOT $ierror Then
                $ierror = 9
                $ilasterror = _winapi_getlasterror()
            EndIf
        EndIf
    EndIf
    $aresult = DllCall("user32.dll", "bool", "CloseClipboard")
    If (@error OR NOT $aresult[0]) AND NOT $ierror Then Return SetError(7, _winapi_getlasterror(), False)
    If $ierror Then Return SetError($ierror, $ilasterror, False)
    Return True
EndFunc

Func _iif($ftest, $vtrueval, $vfalseval)
    If $ftest Then
        Return $vtrueval
    Else
        Return $vfalseval
    EndIf
EndFunc

Func _mousetrap($ileft = 0, $itop = 0, $iright = 0, $ibottom = 0)
    Local $aresult
    If @NumParams == 0 Then
        $aresult = DllCall("user32.dll", "bool", "ClipCursor", "ptr", 0)
        If @error OR NOT $aresult[0] Then Return SetError(1, _winapi_getlasterror(), False)
    Else
        If @NumParams == 2 Then
            $iright = $ileft + 1
            $ibottom = $itop + 1
        EndIf
        Local $trect = DllStructCreate($tagrect)
        DllStructSetData($trect, "Left", $ileft)
        DllStructSetData($trect, "Top", $itop)
        DllStructSetData($trect, "Right", $iright)
        DllStructSetData($trect, "Bottom", $ibottom)
        $aresult = DllCall("user32.dll", "bool", "ClipCursor", "ptr", DllStructGetPtr($trect))
        If @error OR NOT $aresult[0] Then Return SetError(2, _winapi_getlasterror(), False)
    EndIf
    Return True
EndFunc

Func _singleton($soccurencename, $iflag = 0)
    Local Const $error_already_exists = 183
    Local Const $security_descriptor_revision = 1
    Local $psecurityattributes = 0
    If BitAND($iflag, 2) Then
        Local $tsecuritydescriptor = DllStructCreate("dword[5]")
        Local $psecuritydescriptor = DllStructGetPtr($tsecuritydescriptor)
        Local $aret = DllCall("advapi32.dll", "bool", "InitializeSecurityDescriptor", "ptr", $psecuritydescriptor, "dword", $security_descriptor_revision)
        If @error Then Return SetError(@error, @extended, 0)
        If $aret[0] Then
            $aret = DllCall("advapi32.dll", "bool", "SetSecurityDescriptorDacl", "ptr", $psecuritydescriptor, "bool", 1, "ptr", 0, "bool", 0)
            If @error Then Return SetError(@error, @extended, 0)
            If $aret[0] Then
                Local $structsecurityattributes = DllStructCreate($tagsecurity_attributes)
                DllStructSetData($structsecurityattributes, 1, DllStructGetSize($structsecurityattributes))
                DllStructSetData($structsecurityattributes, 2, $psecuritydescriptor)
                DllStructSetData($structsecurityattributes, 3, 0)
                $psecurityattributes = DllStructGetPtr($structsecurityattributes)
            EndIf
        EndIf
    EndIf
    Local $handle = DllCall("kernel32.dll", "handle", "CreateMutexW", "ptr", $psecurityattributes, "bool", 1, "wstr", $soccurencename)
    If @error Then Return SetError(@error, @extended, 0)
    Local $lasterror = DllCall("kernel32.dll", "dword", "GetLastError")
    If @error Then Return SetError(@error, @extended, 0)
    If $lasterror[0] = $error_already_exists Then
        If BitAND($iflag, 1) Then
            Return SetError($lasterror[0], $lasterror[0], 0)
        Else
            Exit -1
        EndIf
    EndIf
    Return $handle[0]
EndFunc

Func _ispressed($shexkey, $vdll = "user32.dll")
    Local $a_r = DllCall($vdll, "short", "GetAsyncKeyState", "int", "0x" & $shexkey)
    If @error Then Return SetError(@error, @extended, False)
    Return BitAND($a_r[0], 32768) <> 0
EndFunc

Func _versioncompare($sversion1, $sversion2)
    If $sversion1 = $sversion2 Then Return 0
    Local $sep = "."
    If StringInStr($sversion1, $sep) = 0 Then $sep = ","
    Local $aversion1 = StringSplit($sversion1, $sep)
    Local $aversion2 = StringSplit($sversion2, $sep)
    If UBound($aversion1) <> UBound($aversion2) OR UBound($aversion1) = 0 Then
        SetExtended(1)
        If $sversion1 > $sversion2 Then
            Return 1
        ElseIf $sversion1 < $sversion2 Then
            Return -1
        EndIf
    Else
        For $i = 1 To UBound($aversion1) - 1
            If StringIsDigit($aversion1[$i]) AND StringIsDigit($aversion2[$i]) Then
                If Number($aversion1[$i]) > Number($aversion2[$i]) Then
                    Return 1
                ElseIf Number($aversion1[$i]) < Number($aversion2[$i]) Then
                    Return -1
                EndIf
            Else
                SetExtended(1)
                If $aversion1[$i] > $aversion2[$i] Then
                    Return 1
                ElseIf $aversion1[$i] < $aversion2[$i] Then
                    Return -1
                EndIf
            EndIf
        Next
    EndIf
    Return SetError(2, 0, 0)
EndFunc

Func __misc_getdc($hwnd)
    Local $aresult = DllCall("User32.dll", "handle", "GetDC", "hwnd", $hwnd)
    If @error OR NOT $aresult[0] Then Return SetError(1, _winapi_getlasterror(), 0)
    Return $aresult[0]
EndFunc

Func __misc_getdevicecaps($hdc, $iindex)
    Local $aresult = DllCall("GDI32.dll", "int", "GetDeviceCaps", "handle", $hdc, "int", $iindex)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aresult[0]
EndFunc

Func __misc_releasedc($hwnd, $hdc)
    Local $aresult = DllCall("User32.dll", "int", "ReleaseDC", "hwnd", $hwnd, "handle", $hdc)
    If @error Then Return SetError(@error, @extended, False)
    Return $aresult[0] <> 0
EndFunc

Global $ping = TimerInit(), $error
If $cmdline[0] = 1 AND $cmdline[1] = "CameraRAWCloser" Then
    Opt("TrayIconHide", 1)
    TraySetToolTip("CameraRAWCloser")
    GUICreate("", 1, 1)
    If _isreceiver("CameraRAWCloser") Then Exit
    _msgregister("CameraRAWCloser", "_Receiver")
    Local $tr, $tim = TimerInit()
    While 1
        $h = WinGetHandle("[CLASS:CameraRaw_WindowClass]")
        If NOT ($h = "") AND NOT (WinActivate($h) = 0) Then ControlSend($h, "", "", "^o")
        $h = WinGetHandle("[TITLE:Adobe Photoshop CS6 (64 Bit); CLASS:#32770]", " is not currently available.")
        If NOT ($h = "") Then ControlSend($h, "", "", "!c")
        $h = WinGetHandle("[TITLE:Adobe Photoshop CS6 (64 Bit) ; CLASS:#32770]", "&Serial Number:")
        If NOT ($h = "") Then
            ControlClick($h, "", "Button25")
        EndIf
        If TimerDiff($ping) > 600000 Then Exit
        If TimerDiff($tim) > 10000 Then
            If NOT (_singleton("PhotoShop Automate", 1) = 0) Then Exit
            $tim = TimerInit()
        EndIf
        Sleep(200)
    WEnd
EndIf
TraySetToolTip("PhotoAutomate")
_singleton("PhotoShop Automate")
If @Compiled Then
    ShellExecute(@ScriptFullPath, "CameraRAWCloser")
Else
    If NOT FileExists(@ProgramFilesDir & "\AutoIt3\AutoIt3.exe") Then Exit
    ShellExecute(@ProgramFilesDir & "\AutoIt3\AutoIt3.exe", '"' & @ScriptFullPath & '"' & " CameraRAWCloser")
EndIf
AdlibRegister("_ping", 5000)
OnAutoItExitRegister("_exit")

Func _arrayadd(ByRef $avarray, $vvalue)
    If NOT IsArray($avarray) Then Return SetError(1, 0, -1)
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, -1)
    Local $iubound = UBound($avarray)
    ReDim $avarray[$iubound + 1]
    $avarray[$iubound] = $vvalue
    Return $iubound
EndFunc

Func _arraybinarysearch(Const ByRef $avarray, $vvalue, $istart = 0, $iend = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, -1)
    If UBound($avarray, 0) <> 1 Then Return SetError(5, 0, -1)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(4, 0, -1)
    Local $imid = Int(($iend + $istart) / 2)
    If $avarray[$istart] > $vvalue OR $avarray[$iend] < $vvalue Then Return SetError(2, 0, -1)
    While $istart <= $imid AND $vvalue <> $avarray[$imid]
        If $vvalue < $avarray[$imid] Then
            $iend = $imid - 1
        Else
            $istart = $imid + 1
        EndIf
        $imid = Int(($iend + $istart) / 2)
    WEnd
    If $istart > $iend Then Return SetError(3, 0, -1)
    Return $imid
EndFunc

Func _arraycombinations(ByRef $avarray, $iset, $sdelim = "")
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, 0)
    Local $in = UBound($avarray)
    Local $ir = $iset
    Local $aidx[$ir]
    For $i = 0 To $ir - 1
        $aidx[$i] = $i
    Next
    Local $itotal = __array_combinations($in, $ir)
    Local $ileft = $itotal
    Local $aresult[$itotal + 1]
    $aresult[0] = $itotal
    Local $icount = 1
    While $ileft > 0
        __array_getnext($in, $ir, $ileft, $itotal, $aidx)
        For $i = 0 To $iset - 1
            $aresult[$icount] &= $avarray[$aidx[$i]] & $sdelim
        Next
        If $sdelim <> "" Then $aresult[$icount] = StringTrimRight($aresult[$icount], 1)
        $icount += 1
    WEnd
    Return $aresult
EndFunc

Func _arrayconcatenate(ByRef $avarraytarget, Const ByRef $avarraysource, $istart = 0)
    If NOT IsArray($avarraytarget) Then Return SetError(1, 0, 0)
    If NOT IsArray($avarraysource) Then Return SetError(2, 0, 0)
    If UBound($avarraytarget, 0) <> 1 Then
        If UBound($avarraysource, 0) <> 1 Then Return SetError(5, 0, 0)
        Return SetError(3, 0, 0)
    EndIf
    If UBound($avarraysource, 0) <> 1 Then Return SetError(4, 0, 0)
    Local $iuboundtarget = UBound($avarraytarget) - $istart, $iuboundsource = UBound($avarraysource)
    ReDim $avarraytarget[$iuboundtarget + $iuboundsource]
    For $i = $istart To $iuboundsource - 1
        $avarraytarget[$iuboundtarget + $i] = $avarraysource[$i]
    Next
    Return $iuboundtarget + $iuboundsource
EndFunc

Func _arraycreate($v_0, $v_1 = 0, $v_2 = 0, $v_3 = 0, $v_4 = 0, $v_5 = 0, $v_6 = 0, $v_7 = 0, $v_8 = 0, $v_9 = 0, $v_10 = 0, $v_11 = 0, $v_12 = 0, $v_13 = 0, $v_14 = 0, $v_15 = 0, $v_16 = 0, $v_17 = 0, $v_18 = 0, $v_19 = 0, $v_20 = 0)
    Local $av_array[21] = [$v_0, $v_1, $v_2, $v_3, $v_4, $v_5, $v_6, $v_7, $v_8, $v_9, $v_10, $v_11, $v_12, $v_13, $v_14, $v_15, $v_16, $v_17, $v_18, $v_19, $v_20]
    ReDim $av_array[@NumParams]
    Return $av_array
EndFunc

Func _arraydelete(ByRef $avarray, $ielement)
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    Local $iubound = UBound($avarray, 1) - 1
    If NOT $iubound Then
        $avarray = ""
        Return 0
    EndIf
    If $ielement < 0 Then $ielement = 0
    If $ielement > $iubound Then $ielement = $iubound
    Switch UBound($avarray, 0)
        Case 1
            For $i = $ielement To $iubound - 1
                $avarray[$i] = $avarray[$i + 1]
            Next
            ReDim $avarray[$iubound]
        Case 2
            Local $isubmax = UBound($avarray, 2) - 1
            For $i = $ielement To $iubound - 1
                For $j = 0 To $isubmax
                    $avarray[$i][$j] = $avarray[$i + 1][$j]
                Next
            Next
            ReDim $avarray[$iubound][$isubmax + 1]
        Case Else
            Return SetError(3, 0, 0)
    EndSwitch
    Return $iubound
EndFunc

Func _arraydisplay(Const ByRef $avarray, $stitle = "Array: ListView Display", $iitemlimit = -1, $itranspose = 0, $sseparator = "", $sreplace = "|", $sheader = "")
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    Local $idimension = UBound($avarray, 0), $iubound = UBound($avarray, 1) - 1, $isubmax = UBound($avarray, 2) - 1
    If $idimension > 2 Then Return SetError(2, 0, 0)
    If $sseparator = "" Then $sseparator = Chr(124)
    If _arraysearch($avarray, $sseparator, 0, 0, 0, 1) <> -1 Then
        For $x = 1 To 255
            If $x >= 32 AND $x <= 127 Then ContinueLoop
            Local $sfind = _arraysearch($avarray, Chr($x), 0, 0, 0, 1)
            If $sfind = -1 Then
                $sseparator = Chr($x)
                ExitLoop
            EndIf
        Next
    EndIf
    Local $vtmp, $ibuffer = 64
    Local $icollimit = 250
    Local $ioneventmode = Opt("GUIOnEventMode", 0), $sdataseparatorchar = Opt("GUIDataSeparatorChar", $sseparator)
    If $isubmax < 0 Then $isubmax = 0
    If $itranspose Then
        $vtmp = $iubound
        $iubound = $isubmax
        $isubmax = $vtmp
    EndIf
    If $isubmax > $icollimit Then $isubmax = $icollimit
    If $iitemlimit < 1 Then $iitemlimit = $iubound
    If $iubound > $iitemlimit Then $iubound = $iitemlimit
    If $sheader = "" Then
        $sheader = "Row  "
        For $i = 0 To $isubmax
            $sheader &= $sseparator & "Col " & $i
        Next
    EndIf
    Local $avarraytext[$iubound + 1]
    For $i = 0 To $iubound
        $avarraytext[$i] = "[" & $i & "]"
        For $j = 0 To $isubmax
            If $idimension = 1 Then
                If $itranspose Then
                    $vtmp = $avarray[$j]
                Else
                    $vtmp = $avarray[$i]
                EndIf
            Else
                If $itranspose Then
                    $vtmp = $avarray[$j][$i]
                Else
                    $vtmp = $avarray[$i][$j]
                EndIf
            EndIf
            $vtmp = StringReplace($vtmp, $sseparator, $sreplace, 0, 1)
            $avarraytext[$i] &= $sseparator & $vtmp
            $vtmp = StringLen($vtmp)
            If $vtmp > $ibuffer Then $ibuffer = $vtmp
        Next
    Next
    $ibuffer += 1
    Local Const $_arrayconstant_gui_dockborders = 102
    Local Const $_arrayconstant_gui_dockbottom = 64
    Local Const $_arrayconstant_gui_dockheight = 512
    Local Const $_arrayconstant_gui_dockleft = 2
    Local Const $_arrayconstant_gui_dockright = 4
    Local Const $_arrayconstant_gui_event_close = -3
    Local Const $_arrayconstant_lvif_param = 4
    Local Const $_arrayconstant_lvif_text = 1
    Local Const $_arrayconstant_lvm_getcolumnwidth = (4096 + 29)
    Local Const $_arrayconstant_lvm_getitemcount = (4096 + 4)
    Local Const $_arrayconstant_lvm_getitemstate = (4096 + 44)
    Local Const $_arrayconstant_lvm_insertitemw = (4096 + 77)
    Local Const $_arrayconstant_lvm_setextendedlistviewstyle = (4096 + 54)
    Local Const $_arrayconstant_lvm_setitemw = (4096 + 76)
    Local Const $_arrayconstant_lvs_ex_fullrowselect = 32
    Local Const $_arrayconstant_lvs_ex_gridlines = 1
    Local Const $_arrayconstant_lvs_showselalways = 8
    Local Const $_arrayconstant_ws_ex_clientedge = 512
    Local Const $_arrayconstant_ws_maximizebox = 65536
    Local Const $_arrayconstant_ws_minimizebox = 131072
    Local Const $_arrayconstant_ws_sizebox = 262144
    Local Const $_arrayconstant_taglvitem = "int Mask;int Item;int SubItem;int State;int StateMask;ptr Text;int TextMax;int Image;int Param;int Indent;int GroupID;int Columns;ptr pColumns"
    Local $iaddmask = BitOR($_arrayconstant_lvif_text, $_arrayconstant_lvif_param)
    Local $tbuffer = DllStructCreate("wchar Text[" & $ibuffer & "]"), $pbuffer = DllStructGetPtr($tbuffer)
    Local $titem = DllStructCreate($_arrayconstant_taglvitem), $pitem = DllStructGetPtr($titem)
    DllStructSetData($titem, "Param", 0)
    DllStructSetData($titem, "Text", $pbuffer)
    DllStructSetData($titem, "TextMax", $ibuffer)
    Local $iwidth = 640, $iheight = 480
    Local $hgui = GUICreate($stitle, $iwidth, $iheight, Default, Default, BitOR($_arrayconstant_ws_sizebox, $_arrayconstant_ws_minimizebox, $_arrayconstant_ws_maximizebox))
    Local $aiguisize = WinGetClientSize($hgui)
    Local $hlistview = GUICtrlCreateListView($sheader, 0, 0, $aiguisize[0], $aiguisize[1] - 26, $_arrayconstant_lvs_showselalways)
    Local $hcopy = GUICtrlCreateButton("Copy Selected", 3, $aiguisize[1] - 23, $aiguisize[0] - 6, 20)
    GUICtrlSetResizing($hlistview, $_arrayconstant_gui_dockborders)
    GUICtrlSetResizing($hcopy, $_arrayconstant_gui_dockleft + $_arrayconstant_gui_dockright + $_arrayconstant_gui_dockbottom + $_arrayconstant_gui_dockheight)
    GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_setextendedlistviewstyle, $_arrayconstant_lvs_ex_gridlines, $_arrayconstant_lvs_ex_gridlines)
    GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_setextendedlistviewstyle, $_arrayconstant_lvs_ex_fullrowselect, $_arrayconstant_lvs_ex_fullrowselect)
    GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_setextendedlistviewstyle, $_arrayconstant_ws_ex_clientedge, $_arrayconstant_ws_ex_clientedge)
    Local $aitem
    For $i = 0 To $iubound
        If GUICtrlCreateListViewItem($avarraytext[$i], $hlistview) = 0 Then
            $aitem = StringSplit($avarraytext[$i], $sseparator)
            DllStructSetData($tbuffer, "Text", $aitem[1])
            DllStructSetData($titem, "Item", $i)
            DllStructSetData($titem, "SubItem", 0)
            DllStructSetData($titem, "Mask", $iaddmask)
            GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_insertitemw, 0, $pitem)
            DllStructSetData($titem, "Mask", $_arrayconstant_lvif_text)
            For $j = 2 To $aitem[0]
                DllStructSetData($tbuffer, "Text", $aitem[$j])
                DllStructSetData($titem, "SubItem", $j - 1)
                GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_setitemw, 0, $pitem)
            Next
        EndIf
    Next
    $iwidth = 0
    For $i = 0 To $isubmax + 1
        $iwidth += GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_getcolumnwidth, $i, 0)
    Next
    If $iwidth < 250 Then $iwidth = 230
    $iwidth += 20
    If $iwidth > @DesktopWidth Then $iwidth = @DesktopWidth - 100
    WinMove($hgui, "", (@DesktopWidth - $iwidth) / 2, Default, $iwidth)
    GUISetState(@SW_SHOW, $hgui)
    While 1
        Switch GUIGetMsg()
            Case $_arrayconstant_gui_event_close
                ExitLoop
            Case $hcopy
                Local $sclip = ""
                Local $aicuritems[1] = [0]
                For $i = 0 To GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_getitemcount, 0, 0)
                    If GUICtrlSendMsg($hlistview, $_arrayconstant_lvm_getitemstate, $i, 2) Then
                        $aicuritems[0] += 1
                        ReDim $aicuritems[$aicuritems[0] + 1]
                        $aicuritems[$aicuritems[0]] = $i
                    EndIf
                Next
                If NOT $aicuritems[0] Then
                    For $sitem In $avarraytext
                        $sclip &= $sitem & @CRLF
                    Next
                Else
                    For $i = 1 To UBound($aicuritems) - 1
                        $sclip &= $avarraytext[$aicuritems[$i]] & @CRLF
                    Next
                EndIf
                ClipPut($sclip)
        EndSwitch
    WEnd
    GUIDelete($hgui)
    Opt("GUIOnEventMode", $ioneventmode)
    Opt("GUIDataSeparatorChar", $sdataseparatorchar)
    Return 1
EndFunc

Func _arrayfindall(Const ByRef $avarray, $vvalue, $istart = 0, $iend = 0, $icase = 0, $ipartial = 0, $isubitem = 0)
    $istart = _arraysearch($avarray, $vvalue, $istart, $iend, $icase, $ipartial, 1, $isubitem)
    If @error Then Return SetError(@error, 0, -1)
    Local $iindex = 0, $avresult[UBound($avarray)]
    Do
        $avresult[$iindex] = $istart
        $iindex += 1
        $istart = _arraysearch($avarray, $vvalue, $istart + 1, $iend, $icase, $ipartial, 1, $isubitem)
    Until @error
    ReDim $avresult[$iindex]
    Return $avresult
EndFunc

Func _arrayinsert(ByRef $avarray, $ielement, $vvalue = "")
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, 0)
    Local $iubound = UBound($avarray) + 1
    ReDim $avarray[$iubound]
    For $i = $iubound - 1 To $ielement + 1 Step -1
        $avarray[$i] = $avarray[$i - 1]
    Next
    $avarray[$ielement] = $vvalue
    Return $iubound
EndFunc

Func _arraymax(Const ByRef $avarray, $icompnumeric = 0, $istart = 0, $iend = 0)
    Local $iresult = _arraymaxindex($avarray, $icompnumeric, $istart, $iend)
    If @error Then Return SetError(@error, 0, "")
    Return $avarray[$iresult]
EndFunc

Func _arraymaxindex(Const ByRef $avarray, $icompnumeric = 0, $istart = 0, $iend = 0)
    If NOT IsArray($avarray) OR UBound($avarray, 0) <> 1 Then Return SetError(1, 0, -1)
    If UBound($avarray, 0) <> 1 Then Return SetError(3, 0, -1)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(2, 0, -1)
    Local $imaxindex = $istart
    If $icompnumeric Then
        For $i = $istart To $iend
            If Number($avarray[$imaxindex]) < Number($avarray[$i]) Then $imaxindex = $i
        Next
    Else
        For $i = $istart To $iend
            If $avarray[$imaxindex] < $avarray[$i] Then $imaxindex = $i
        Next
    EndIf
    Return $imaxindex
EndFunc

Func _arraymin(Const ByRef $avarray, $icompnumeric = 0, $istart = 0, $iend = 0)
    Local $iresult = _arrayminindex($avarray, $icompnumeric, $istart, $iend)
    If @error Then Return SetError(@error, 0, "")
    Return $avarray[$iresult]
EndFunc

Func _arrayminindex(Const ByRef $avarray, $icompnumeric = 0, $istart = 0, $iend = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, -1)
    If UBound($avarray, 0) <> 1 Then Return SetError(3, 0, -1)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(2, 0, -1)
    Local $iminindex = $istart
    If $icompnumeric Then
        For $i = $istart To $iend
            If Number($avarray[$iminindex]) > Number($avarray[$i]) Then $iminindex = $i
        Next
    Else
        For $i = $istart To $iend
            If $avarray[$iminindex] > $avarray[$i] Then $iminindex = $i
        Next
    EndIf
    Return $iminindex
EndFunc

Func _arraypermute(ByRef $avarray, $sdelim = "")
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, 0)
    Local $isize = UBound($avarray), $ifactorial = 1, $aidx[$isize], $aresult[1], $icount = 1
    For $i = 0 To $isize - 1
        $aidx[$i] = $i
    Next
    For $i = $isize To 1 Step -1
        $ifactorial *= $i
    Next
    ReDim $aresult[$ifactorial + 1]
    $aresult[0] = $ifactorial
    __array_exeterinternal($avarray, 0, $isize, $sdelim, $aidx, $aresult, $icount)
    Return $aresult
EndFunc

Func _arraypop(ByRef $avarray)
    If (NOT IsArray($avarray)) Then Return SetError(1, 0, "")
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, "")
    Local $iubound = UBound($avarray) - 1, $slastval = $avarray[$iubound]
    If NOT $iubound Then
        $avarray = ""
    Else
        ReDim $avarray[$iubound]
    EndIf
    Return $slastval
EndFunc

Func _arraypush(ByRef $avarray, $vvalue, $idirection = 0)
    If (NOT IsArray($avarray)) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(3, 0, 0)
    Local $iubound = UBound($avarray) - 1
    If IsArray($vvalue) Then
        Local $iubounds = UBound($vvalue)
        If ($iubounds - 1) > $iubound Then Return SetError(2, 0, 0)
        If $idirection Then
            For $i = $iubound To $iubounds Step -1
                $avarray[$i] = $avarray[$i - $iubounds]
            Next
            For $i = 0 To $iubounds - 1
                $avarray[$i] = $vvalue[$i]
            Next
        Else
            For $i = 0 To $iubound - $iubounds
                $avarray[$i] = $avarray[$i + $iubounds]
            Next
            For $i = 0 To $iubounds - 1
                $avarray[$i + $iubound - $iubounds + 1] = $vvalue[$i]
            Next
        EndIf
    Else
        If $idirection Then
            For $i = $iubound To 1 Step -1
                $avarray[$i] = $avarray[$i - 1]
            Next
            $avarray[0] = $vvalue
        Else
            For $i = 0 To $iubound - 1
                $avarray[$i] = $avarray[$i + 1]
            Next
            $avarray[$iubound] = $vvalue
        EndIf
    EndIf
    Return 1
EndFunc

Func _arrayreverse(ByRef $avarray, $istart = 0, $iend = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(3, 0, 0)
    Local $vtmp, $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(2, 0, 0)
    For $i = $istart To Int(($istart + $iend - 1) / 2)
        $vtmp = $avarray[$i]
        $avarray[$i] = $avarray[$iend]
        $avarray[$iend] = $vtmp
        $iend -= 1
    Next
    Return 1
EndFunc

Func _arraysearch(Const ByRef $avarray, $vvalue, $istart = 0, $iend = 0, $icase = 0, $ipartial = 0, $iforward = 1, $isubitem = -1)
    If NOT IsArray($avarray) Then Return SetError(1, 0, -1)
    If UBound($avarray, 0) > 2 OR UBound($avarray, 0) < 1 Then Return SetError(2, 0, -1)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(4, 0, -1)
    Local $istep = 1
    If NOT $iforward Then
        Local $itmp = $istart
        $istart = $iend
        $iend = $itmp
        $istep = -1
    EndIf
    Switch UBound($avarray, 0)
        Case 1
            If NOT $ipartial Then
                If NOT $icase Then
                    For $i = $istart To $iend Step $istep
                        If $avarray[$i] = $vvalue Then Return $i
                    Next
                Else
                    For $i = $istart To $iend Step $istep
                        If $avarray[$i] == $vvalue Then Return $i
                    Next
                EndIf
            Else
                For $i = $istart To $iend Step $istep
                    If StringInStr($avarray[$i], $vvalue, $icase) > 0 Then Return $i
                Next
            EndIf
        Case 2
            Local $iuboundsub = UBound($avarray, 2) - 1
            If $isubitem > $iuboundsub Then $isubitem = $iuboundsub
            If $isubitem < 0 Then
                $isubitem = 0
            Else
                $iuboundsub = $isubitem
            EndIf
            For $j = $isubitem To $iuboundsub
                If NOT $ipartial Then
                    If NOT $icase Then
                        For $i = $istart To $iend Step $istep
                            If $avarray[$i][$j] = $vvalue Then Return $i
                        Next
                    Else
                        For $i = $istart To $iend Step $istep
                            If $avarray[$i][$j] == $vvalue Then Return $i
                        Next
                    EndIf
                Else
                    For $i = $istart To $iend Step $istep
                        If StringInStr($avarray[$i][$j], $vvalue, $icase) > 0 Then Return $i
                    Next
                EndIf
            Next
        Case Else
            Return SetError(7, 0, -1)
    EndSwitch
    Return SetError(6, 0, -1)
EndFunc

Func _arraysort(ByRef $avarray, $idescending = 0, $istart = 0, $iend = 0, $isubitem = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(2, 0, 0)
    Switch UBound($avarray, 0)
        Case 1
            __arrayquicksort1d($avarray, $istart, $iend)
            If $idescending Then _arrayreverse($avarray, $istart, $iend)
        Case 2
            Local $isubmax = UBound($avarray, 2) - 1
            If $isubitem > $isubmax Then Return SetError(3, 0, 0)
            If $idescending Then
                $idescending = -1
            Else
                $idescending = 1
            EndIf
            __arrayquicksort2d($avarray, $idescending, $istart, $iend, $isubitem, $isubmax)
        Case Else
            Return SetError(4, 0, 0)
    EndSwitch
    Return 1
EndFunc

Func __arrayquicksort1d(ByRef $avarray, ByRef $istart, ByRef $iend)
    If $iend <= $istart Then Return
    Local $vtmp
    If ($iend - $istart) < 15 Then
        Local $vcur
        For $i = $istart + 1 To $iend
            $vtmp = $avarray[$i]
            If IsNumber($vtmp) Then
                For $j = $i - 1 To $istart Step -1
                    $vcur = $avarray[$j]
                    If ($vtmp >= $vcur AND IsNumber($vcur)) OR (NOT IsNumber($vcur) AND StringCompare($vtmp, $vcur) >= 0) Then ExitLoop
                    $avarray[$j + 1] = $vcur
                Next
            Else
                For $j = $i - 1 To $istart Step -1
                    If (StringCompare($vtmp, $avarray[$j]) >= 0) Then ExitLoop
                    $avarray[$j + 1] = $avarray[$j]
                Next
            EndIf
            $avarray[$j + 1] = $vtmp
        Next
        Return
    EndIf
    Local $l = $istart, $r = $iend, $vpivot = $avarray[Int(($istart + $iend) / 2)], $fnum = IsNumber($vpivot)
    Do
        If $fnum Then
            While ($avarray[$l] < $vpivot AND IsNumber($avarray[$l])) OR (NOT IsNumber($avarray[$l]) AND StringCompare($avarray[$l], $vpivot) < 0)
                $l += 1
            WEnd
            While ($avarray[$r] > $vpivot AND IsNumber($avarray[$r])) OR (NOT IsNumber($avarray[$r]) AND StringCompare($avarray[$r], $vpivot) > 0)
                $r -= 1
            WEnd
        Else
            While (StringCompare($avarray[$l], $vpivot) < 0)
                $l += 1
            WEnd
            While (StringCompare($avarray[$r], $vpivot) > 0)
                $r -= 1
            WEnd
        EndIf
        If $l <= $r Then
            $vtmp = $avarray[$l]
            $avarray[$l] = $avarray[$r]
            $avarray[$r] = $vtmp
            $l += 1
            $r -= 1
        EndIf
    Until $l > $r
    __arrayquicksort1d($avarray, $istart, $r)
    __arrayquicksort1d($avarray, $l, $iend)
EndFunc

Func __arrayquicksort2d(ByRef $avarray, ByRef $istep, ByRef $istart, ByRef $iend, ByRef $isubitem, ByRef $isubmax)
    If $iend <= $istart Then Return
    Local $vtmp, $l = $istart, $r = $iend, $vpivot = $avarray[Int(($istart + $iend) / 2)][$isubitem], $fnum = IsNumber($vpivot)
    Do
        If $fnum Then
            While ($istep * ($avarray[$l][$isubitem] - $vpivot) < 0 AND IsNumber($avarray[$l][$isubitem])) OR (NOT IsNumber($avarray[$l][$isubitem]) AND $istep * StringCompare($avarray[$l][$isubitem], $vpivot) < 0)
                $l += 1
            WEnd
            While ($istep * ($avarray[$r][$isubitem] - $vpivot) > 0 AND IsNumber($avarray[$r][$isubitem])) OR (NOT IsNumber($avarray[$r][$isubitem]) AND $istep * StringCompare($avarray[$r][$isubitem], $vpivot) > 0)
                $r -= 1
            WEnd
        Else
            While ($istep * StringCompare($avarray[$l][$isubitem], $vpivot) < 0)
                $l += 1
            WEnd
            While ($istep * StringCompare($avarray[$r][$isubitem], $vpivot) > 0)
                $r -= 1
            WEnd
        EndIf
        If $l <= $r Then
            For $i = 0 To $isubmax
                $vtmp = $avarray[$l][$i]
                $avarray[$l][$i] = $avarray[$r][$i]
                $avarray[$r][$i] = $vtmp
            Next
            $l += 1
            $r -= 1
        EndIf
    Until $l > $r
    __arrayquicksort2d($avarray, $istep, $istart, $r, $isubitem, $isubmax)
    __arrayquicksort2d($avarray, $istep, $l, $iend, $isubitem, $isubmax)
EndFunc

Func _arrayswap(ByRef $vitem1, ByRef $vitem2)
    Local $vtmp = $vitem1
    $vitem1 = $vitem2
    $vitem2 = $vtmp
EndFunc

Func _arraytoclip(Const ByRef $avarray, $istart = 0, $iend = 0)
    Local $sresult = _arraytostring($avarray, @CR, $istart, $iend)
    If @error Then Return SetError(@error, 0, 0)
    Return ClipPut($sresult)
EndFunc

Func _arraytostring(Const ByRef $avarray, $sdelim = "|", $istart = 0, $iend = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, "")
    If UBound($avarray, 0) <> 1 Then Return SetError(3, 0, "")
    Local $sresult, $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(2, 0, "")
    For $i = $istart To $iend
        $sresult &= $avarray[$i] & $sdelim
    Next
    Return StringTrimRight($sresult, StringLen($sdelim))
EndFunc

Func _arraytrim(ByRef $avarray, $itrimnum, $idirection = 0, $istart = 0, $iend = 0)
    If NOT IsArray($avarray) Then Return SetError(1, 0, 0)
    If UBound($avarray, 0) <> 1 Then Return SetError(2, 0, 0)
    Local $iubound = UBound($avarray) - 1
    If $iend < 1 OR $iend > $iubound Then $iend = $iubound
    If $istart < 0 Then $istart = 0
    If $istart > $iend Then Return SetError(5, 0, 0)
    If $idirection Then
        For $i = $istart To $iend
            $avarray[$i] = StringTrimRight($avarray[$i], $itrimnum)
        Next
    Else
        For $i = $istart To $iend
            $avarray[$i] = StringTrimLeft($avarray[$i], $itrimnum)
        Next
    EndIf
    Return 1
EndFunc

Func _arrayunique($aarray, $idimension = 1, $ibase = 0, $icase = 0, $vdelim = "|")
    Local $iubounddim
    If $vdelim = "|" Then $vdelim = Chr(1)
    If NOT IsArray($aarray) Then Return SetError(1, 0, 0)
    If NOT $idimension > 0 Then
        Return SetError(3, 0, 0)
    Else
        $iubounddim = UBound($aarray, 1)
        If @error Then Return SetError(3, 0, 0)
        If $idimension > 1 Then
            Local $aarraytmp[1]
            For $i = 0 To $iubounddim - 1
                _arrayadd($aarraytmp, $aarray[$i][$idimension - 1])
            Next
            _arraydelete($aarraytmp, 0)
        Else
            If UBound($aarray, 0) = 1 Then
                Dim $aarraytmp[1]
                For $i = 0 To $iubounddim - 1
                    _arrayadd($aarraytmp, $aarray[$i])
                Next
                _arraydelete($aarraytmp, 0)
            Else
                Dim $aarraytmp[1]
                For $i = 0 To $iubounddim - 1
                    _arrayadd($aarraytmp, $aarray[$i][$idimension - 1])
                Next
                _arraydelete($aarraytmp, 0)
            EndIf
        EndIf
    EndIf
    Local $shold
    For $icc = $ibase To UBound($aarraytmp) - 1
        If NOT StringInStr($vdelim & $shold, $vdelim & $aarraytmp[$icc] & $vdelim, $icase) Then $shold &= $aarraytmp[$icc] & $vdelim
    Next
    If $shold Then
        $aarraytmp = StringSplit(StringTrimRight($shold, StringLen($vdelim)), $vdelim, 1)
        Return $aarraytmp
    EndIf
    Return SetError(2, 0, 0)
EndFunc

Func __array_exeterinternal(ByRef $avarray, $istart, $isize, $sdelim, ByRef $aidx, ByRef $aresult, ByRef $icount)
    If $istart == $isize - 1 Then
        For $i = 0 To $isize - 1
            $aresult[$icount] &= $avarray[$aidx[$i]] & $sdelim
        Next
        If $sdelim <> "" Then $aresult[$icount] = StringTrimRight($aresult[$icount], 1)
        $icount += 1
    Else
        Local $itemp
        For $i = $istart To $isize - 1
            $itemp = $aidx[$i]
            $aidx[$i] = $aidx[$istart]
            $aidx[$istart] = $itemp
            __array_exeterinternal($avarray, $istart + 1, $isize, $sdelim, $aidx, $aresult, $icount)
            $aidx[$istart] = $aidx[$i]
            $aidx[$i] = $itemp
        Next
    EndIf
EndFunc

Func __array_combinations($in, $ir)
    Local $i_total = 1
    For $i = $ir To 1 Step -1
        $i_total *= ($in / $i)
        $in -= 1
    Next
    Return Round($i_total)
EndFunc

Func __array_getnext($in, $ir, ByRef $ileft, $itotal, ByRef $aidx)
    If $ileft == $itotal Then
        $ileft -= 1
        Return
    EndIf
    Local $i = $ir - 1
    While $aidx[$i] == $in - $ir + $i
        $i -= 1
    WEnd
    $aidx[$i] += 1
    For $j = $i + 1 To $ir - 1
        $aidx[$j] = $aidx[$i] + $j - $i
    Next
    $ileft -= 1
EndFunc

Global Const $fc_nooverwrite = 0
Global Const $fc_overwrite = 1
Global Const $ft_modified = 0
Global Const $ft_created = 1
Global Const $ft_accessed = 2
Global Const $fo_read = 0
Global Const $fo_append = 1
Global Const $fo_overwrite = 2
Global Const $fo_binary = 16
Global Const $fo_unicode = 32
Global Const $fo_utf16_le = 32
Global Const $fo_utf16_be = 64
Global Const $fo_utf8 = 128
Global Const $fo_utf8_nobom = 256
Global Const $eof = -1
Global Const $fd_filemustexist = 1
Global Const $fd_pathmustexist = 2
Global Const $fd_multiselect = 4
Global Const $fd_promptcreatenew = 8
Global Const $fd_promptoverwrite = 16
Global Const $create_new = 1
Global Const $create_always = 2
Global Const $open_existing = 3
Global Const $open_always = 4
Global Const $truncate_existing = 5
Global Const $invalid_set_file_pointer = -1
Global Const $file_begin = 0
Global Const $file_current = 1
Global Const $file_end = 2
Global Const $file_attribute_readonly = 1
Global Const $file_attribute_hidden = 2
Global Const $file_attribute_system = 4
Global Const $file_attribute_directory = 16
Global Const $file_attribute_archive = 32
Global Const $file_attribute_device = 64
Global Const $file_attribute_normal = 128
Global Const $file_attribute_temporary = 256
Global Const $file_attribute_sparse_file = 512
Global Const $file_attribute_reparse_point = 1024
Global Const $file_attribute_compressed = 2048
Global Const $file_attribute_offline = 4096
Global Const $file_attribute_not_content_indexed = 8192
Global Const $file_attribute_encrypted = 16384
Global Const $file_share_read = 1
Global Const $file_share_write = 2
Global Const $file_share_delete = 4
Global Const $generic_all = 268435456
Global Const $generic_execute = 536870912
Global Const $generic_write = 1073741824
Global Const $generic_read = --2147483648

Func _filecountlines($sfilepath)
    Local $hfile = FileOpen($sfilepath, $fo_read)
    If $hfile = -1 Then Return SetError(1, 0, 0)
    Local $sfilecontent = StringStripWS(FileRead($hfile), 2)
    FileClose($hfile)
    Local $atmp
    If StringInStr($sfilecontent, @LF) Then
        $atmp = StringSplit(StringStripCR($sfilecontent), @LF)
    ElseIf StringInStr($sfilecontent, @CR) Then
        $atmp = StringSplit($sfilecontent, @CR)
    Else
        If StringLen($sfilecontent) Then
            Return 1
        Else
            Return SetError(2, 0, 0)
        EndIf
    EndIf
    Return $atmp[0]
EndFunc

Func _filecreate($sfilepath)
    Local $hopenfile = FileOpen($sfilepath, $fo_overwrite)
    If $hopenfile = -1 Then Return SetError(1, 0, 0)
    Local $hwritefile = FileWrite($hopenfile, "")
    FileClose($hopenfile)
    If $hwritefile = -1 Then Return SetError(2, 0, 0)
    Return 1
EndFunc

Func _filelisttoarray($spath, $sfilter = "*", $iflag = 0)
    Local $hsearch, $sfile, $sfilelist, $sdelim = "|"
    $spath = StringRegExpReplace($spath, "[\\/]+\z", "") & "\"
    If NOT FileExists($spath) Then Return SetError(1, 1, "")
    If StringRegExp($sfilter, "[\\/:><\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
    If NOT ($iflag = 0 OR $iflag = 1 OR $iflag = 2) Then Return SetError(3, 3, "")
    $hsearch = FileFindFirstFile($spath & $sfilter)
    If @error Then Return SetError(4, 4, "")
    While 1
        $sfile = FileFindNextFile($hsearch)
        If @error Then ExitLoop
        If ($iflag + @extended = 2) Then ContinueLoop
        $sfilelist &= $sdelim & $sfile
    WEnd
    FileClose($hsearch)
    If NOT $sfilelist Then Return SetError(4, 4, "")
    Return StringSplit(StringTrimLeft($sfilelist, 1), "|")
EndFunc

Func _fileprint($s_file, $i_show = @SW_HIDE)
    Local $a_ret = DllCall("shell32.dll", "int", "ShellExecuteW", "hwnd", 0, "wstr", "print", "wstr", $s_file, "wstr", "", "wstr", "", "int", $i_show)
    If @error Then Return SetError(@error, @extended, 0)
    If $a_ret[0] <= 32 Then Return SetError(10, $a_ret[0], 0)
    Return 1
EndFunc

Func _filereadtoarray($sfilepath, ByRef $aarray)
    Local $hfile = FileOpen($sfilepath, $fo_read)
    If $hfile = -1 Then Return SetError(1, 0, 0)
    Local $afile = FileRead($hfile, FileGetSize($sfilepath))
    If StringRight($afile, 1) = @LF Then $afile = StringTrimRight($afile, 1)
    If StringRight($afile, 1) = @CR Then $afile = StringTrimRight($afile, 1)
    FileClose($hfile)
    If StringInStr($afile, @LF) Then
        $aarray = StringSplit(StringStripCR($afile), @LF)
    ElseIf StringInStr($afile, @CR) Then
        $aarray = StringSplit($afile, @CR)
    Else
        If StringLen($afile) Then
            Dim $aarray[2] = [1, $afile]
        Else
            Return SetError(2, 0, 0)
        EndIf
    EndIf
    Return 1
EndFunc

Func _filewritefromarray($file, $a_array, $i_base = 0, $i_ubound = 0)
    If NOT IsArray($a_array) Then Return SetError(2, 0, 0)
    Local $last = UBound($a_array) - 1
    If $i_ubound < 1 OR $i_ubound > $last Then $i_ubound = $last
    If $i_base < 0 OR $i_base > $last Then $i_base = 0
    Local $hfile
    If IsString($file) Then
        $hfile = FileOpen($file, $fo_overwrite)
    Else
        $hfile = $file
    EndIf
    If $hfile = -1 Then Return SetError(1, 0, 0)
    Local $errorsav = 0
    For $x = $i_base To $i_ubound
        If FileWrite($hfile, $a_array[$x] & @CRLF) = 0 Then
            $errorsav = 3
            ExitLoop
        EndIf
    Next
    If IsString($file) Then FileClose($hfile)
    If $errorsav Then Return SetError($errorsav, 0, 0)
    Return 1
EndFunc

Func _filewritelog($slogpath, $slogmsg, $iflag = -1)
    Local $iopenmode = $fo_append
    Local $sdatenow = @YEAR & "-" & @MON & "-" & @MDAY
    Local $stimenow = @HOUR & ":" & @MIN & ":" & @SEC
    Local $smsg = $sdatenow & " " & $stimenow & " : " & $slogmsg
    If $iflag <> -1 Then
        $smsg &= @CRLF & FileRead($slogpath)
        $iopenmode = $fo_overwrite
    EndIf
    Local $hopenfile = FileOpen($slogpath, $iopenmode)
    If $hopenfile = -1 Then Return SetError(1, 0, 0)
    Local $iwritefile = FileWriteLine($hopenfile, $smsg)
    Local $iret = FileClose($hopenfile)
    If $iwritefile = -1 Then Return SetError(2, $iret, 0)
    Return $iret
EndFunc

Func _filewritetoline($sfile, $iline, $stext, $foverwrite = 0)
    If $iline <= 0 Then Return SetError(4, 0, 0)
    If NOT IsString($stext) Then
        $stext = String($stext)
        If $stext = "" Then Return SetError(6, 0, 0)
    EndIf
    If $foverwrite <> 0 AND $foverwrite <> 1 Then Return SetError(5, 0, 0)
    If NOT FileExists($sfile) Then Return SetError(2, 0, 0)
    Local $sread_file = FileRead($sfile)
    Local $asplit_file = StringSplit(StringStripCR($sread_file), @LF)
    If UBound($asplit_file) < $iline Then Return SetError(1, 0, 0)
    Local $hfile = FileOpen($sfile, $fo_overwrite)
    If $hfile = -1 Then Return SetError(3, 0, 0)
    $sread_file = ""
    For $i = 1 To $asplit_file[0]
        If $i = $iline Then
            If $foverwrite = 1 Then
                If $stext <> "" Then $sread_file &= $stext & @CRLF
            Else
                $sread_file &= $stext & @CRLF & $asplit_file[$i] & @CRLF
            EndIf
        ElseIf $i < $asplit_file[0] Then
            $sread_file &= $asplit_file[$i] & @CRLF
        ElseIf $i = $asplit_file[0] Then
            $sread_file &= $asplit_file[$i]
        EndIf
    Next
    FileWrite($hfile, $sread_file)
    FileClose($hfile)
    Return 1
EndFunc

Func _pathfull($srelativepath, $sbasepath = @WorkingDir)
    If NOT $srelativepath OR $srelativepath = "." Then Return $sbasepath
    Local $sfullpath = StringReplace($srelativepath, "/", "\")
    Local Const $sfullpathconst = $sfullpath
    Local $spath
    Local $brootonly = StringLeft($sfullpath, 1) = "\" AND StringMid($sfullpath, 2, 1) <> "\"
    For $i = 1 To 2
        $spath = StringLeft($sfullpath, 2)
        If $spath = "\\" Then
            $sfullpath = StringTrimLeft($sfullpath, 2)
            Local $nserverlen = StringInStr($sfullpath, "\") - 1
            $spath = "\\" & StringLeft($sfullpath, $nserverlen)
            $sfullpath = StringTrimLeft($sfullpath, $nserverlen)
            ExitLoop
        ElseIf StringRight($spath, 1) = ":" Then
            $sfullpath = StringTrimLeft($sfullpath, 2)
            ExitLoop
        Else
            $sfullpath = $sbasepath & "\" & $sfullpath
        EndIf
    Next
    If $i = 3 Then Return ""
    If StringLeft($sfullpath, 1) <> "\" Then
        If StringLeft($sfullpathconst, 2) = StringLeft($sbasepath, 2) Then
            $sfullpath = $sbasepath & "\" & $sfullpath
        Else
            $sfullpath = "\" & $sfullpath
        EndIf
    EndIf
    Local $atemp = StringSplit($sfullpath, "\")
    Local $apathparts[$atemp[0]], $j = 0
    For $i = 2 To $atemp[0]
        If $atemp[$i] = ".." Then
            If $j Then $j -= 1
        ElseIf NOT ($atemp[$i] = "" AND $i <> $atemp[0]) AND $atemp[$i] <> "." Then
            $apathparts[$j] = $atemp[$i]
            $j += 1
        EndIf
    Next
    $sfullpath = $spath
    If NOT $brootonly Then
        For $i = 0 To $j - 1
            $sfullpath &= "\" & $apathparts[$i]
        Next
    Else
        $sfullpath &= $sfullpathconst
        If StringInStr($sfullpath, "..") Then $sfullpath = _pathfull($sfullpath)
    EndIf
    While StringInStr($sfullpath, ".\")
        $sfullpath = StringReplace($sfullpath, ".\", "\")
    WEnd
    Return $sfullpath
EndFunc

Func _pathgetrelative($sfrom, $sto)
    If StringRight($sfrom, 1) <> "\" Then $sfrom &= "\"
    If StringRight($sto, 1) <> "\" Then $sto &= "\"
    If $sfrom = $sto Then Return SetError(1, 0, StringTrimRight($sto, 1))
    Local $asfrom = StringSplit($sfrom, "\")
    Local $asto = StringSplit($sto, "\")
    If $asfrom[1] <> $asto[1] Then Return SetError(2, 0, StringTrimRight($sto, 1))
    Local $i = 2
    Local $idiff = 1
    While 1
        If $asfrom[$i] <> $asto[$i] Then
            $idiff = $i
            ExitLoop
        EndIf
        $i += 1
    WEnd
    $i = 1
    Local $srelpath = ""
    For $j = 1 To $asto[0]
        If $i >= $idiff Then
            $srelpath &= "\" & $asto[$i]
        EndIf
        $i += 1
    Next
    $srelpath = StringTrimLeft($srelpath, 1)
    $i = 1
    For $j = 1 To $asfrom[0]
        If $i > $idiff Then
            $srelpath = "..\" & $srelpath
        EndIf
        $i += 1
    Next
    If StringRight($srelpath, 1) == "\" Then $srelpath = StringTrimRight($srelpath, 1)
    Return $srelpath
EndFunc

Func _pathmake($szdrive, $szdir, $szfname, $szext)
    If StringLen($szdrive) Then
        If NOT (StringLeft($szdrive, 2) = "\\") Then $szdrive = StringLeft($szdrive, 1) & ":"
    EndIf
    If StringLen($szdir) Then
        If NOT (StringRight($szdir, 1) = "\") AND NOT (StringRight($szdir, 1) = "/") Then $szdir = $szdir & "\"
    EndIf
    If StringLen($szext) Then
        If NOT (StringLeft($szext, 1) = ".") Then $szext = "." & $szext
    EndIf
    Return $szdrive & $szdir & $szfname & $szext
EndFunc

Func _pathsplit($szpath, ByRef $szdrive, ByRef $szdir, ByRef $szfname, ByRef $szext)
    Local $drive = ""
    Local $dir = ""
    Local $fname = ""
    Local $ext = ""
    Local $pos
    Local $array[5]
    $array[0] = $szpath
    If StringMid($szpath, 2, 1) = ":" Then
        $drive = StringLeft($szpath, 2)
        $szpath = StringTrimLeft($szpath, 2)
    ElseIf StringLeft($szpath, 2) = "\\" Then
        $szpath = StringTrimLeft($szpath, 2)
        $pos = StringInStr($szpath, "\")
        If $pos = 0 Then $pos = StringInStr($szpath, "/")
        If $pos = 0 Then
            $drive = "\\" & $szpath
            $szpath = ""
        Else
            $drive = "\\" & StringLeft($szpath, $pos - 1)
            $szpath = StringTrimLeft($szpath, $pos - 1)
        EndIf
    EndIf
    Local $nposforward = StringInStr($szpath, "/", 0, -1)
    Local $nposbackward = StringInStr($szpath, "\", 0, -1)
    If $nposforward >= $nposbackward Then
        $pos = $nposforward
    Else
        $pos = $nposbackward
    EndIf
    $dir = StringLeft($szpath, $pos)
    $fname = StringRight($szpath, StringLen($szpath) - $pos)
    If StringLen($dir) = 0 Then $fname = $szpath
    $pos = StringInStr($fname, ".", 0, -1)
    If $pos Then
        $ext = StringRight($fname, StringLen($fname) - ($pos - 1))
        $fname = StringLeft($fname, $pos - 1)
    EndIf
    $szdrive = $drive
    $szdir = $dir
    $szfname = $fname
    $szext = $ext
    $array[1] = $drive
    $array[2] = $dir
    $array[3] = $fname
    $array[4] = $ext
    Return $array
EndFunc

Func _replacestringinfile($szfilename, $szsearchstring, $szreplacestring, $fcaseness = 0, $foccurance = 1)
    Local $iretval = 0
    Local $ncount, $sendswith
    If StringInStr(FileGetAttrib($szfilename), "R") Then Return SetError(6, 0, -1)
    Local $hfile = FileOpen($szfilename, $fo_read)
    If $hfile = -1 Then Return SetError(1, 0, -1)
    Local $s_totfile = FileRead($hfile, FileGetSize($szfilename))
    If StringRight($s_totfile, 2) = @CRLF Then
        $sendswith = @CRLF
    ElseIf StringRight($s_totfile, 1) = @CR Then
        $sendswith = @CR
    ElseIf StringRight($s_totfile, 1) = @LF Then
        $sendswith = @LF
    Else
        $sendswith = ""
    EndIf
    Local $afilelines = StringSplit(StringStripCR($s_totfile), @LF)
    FileClose($hfile)
    Local $hwritehandle = FileOpen($szfilename, $fo_overwrite)
    If $hwritehandle = -1 Then Return SetError(2, 0, -1)
    For $ncount = 1 To $afilelines[0]
        If StringInStr($afilelines[$ncount], $szsearchstring, $fcaseness) Then
            $afilelines[$ncount] = StringReplace($afilelines[$ncount], $szsearchstring, $szreplacestring, 1 - $foccurance, $fcaseness)
            $iretval = $iretval + 1
            If $foccurance = 0 Then
                $iretval = 1
                ExitLoop
            EndIf
        EndIf
    Next
    For $ncount = 1 To $afilelines[0] - 1
        If FileWriteLine($hwritehandle, $afilelines[$ncount]) = 0 Then
            FileClose($hwritehandle)
            Return SetError(3, 0, -1)
        EndIf
    Next
    If $afilelines[$ncount] <> "" Then FileWrite($hwritehandle, $afilelines[$ncount] & $sendswith)
    FileClose($hwritehandle)
    Return $iretval
EndFunc

Func _tempfile($s_directoryname = @TempDir, $s_fileprefix = "~", $s_fileextension = ".tmp", $i_randomlength = 7)
    If NOT FileExists($s_directoryname) Then $s_directoryname = @TempDir
    If NOT FileExists($s_directoryname) Then $s_directoryname = @ScriptDir
    If StringRight($s_directoryname, 1) <> "\" Then $s_directoryname = $s_directoryname & "\"
    Local $s_tempname
    Do
        $s_tempname = ""
        While StringLen($s_tempname) < $i_randomlength
            $s_tempname = $s_tempname & Chr(Random(97, 122, 1))
        WEnd
        $s_tempname = $s_directoryname & $s_fileprefix & $s_tempname & $s_fileextension
    Until NOT FileExists($s_tempname)
    Return $s_tempname
EndFunc

Global $omyerror = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $ophotoshop = ObjCreate("Photoshop.Application")
If IsObj($ophotoshop) = False Then
    MsgBox(0, "", "Not Obj")
    Exit
EndIf
$ophotoshop.displaydialogs = 3
$vars = IniReadSection("ps.ini", "Main")
Local $raw[6]
For $i = 1 To UBound($raw) - 1
    $raw[$i] = _read_cameraraw_xmp(@ScriptDir & "\SkinTone" & $i & ".xmp")
Next
$toedit = _get_var("toedit")
$list = _filelisttoarray($toedit, "*", 2)
If $list = 0 Then Exit
For $i = 1 To $list[0]
    $m = StringRight($list[$i], 1)
    If NOT (String(Number($m)) = $m) Then
        $folder = $toedit & "\" & $list[$i]
        $list2 = _filelisttoarray($folder, "*.jp*", 1)
        If $list2 = 0 Then ContinueLoop
        Do
            $error = 0
            $ophotoshop.load($folder & "\" & $list2[1])
        Until $error = 0
        $ophotoshop.doaction("ToneCalc", "AutoEditActions")
        Local $pos[2] = [Floor($ophotoshop.activedocument.width / 2), Floor($ophotoshop.activedocument.height / 2)]
        $ophotoshop.activedocument.colorsamplers.removeall()
        $sampler = $ophotoshop.activedocument.colorsamplers.add($pos)
        $c = $sampler.color.cmyk.cyan
        $m = $sampler.color.cmyk.magenta
        $y = $sampler.color.cmyk.yellow
        $k = $sampler.color.cmyk.black
        $ophotoshop.activedocument.close(2)
        $m = $c + $m + $y + $k
        If $m < 45 Then
            $m = 1
        ElseIf $m < 61 Then
            $m = 2
        ElseIf $m < 85 Then
            $m = 3
        ElseIf $m < 130 Then
            $m = 4
        Else
            $m = 5
        EndIf
        DirMove($folder, $folder & $m)
        $folder &= $m
    Else
        $folder = $toedit & "\" & $list[$i]
        $list[$i] = StringTrimRight($list[$i], 1)
    EndIf
    $list3 = _filelisttoarray($folder, "*.xmp", 1)
    If NOT IsArray($list3) Then ContinueLoop
    $tododge = _get_var("tododge")
    DirCreate($tododge & "\" & $list[$i] & $m)
    For $j = 1 To $list3[0]
        $file = StringRegExp($list3[$j], "(.*?)\.[^.]*?", 3)
        If _xmp_get_label($folder & "\" & $list3[$j]) = 0 Then ContinueLoop
        _write_cameraraw_xmp($raw[$m], $folder & "\" & $list3[$j])
        $list4 = _filelisttoarray($folder, $file[0] & ".*", 1)
        If $list4 = 0 Then ContinueLoop
        Do
            $error = 0
            $ophotoshop.load($folder & "\" & $list4[1])
        Until $error = 0
        $jpgsaveoptions = ObjCreate("Photoshop.JPEGSaveOptions")
        $jpgsaveoptions.embedcolorprofile = True
        $jpgsaveoptions.quality = 12
        $ophotoshop.activedocument.saveas($tododge & "\" & $list[$i] & $m & "\" & $file[0] & ".jpg", $jpgsaveoptions)
        $ophotoshop.doaction("UltimateKYDodge", "AutoEditActions")
    Next
    $tocheck = _get_var("tocheck")
    DirRemove($tocheck & "\" & $list[$i] & $m, 1)
    DirMove($tododge & "\" & $list[$i] & $m, $tocheck & "\" & $list[$i] & $m, 9)
    $raw2 = _get_var("RAW")
    DirRemove($raw2 & "\" & $list[$i] & $m, 1)
    DirMove($folder, $raw2 & "\" & $list[$i] & $m, 9)
Next
Do
    $error = 0
    $ophotoshop.quit
Until $error = 0

Func _ping()
    _msgsend("CameraRAWCloser", "Ping")
EndFunc

Func _exit()
    _msgsend("CameraRAWCloser", "ShutDown")
    Exit
EndFunc

Func _receiver($smessage)
    If $smessage = "Ping" Then
        $ping = TimerInit()
    Else
        Exit
    EndIf
EndFunc

Func _xmp_get_label($xmp)
    $label = StringRegExp(FileRead($xmp), 'x.p:Label="(.*?)"', 3)
    If NOT IsArray($label) Then Return 0
    Switch $label[0]
        Case "Select"
            $label = 6
        Case "Second"
            $label = 7
        Case "Approved"
            $label = 8
        Case "Review"
            $label = 9
        Case Else
            $label = 0
    EndSwitch
    Return $label
EndFunc

Func _read_cameraraw_xmp($xmp)
    $xmp_data = StringRegExp(FileRead($xmp), "</rdf\:Description>", 3)
    If NOT IsArray($xmp_data) Then Return 0
    Return $xmp_data[0]
EndFunc

Func _write_cameraraw_xmp($data, $cr)
    $set = StringRegExpReplace(FileRead($cr), "</rdf\:Description>", $data)
    $f = FileOpen($cr, 2)
    FileWrite($f, $set)
    FileClose($f)
EndFunc

Func _load_labels($dir)
    $labels = StringRegExp(FileRead($dir & "\.BridgeLabelsAndRatings"), "<item key='(.*?)' label='(.*?)'/>", 3)
    If NOT IsArray($labels) Then Return 0
    Local $data[UBound($labels) / 2][2]
    For $i = 0 To UBound($labels) - 1 Step 2
        $data[$i / 2][0] = $labels[$i]
        Switch $labels[$i + 1]
            Case "Select"
                $data[$i / 2][1] = 6
            Case "Second"
                $data[$i / 2][1] = 7
            Case "Approved"
                $data[$i / 2][1] = 8
            Case "Review"
                $data[$i / 2][1] = 9
        EndSwitch
    Next
    Return $data
EndFunc

Func _get_var($id)
    For $i = 1 To $vars[0][0]
        If $vars[$i][0] = $id Then Return $vars[$i][1]
    Next
EndFunc

Func myerrfunc()
    Local $line = $omyerror.scriptline
    ConsoleWrite("COM Error: " & Hex($omyerror.number, 8) & @CRLF & "Line #" & $line & @CRLF & "Description: " & $omyerror.description & @CRLF)
    If NOT ($line = 75 OR $line = 117 OR $line = 136) AND MsgBox(64 + 1, "COM Error", "Line #" & $line & @CRLF & "Continue?") = 2 Then Exit
    $error = 1
EndFunc
Link to comment
Share on other sites

 

As a note it always seems to stop around when the program reaches 2.6gb of system memory however I have allocated Photoshop with 12gb out of the 16 GB available.

I don't know how you have done that. Memory limits on Windows.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The machine itself is 64 bit .

I have no problem with manually opening files and Photoshop exceeding 3gb-16GB of memory usage.

It only seems to cap itself when the script is running.   It seems to stop consistently when opening the raw at around that memory usage. 2.6gb-3gb

From what I can tell this is the part of the script which controls that part:

Global $ping = TimerInit(), $error
If $cmdline[0] = 1 AND $cmdline[1] = "CameraRAWCloser" Then
    Opt("TrayIconHide", 1)
    TraySetToolTip("CameraRAWCloser")
    GUICreate("", 1, 1)
    If _isreceiver("CameraRAWCloser") Then Exit
    _msgregister("CameraRAWCloser", "_Receiver")
    Local $tr, $tim = TimerInit()
    While 1
        $h = WinGetHandle("[CLASS:CameraRaw_WindowClass]")
        If NOT ($h = "") AND NOT (WinActivate($h) = 0) Then ControlSend($h, "", "", "^o")
        $h = WinGetHandle("[TITLE:Photoshop CS; CLASS:#32770]", " is not currently available.")
        If NOT ($h = "") Then ControlSend($h, "", "", "!c")
        $h = WinGetHandle("[TITLE:Photoshop CS ; CLASS:#32770]", "&Serial Number:")
        If NOT ($h = "") Then
            ControlClick($h, "", "Button25")
        EndIf
        If TimerDiff($ping) > 600000 Then Exit
        If TimerDiff($tim) > 70000 Then
            If NOT (_singleton("PhotoShop Automate", 1) = 0) Then Exit
            $tim = TimerInit()
        EndIf
        Sleep(50)
    WEnd
EndIf
TraySetToolTip("PhotoAutomate")
_singleton("PhotoShop Automate")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...