private void SaveToFile(bool saveAs)
        {
            if (_selectedTabScriptActions.Items.Count == 0)
            {
                Notify("You must have at least 1 automation command to save.", Color.Yellow);
                return;
            }

            int beginLoopValidationCount   = 0;
            int beginIfValidationCount     = 0;
            int tryCatchValidationCount    = 0;
            int retryValidationCount       = 0;
            int beginSwitchValidationCount = 0;

            foreach (ListViewItem item in _selectedTabScriptActions.Items)
            {
                if (item.Tag is BrokenCodeCommentCommand)
                {
                    Notify("Please verify that all broken code has been removed or replaced.", Color.Yellow);
                    return;
                }
                else if ((item.Tag is LoopCollectionCommand) || (item.Tag is LoopContinuouslyCommand) ||
                         (item.Tag is LoopNumberOfTimesCommand) || (item.Tag is BeginLoopCommand) ||
                         (item.Tag is BeginMultiLoopCommand))
                {
                    beginLoopValidationCount++;
                }
                else if (item.Tag is EndLoopCommand)
                {
                    beginLoopValidationCount--;
                }
                else if ((item.Tag is BeginIfCommand) || (item.Tag is BeginMultiIfCommand))
                {
                    beginIfValidationCount++;
                }
                else if (item.Tag is EndIfCommand)
                {
                    beginIfValidationCount--;
                }
                else if (item.Tag is BeginTryCommand)
                {
                    tryCatchValidationCount++;
                }
                else if (item.Tag is EndTryCommand)
                {
                    tryCatchValidationCount--;
                }
                else if (item.Tag is BeginRetryCommand)
                {
                    retryValidationCount++;
                }
                else if (item.Tag is EndRetryCommand)
                {
                    retryValidationCount--;
                }
                else if (item.Tag is BeginSwitchCommand)
                {
                    beginSwitchValidationCount++;
                }
                else if (item.Tag is EndSwitchCommand)
                {
                    beginSwitchValidationCount--;
                }

                //end loop was found first
                if (beginLoopValidationCount < 0)
                {
                    Notify("Please verify the ordering of your loops.", Color.Yellow);
                    return;
                }

                //end if was found first
                if (beginIfValidationCount < 0)
                {
                    Notify("Please verify the ordering of your ifs.", Color.Yellow);
                    return;
                }

                if (tryCatchValidationCount < 0)
                {
                    Notify("Please verify the ordering of your try/catch blocks.", Color.Yellow);
                    return;
                }

                if (retryValidationCount < 0)
                {
                    Notify("Please verify the ordering of your retry blocks.", Color.Yellow);
                    return;
                }

                if (beginSwitchValidationCount < 0)
                {
                    Notify("Please verify the ordering of your switch/case blocks.", Color.Yellow);
                    return;
                }
            }

            //extras were found
            if (beginLoopValidationCount != 0)
            {
                Notify("Please verify the ordering of your loops.", Color.Yellow);
                return;
            }

            //extras were found
            if (beginIfValidationCount != 0)
            {
                Notify("Please verify the ordering of your ifs.", Color.Yellow);
                return;
            }

            if (tryCatchValidationCount != 0)
            {
                Notify("Please verify the ordering of your try/catch blocks.", Color.Yellow);
                return;
            }

            if (retryValidationCount != 0)
            {
                Notify("Please verify the ordering of your retry blocks.", Color.Yellow);
                return;
            }

            if (beginSwitchValidationCount != 0)
            {
                Notify("Please verify the ordering of your switch/case blocks.", Color.Yellow);
                return;
            }

            //define default output path
            if (string.IsNullOrEmpty(ScriptFilePath) || (saveAs))
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog
                {
                    InitialDirectory = ScriptProjectPath,
                    RestoreDirectory = true,
                    Filter           = "Json (*.json)|*.json"
                };

                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                if (!saveFileDialog.FileName.ToString().Contains(ScriptProjectPath))
                {
                    Notify("An Error Occured: Attempted to save script outside of project directory", Color.Red);
                    return;
                }

                ScriptFilePath = saveFileDialog.FileName;
                string scriptFileName = Path.GetFileNameWithoutExtension(ScriptFilePath);
                if (uiScriptTabControl.SelectedTab.Text != scriptFileName)
                {
                    UpdateTabPage(uiScriptTabControl.SelectedTab, ScriptFilePath);
                }
            }

            //serialize script
            try
            {
                var exportedScript = Script.SerializeScript(_selectedTabScriptActions.Items, _scriptVariables, _scriptElements, ScriptFilePath);
                uiScriptTabControl.SelectedTab.Text = uiScriptTabControl.SelectedTab.Text.Replace(" *", "");
                //show success dialog
                Notify("File has been saved successfully!", Color.White);

                try
                {
                    ScriptProject.SaveProject(ScriptFilePath);
                }
                catch (Exception ex)
                {
                    Notify(ex.Message, Color.Red);
                }
            }
            catch (Exception ex)
            {
                Notify("An Error Occured: " + ex.Message, Color.Red);
            }
        }
        private void tsmiRenameFile_Click(object sender, EventArgs e)
        {
            try
            {
                string selectedNodePath = tvProject.SelectedNode.Tag.ToString();
                string selectedNodeName = tvProject.SelectedNode.Text.ToString();
                string selectedNodeNameWithoutExtension = Path.GetFileNameWithoutExtension(selectedNodeName);

                if (selectedNodeName != "project.obconfig")
                {
                    FileInfo selectedNodeDirectoryInfo = new FileInfo(selectedNodePath);

                    string newName     = "";
                    var    newNameForm = new frmInputBox("Enter the new name of the file WITH extension", "Rename File");
                    newNameForm.txtInput.Text = selectedNodeDirectoryInfo.Name;
                    newNameForm.ShowDialog();

                    if (newNameForm.DialogResult == DialogResult.OK)
                    {
                        newName = newNameForm.txtInput.Text;
                        newNameForm.Dispose();
                    }
                    else if (newNameForm.DialogResult == DialogResult.Cancel)
                    {
                        newNameForm.Dispose();
                        return;
                    }

                    if (!Path.HasExtension(newName))
                    {
                        throw new FileFormatException($"No extension provided for '{newName}'");
                    }

                    string newPath = Path.Combine(selectedNodeDirectoryInfo.DirectoryName, newName);

                    bool isInvalidProjectName = new[] { @"/", @"\" }.Any(c => newName.Contains(c));
                    if (isInvalidProjectName)
                    {
                        throw new Exception("Illegal characters in path");
                    }

                    if (File.Exists(newPath))
                    {
                        throw new Exception("A file with this name already exists");
                    }

                    var foundTab = uiScriptTabControl.TabPages.Cast <TabPage>().Where(t => t.ToolTipText == selectedNodePath)
                                   .FirstOrDefault();

                    if (foundTab != null)
                    {
                        DialogResult result = CheckForUnsavedScript(foundTab);
                        if (result == DialogResult.Cancel)
                        {
                            return;
                        }

                        uiScriptTabControl.TabPages.Remove(foundTab);
                    }

                    FileSystem.Rename(selectedNodePath, newPath);

                    if (selectedNodeName == _mainFileName)
                    {
                        string newMainName = Path.GetFileName(newPath);
                        _mainFileName      = newMainName;
                        ScriptProject.Main = newMainName;
                        ScriptProject.SaveProject(newPath);
                    }

                    tvProject.SelectedNode.Name = newName;
                    tvProject.SelectedNode.Text = newName;
                    tvProject.SelectedNode.Tag  = newPath;
                }
            }
            catch (Exception ex)
            {
                Notify("An Error Occured: " + ex.Message, Color.Red);
            }
        }