TimeRange Randomizer

if (`window -exists timeScale`) deleteUI timeScale;
        window -title "Randomize TimeRange" -widthHeight 160 220 timeScale;
            if (`windowPref -exists timeScale`) windowPref -remove timeScale;
            
global proc ajInputError()
{
    string $animInputErrorWindow = `window -title "WHOA!"
       -iconName "Short Name"
       -widthHeight 200 80`;
        columnLayout -adjustableColumn true;
        text -label "";
        text -label "Start frame error! ZOMG!";
        text -label "CHECK YO SHIT!";
        text -label "";
       button -label "I'll fix it! OMG!" -command ("deleteUI -window " + $animInputErrorWindow);
    setParent ..;
    showWindow $animInputErrorWindow;
}

global proc ajNothingSelected()
{
    string $nothingSelectedErrorWindow = `window -title "WHOA!"
       -iconName "Short Name"
       -widthHeight 200 80`;
        columnLayout -adjustableColumn true;
        text -label "";
        text -label "You gotta select something!";
        text -label "Ermagerde!!!";
        text -label "";
       button -label "I'll fix it! OMG!" -command ("deleteUI -window " + $nothingSelectedErrorWindow);
    setParent ..;
    showWindow $nothingSelectedErrorWindow;
}
frameLayout -label "Timerange Randomizer";
    columnLayout -adjustableColumn true;
        columnLayout;
            text -label "\nPlease set start and end frames.\n";
        setParent ..;
        rowLayout -numberOfColumns 5;
            text -label "   ";
            text -label "Start";
            intField -w 40 -value 1 timeScaleStartFrame; //Start Frame input
            text -label "   End";
            intField -w 40 -value 100 timeScaleEndFrame; //End Frame Input
        setParent ..;
        rowLayout -numberOfColumns 5;
            text -label "   ";
            text -label "Amount: ";
            floatField -w 60 -value 1 animInputInt; //Amount input
        setParent ..;
        setParent ..;
        columnLayout;
            text -label "";
            rowLayout -numberOfColumns 2;
                button -label "Copy Keys" -w 65 -command "ajCopyKeys";
                button -label "Reset" -w 65 -command ("ajRevertKeys");
            setParent ..;
            rowLayout -numberOfColumns 2;
                button -label "GO!" -w 65 -command "ajTimeScaleTest";
                button -label "Re-seed" -w 65 -command ("ajReseed");
            setParent ..;
            rowLayout -numberOfColumns 2;
                button -label "Close" -w 65 -command ("deleteUI -window timeScale");
            setParent ..;
        setParent ..;
    setParent ..;
setParent ..;

proc ajTimeScaleTest()
{
    float $animInputInt = `floatField -q -value animInputInt`;
    int $timeScaleStartFrame = `intField -q -value timeScaleStartFrame`;
    int $timeScaleEndFrame = `intField -q -value timeScaleEndFrame`;
    if ($animInputInt == 0)
        ajInputError;
    int $i;
    string $selection[] = `ls -sl`;
    for ($i=0; $i<size($selection); $i=$i+1)
    {
        select -cl;
        select $selection[$i];
        float $rando = rand (($animInputInt * -1), $animInputInt);
        keyframe -time ($timeScaleStartFrame + ":" + $timeScaleEndFrame) -e -iub true -r -o over -tc $rando  ;
    }
    select -cl;
    select -r $selection;
}

proc ajCopyKeys()
{
    int $timeScaleStartFrame = `intField -q -value timeScaleStartFrame`;
    int $timeScaleEndFrame = `intField -q -value timeScaleEndFrame`;
    
    copyKey -time ($timeScaleStartFrame + ":" + $timeScaleEndFrame) -option keys -hierarchy none -controlPoints 0 -shape 1;
}

proc ajRevertKeys()
{
    int $timeScaleStartFrame = `intField -q -value timeScaleStartFrame`;
    int $timeScaleEndFrame = `intField -q -value timeScaleEndFrame`;
    
    cutKey -clear -time ($timeScaleStartFrame + ":" + $timeScaleEndFrame) -option keys -hierarchy none -controlPoints 0 -shape 1;
    pasteKey -option replace -copies 1 -connect 0 -timeOffset 0 -floatOffset 0 -valueOffset 0;
}

proc ajReseed()
{
    float $animInputInt = `floatField -q -value animInputInt`;
    int $timeScaleStartFrame = `intField -q -value timeScaleStartFrame`;
    int $timeScaleEndFrame = `intField -q -value timeScaleEndFrame`;
    if ($animInputInt == 0)
        ajInputError;
    int $i;
    string $selection[] = `ls -sl`;
    cutKey -clear -time ($timeScaleStartFrame + ":" + $timeScaleEndFrame) -option keys -hierarchy none -controlPoints 0 -shape 1;
    pasteKey -option replace -copies 1 -connect 0 -timeOffset 0 -floatOffset 0 -valueOffset 0;
    for ($i=0; $i<size($selection); $i=$i+1)
    {
        select -cl;
        select $selection[$i];
        float $rando = rand (($animInputInt * -1), $animInputInt);
        keyframe -time ($timeScaleStartFrame + ":" + $timeScaleEndFrame) -e -iub true -r -o over -tc $rando  ;
    }
    select -cl;
    select -r $selection;
}
showWindow timeScale;