Citavi批量制作Obsidian前导区

Citavi批量制作Obsidian前导区

文献精读笔记需要一个模板,方便溯源:
下面是我按模板设置好的开头,这里全部都是citavi自动生成的:

其中:
Cite是可以自定义citavi Style打印
File是citavi条目中所有的pdf, excel和word,可以点击用默认应用打开
Style可以根据影响因子和分区范围,显示不同的颜色。我设置的区段是<3,3-5,5-10,>10。
ShortNote是我的文献备注说明
后面就是论文精读笔记了。

那么这样的模板该怎么做呢,思路就是,用Citavi的宏读取所有相关字段,用宏脚本DebugMacro.WriteLine()打印成markdown格式,然后复制粘贴过来。
具体如下:

1、前置需求:

使用Citavi的宏翻译标题和摘要 - 知乎 (zhihu.com)
使用Citavi的宏配合easyScholar获取期刊信息 - 知乎 (zhihu.com)
使用这两个方法获取条目的翻译和影响因子信息。

2、整合信息:

打开宏脚本器,用宏脚本运行之后,复制粘贴到obsidian即可:

下面是宏脚本,建议有动手能力的人尝试,注意可能需要设置styleName

using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using SwissAcademic.Citavi;
using SwissAcademic.Citavi.Metadata;
using SwissAcademic.Citavi.Shell;
using SwissAcademic.Collections;
using SwissAcademic.Citavi.Citations;

using System.Net;
using System.Text;
using System.Text.RegularExpressions;
// 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()
	{
		//Get the active project
		Project project = Program.ActiveProjectShell.Project;
		
		//Get the active ("primary") MainForm
		MainForm mainForm = Program.ActiveProjectShell.PrimaryMainForm;
		//reference to active Project
        Project activeProject = Program.ActiveProjectShell.Project;
		List<Reference> references = mainForm.GetSelectedReferences();
		foreach (Reference reference in references)
		{
			//	Cite
			string newShortTitle = null;
			newShortTitle = NewShortTitle(reference);
			// DebugMacro.WriteLine(newShortTitle);
			DebugMacro.WriteLine("> Cite:: "+newShortTitle);
			
			// 路径打印:
	        List<Location> refLocation = reference.Locations.ToList();
			string pathString = "";
            foreach (Location location in refLocation)
            {
                if (reference.Locations == null) continue;
				if (string.IsNullOrEmpty(location.Address.ToString())) continue;
				if (location.LocationType != LocationType.ElectronicAddress) continue;
				string filePath = location.Address.ToString();
				//reference.Locations.Remove
				// && filePath.StartsWith(@"\\"))
				if (filePath.EndsWith(".pdf") || filePath.EndsWith(".xlsx") || filePath.EndsWith(".docx") || filePath.EndsWith(".doc") || filePath.EndsWith(".xls"))
				{
					string path = location.Address.Resolve().LocalPath;
					path = path.Replace(" ","%20");
					string thisString = "["+filePath+"]"+"(file:///"+ path +");";
					pathString = pathString+thisString;

				}
            }
			DebugMacro.WriteLine("> File:: "+pathString);
			// Style高亮设置
			string color3 = "rgb(248, 238, 205)";
			string color35 = "rgb(219, 237, 219)";
			string color510 = "rgb(232, 222, 238)";
			string color10 = "rgb(255, 226, 221)";
			// 使用Parse方法进行转换,如果转换失败会抛出异常
			float IF = float.Parse(reference.CustomField2.Replace("IF: ",""));
			string styleString = "";
			if (IF < 3){
				styleString = "> Style:: <span style=\"background-color: "+ color3+ ";\">IF "+ IF+"</span>";
			}else if(IF >=3 && IF <5){
				styleString = "> Style:: <span style=\"background-color: "+ color35+ ";\">IF "+ IF+"</span>";
			}else if(IF >=5 && IF <10){
				styleString = "> Style:: <span style=\"background-color: "+ color510+ ";\">IF "+ IF+"</span>";
			}else if(IF >=10){
				styleString = "> Style:: <span style=\"background-color: "+ color10+ ";\">IF "+ IF+"</span>";
			}
			// 分区
			string partmy = reference.CustomField4;
			if (partmy.Contains("4区")){
				styleString = styleString + " <span style=\"background-color: "+ color3+ ";\">"+ partmy+"</span>";
			}else if(partmy.Contains("3区")){
				styleString = styleString + " <span style=\"background-color: "+ color35+ ";\">"+ partmy+"</span>";
			}else if(partmy.Contains("2区")){
				styleString = styleString + " <span style=\"background-color: "+ color510+ ";\">"+ partmy+"</span>";
			}else if(partmy.Contains("1区")){
				styleString = styleString + " <span style=\"background-color: "+ color10+ ";\">"+ partmy+"</span>";
			}
			DebugMacro.WriteLine(styleString);
			
			// 作者
			string result= "";
            List<Person> refPersons = reference.Authors.ToList();
            List<string> nameString5 = new List<string>(); // 创建一个空的 List<string>
            foreach (Person person in refPersons)
            {
                if (reference.Authors == null) continue;
                nameString5.Add(person.FullName);
            }
            // 将 List 转换为数组后连接起来
			string[] strings = nameString5.ToArray();
			result = string.Join("; ", strings);
			DebugMacro.WriteLine("> Author:: "+result.Replace(",",""));
			
			// 其他
			DebugMacro.WriteLine("> Year:: "+reference.Year);
			DebugMacro.WriteLine("> DOI:: "+reference.Doi);
			DebugMacro.WriteLine("> IF:: =="+reference.CustomField2.Replace("IF: ","")+"==");
			DebugMacro.WriteLine("> 中科院分区升级版:: =="+reference.CustomField4 +"==");
			DebugMacro.WriteLine("> 5年影响因子:: "+reference.CustomField5.Replace("5年影响因子: ",""));
			DebugMacro.WriteLine("> JCR分区:: "+reference.CustomField3);
			DebugMacro.WriteLine("> Journal:: "+reference.Periodical.ToString());
			DebugMacro.WriteLine("> journalAbbreviation:: "+reference.Periodical.UserAbbreviation1.ToString());
			DebugMacro.WriteLine("> ShortNote:: "+reference.CustomField1);
			
			DebugMacro.WriteLine("> titleTranslation:: "+reference.TranslatedTitle);
			DebugMacro.WriteLine("> abstractTranslation:: "+reference.TableOfContents.Text);
			

		}
	
	}
	
	// 此函数是用来打印相应cite引用的,需要设置styleName!!
	public static string NewShortTitle(Reference reference)
	{
		try
		{
			if (reference == null) return null;

			string newShortTitle = string.Empty;

			Project project = Program.ActiveProjectShell.Project;

			string styleName = "CitaviDefaultCitationStyle.ccs"; // 在这里修改你的参考文献样式	
			// 这是citavi系统style文件夹
			string folder = project.Addresses.GetFolderPath(CitaviFolder.CitationStyles).ToString(); 
			string fullPath = folder + @"\" + styleName;
			
			// 这是用户style
			//string folder = project.Addresses.GetFolderPath(CitaviFolder.CustomCitationStyles).ToString(); 
			//DebugMacro.WriteLine(project.Addresses.GetFolderPath(CitaviFolder.CustomCitationStyles).ToString());
			//string fullPath = folder + @"\" + styleName;
			
			if (!System.IO.File.Exists(fullPath))
			{
				MessageBox.Show("没有Style");
				return null;
			}
			Uri uri = new Uri(fullPath);
			CitationStyle citationStyle = CitationStyle.Load(uri.AbsoluteUri); // 根据路径加载style

			List<Reference> references = new List<Reference>();
			references.Add(reference);
			CitationManager citationManager = new CitationManager(Program.ActiveProjectShell.Project.Engine, citationStyle, references);
			if (citationManager == null) return null;
			BibliographyCitation bibliographyCitation = citationManager.BibliographyCitations.FirstOrDefault();
			if (bibliographyCitation == null) return null;
			List<ITextUnit> textUnits = bibliographyCitation.GetTextUnits();
			if (textUnits == null) return null;

			var output = new TextUnitCollection();

			foreach (ITextUnit textUnit in textUnits)
			{
				output.Add(textUnit);
			}

			newShortTitle = output.ToString();
			return newShortTitle;
		}
		catch { return null; }
	}

}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值