Skip to content

Instantly share code, notes, and snippets.

@SchmidtPaul
Created January 21, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SchmidtPaul/9b381ad91d6b6466c8340c497fee7538 to your computer and use it in GitHub Desktop.
Save SchmidtPaul/9b381ad91d6b6466c8340c497fee7538 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using SwissAcademic.Citavi;
using SwissAcademic.Citavi.Metadata;
using SwissAcademic.Citavi.Shell;
using SwissAcademic.Collections;
using SwissAcademic.Citavi.DataExchange;
// Implementation of macro editor is preliminary and experimental.
// The Citavi object model is subject to change in future version.
public static class CitaviMacro
{
public static void Main()
{
Project project = Program.ActiveProjectShell.Project; //Get the active project
MainForm mainForm = Program.ActiveProjectShell.PrimaryMainForm; //Get the active ("primary") MainForm
ProjectReferenceCollection references = project.References; //if this macro should ALWAYS affect all titles in active project, choose:
// Create List with all categories
List<Category> allCategories = project.AllCategories.ToList();
// Run through references
foreach (Reference reference in references)
{
DebugMacro.WriteLine(reference.ShortTitle);
// DebugMacro.WriteLine(reference.Categories);
string setCategoryFullString = "Kat1; Kat2";
// Split setCategoryFullString into category list setCategoryList
List<string> setCategoryList = setCategoryFullString.Split(new String[]{"; "}, StringSplitOptions.RemoveEmptyEntries).ToList();
setCategoryList.RemoveAll(string.IsNullOrWhiteSpace);
foreach (string setCategoryString in setCategoryList)
{
// Add category for this reference
List<Category> categoriesToAdd = new List<Category>();
var addThisCategory = allCategories.Where(item => item.FullName.Equals(setCategoryString, StringComparison.InvariantCulture)).FirstOrDefault();
categoriesToAdd.Add(addThisCategory);
if(addThisCategory != null && !reference.Categories.Contains(addThisCategory))
{
reference.Categories.AddRange(categoriesToAdd); // Add categories to this reference
DebugMacro.WriteLine(" Set to category " + addThisCategory);
}
else if (reference.Categories.Contains(addThisCategory))
{
DebugMacro.WriteLine(" is already category " + addThisCategory);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment