using System; using System.Net; using System.Collections; using System.Collections.Specialized; using System.Web; using System.Threading; using System.Threading.Tasks; using System.IO; using System.Linq; using System.ComponentModel; using System.Collections.Generic; using System.Windows.Forms; using SwissAcademic.Citavi; using SwissAcademic.Citavi.Metadata; using SwissAcademic.Citavi.Shell; using SwissAcademic.Collections; public static class CitaviMacro { public static void Main() { int counter = 0; int counterTotal = 0; //if this macro should ALWAYS affect all titles in active project, choose first option //if this macro should affect just filtered rows if there is a filter applied and ALL if not, choose second option //ProjectReferenceCollection references = Program.ActiveProjectShell.Project.References; List references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); //if we need a ref to the active project SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project; foreach (Reference reference in references) { counterTotal += 1; ReferenceType referenceType = reference.ReferenceType; if(reference.Locations.Count>1){ // skipp all where there are already 2 locations (1 online, 1 pdf) MessageBox.Show(string.Format("SKIPPING: \n{0}\n{1}\n{2}\n",reference.ToString(),reference.BibTeXKey.ToString(),reference.Locations.Count.ToString())); continue; } foreach (Location location in reference.Locations) { //if(location.LocationType != LocationType.ElectronicAddress){ // counterTotal += 1; //} if (location == null) return; if (location.Address == null) continue; if (location.Address.UriString == null) continue; //MessageBox.Show(string.Format("cur: \n{0}\n{1}",reference.ToString(),reference.Locations.Count)); if (location.LocationType == LocationType.ElectronicAddress) { if(location.Address.ToString().StartsWith("http") || location.Address.ToString().StartsWith("www")){ // try A string pdfTarget = @"C:\Users\xxx\Desktop\aaa\" + location.Address.UriString.Substring(location.Address.UriString.LastIndexOf("/")+1)+".pdf"; // this would need some conversion? //File.Create(p); //PdfExportOptions a = new PdfExportOptions(); MessageBox.Show(string.Format("Will Download for \n{0}\n{1}\n{2}\n{3}",reference.ToString(),location.Address.ToString(),reference.BibTeXKey.ToString(),pdfTarget)); // try B System.Threading.CancellationToken ct = new System.Threading.CancellationToken(); IProgress progress = new Progress(); System.Threading.Tasks.Task t = location.DownloadFullTextExtractionAsync(progress,ct); //Console.WriteLine("taskA Status: {0}", taskA.Status); try { t.RunSynchronously(); t.Start(); t.Wait(); } catch (AggregateException) { MessageBox.Show(string.Format("error \n{0}","unknown")); } counter +=1; MessageBox.Show(string.Format("done for \n{0}\n{1}\n{2}",reference.ToString(),location.Address.ToString(),t.Status.ToString())); // try C //var fbd = new FolderBrowserDialog(); //DialogResult result = fbd.ShowDialog(); //p = fbd.SelectedPath; // try ??? //location.ExportPdf(p,a, true); //client.OpenRead(location.Address); //client.DownloadFile(location.Address, p); } } } // MessageBox.Show(string.Format("cur: ",reference.Locations[0].AddressUri); if (counterTotal>3){ break; } } MessageBox.Show(string.Format("Macro has finished execution.\r\n{0} changes were made (total {1} touched)", counter.ToString(), counterTotal.ToString()), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); } }