Zotero to Word sources.xml using bibutils
Hi there,
this is my first post here. A quick search did not reveal any apparent duplicates, so I thought I would share the following timesaver.
I was writing my master thesis and needed to work with Word. Thanks to a special literary format provided by my university for Word I needed to use the integrated literature management in Word. I could not use the Zotero Plugin.
Since I wanted to use Zotero to manage my literature though I needed a way to import my zotero library in Word as easily as possible. So I wrote an Applescript with the help of ChatGPT that makes use of the bibutils that run natively on MacOS. All I need to do now is select all the elements in Zotero I need in Word and right-click -> export -> export as RIS (max. amount of attributes retained)
So what does the script do?
1. It takes a file with one of the following endings:
.ris, .bib, .enl, .nbib, .medline, .isi, .xml, .txt
2. it converts the file to the MODS format
3. it converts the MODS format to the OpenXML-Format Word uses for the sources.xml file.
4. it copies the file to the correct location for Word on MacOS Sequoia with Word installed via a 365 Subscription deleting the original file, the existing sources.xml and deleting any intermediate files created.
I hope someone else can profit from this.
Constructive feedback welcome. :)
If you want to use this, just create a new Apple Script using Script Editor and paste the following. Don't forget to adjust the Variables up top, like your username and the location of the installed bibutils (https://formulae.brew.sh/formula/bibutils).
The Script will request access to other apps' data due to the target path lying in words territory.
-- Code Begins --
-- User Editable Variables:
-- Change the following variables as needed.
set defaultRISFileName to "Exported Entries.ris" -- Default file name in the script directory
-- Replace with your username
set outputDir to "/Users//Library/Containers/com.microsoft.Word/Data/Library/Application Support/Microsoft/Office"
set bibutilsPath to "/opt/homebrew/bin/" -- Path to bibutils tools
set fileFormatsPrompt to "Choose a file to convert. Supported formats: .ris, .bib, .enl, .nbib, .medline, .isi, .xml, .txt:"
-- Get the path of the current script directory
set scriptPath to (path to me as text)
set scriptDir to POSIX path of (do shell script "dirname " & quoted form of (POSIX path of scriptPath))
set defaultFilePath to scriptDir & "/" & defaultRISFileName
-- Initialize variables
set fileChosenManually to false
set inputFilePath to ""
-- Check if the default file exists
if (do shell script "test -e " & quoted form of defaultFilePath & " && echo exists || echo not_exists") is "exists" then
-- File found, set path
set inputFilePath to defaultFilePath
else
-- File not found, open file selection dialog
set fileChosenManually to true
set inputFile to choose file with prompt fileFormatsPrompt
set inputFilePath to POSIX path of inputFile
end if
-- Get directory and file name of the input file
set inputDir to POSIX path of (do shell script "dirname " & quoted form of inputFilePath)
set inputFileName to (do shell script "basename " & quoted form of inputFilePath)
set fileExtension to do shell script "echo " & quoted form of inputFileName & " | awk -F. '{print tolower($NF)}'"
-- Generate output path and ensure the output directory exists
do shell script "mkdir -p " & quoted form of outputDir
set outputFilePath to outputDir & "/Sources.xml"
-- Function to detect file format based on content
on detectFileFormat(filePath, extension)
try
-- Read the first few lines of the file
set fileContent to do shell script "head -n 10 " & quoted form of filePath
-- Check for specific tags or namespaces
if extension is "xml" and fileContent contains " " & quoted form of intermediateModsFilePath
do shell script bibutilsPath & "xml2wordbib " & quoted form of intermediateModsFilePath & " > " & quoted form of outputFilePath
do shell script "rm -f " & quoted form of intermediateModsFilePath
-- Step 2: Replace with in the output XML
do shell script "sed -i '' 's///g; s/<\\/b:Url>/<\\/b:URL>/g' " & quoted form of outputFilePath
-- Delete the selected file by moving it to Trash
do shell script "mv " & quoted form of inputFilePath & " ~/.Trash/"
-- Success message
display alert "Conversion Successful!" message "The original file (and intermediate file) has been removed:
" & inputFilePath & "
The converted file has been exported to:
" & outputFilePath buttons {"OK"} default button 1 as informational
on error errMsg
display alert "Conversion Error!" message errMsg buttons {"OK"} default button 1 as critical
end try
-- Code Ends --
this is my first post here. A quick search did not reveal any apparent duplicates, so I thought I would share the following timesaver.
I was writing my master thesis and needed to work with Word. Thanks to a special literary format provided by my university for Word I needed to use the integrated literature management in Word. I could not use the Zotero Plugin.
Since I wanted to use Zotero to manage my literature though I needed a way to import my zotero library in Word as easily as possible. So I wrote an Applescript with the help of ChatGPT that makes use of the bibutils that run natively on MacOS. All I need to do now is select all the elements in Zotero I need in Word and right-click -> export -> export as RIS (max. amount of attributes retained)
So what does the script do?
1. It takes a file with one of the following endings:
.ris, .bib, .enl, .nbib, .medline, .isi, .xml, .txt
2. it converts the file to the MODS format
3. it converts the MODS format to the OpenXML-Format Word uses for the sources.xml file.
4. it copies the file to the correct location for Word on MacOS Sequoia with Word installed via a 365 Subscription deleting the original file, the existing sources.xml and deleting any intermediate files created.
I hope someone else can profit from this.
Constructive feedback welcome. :)
If you want to use this, just create a new Apple Script using Script Editor and paste the following. Don't forget to adjust the Variables up top, like your username and the location of the installed bibutils (https://formulae.brew.sh/formula/bibutils).
The Script will request access to other apps' data due to the target path lying in words territory.
-- Code Begins --
-- User Editable Variables:
-- Change the following variables as needed.
set defaultRISFileName to "Exported Entries.ris" -- Default file name in the script directory
-- Replace with your username
set outputDir to "/Users//Library/Containers/com.microsoft.Word/Data/Library/Application Support/Microsoft/Office"
set bibutilsPath to "/opt/homebrew/bin/" -- Path to bibutils tools
set fileFormatsPrompt to "Choose a file to convert. Supported formats: .ris, .bib, .enl, .nbib, .medline, .isi, .xml, .txt:"
-- Get the path of the current script directory
set scriptPath to (path to me as text)
set scriptDir to POSIX path of (do shell script "dirname " & quoted form of (POSIX path of scriptPath))
set defaultFilePath to scriptDir & "/" & defaultRISFileName
-- Initialize variables
set fileChosenManually to false
set inputFilePath to ""
-- Check if the default file exists
if (do shell script "test -e " & quoted form of defaultFilePath & " && echo exists || echo not_exists") is "exists" then
-- File found, set path
set inputFilePath to defaultFilePath
else
-- File not found, open file selection dialog
set fileChosenManually to true
set inputFile to choose file with prompt fileFormatsPrompt
set inputFilePath to POSIX path of inputFile
end if
-- Get directory and file name of the input file
set inputDir to POSIX path of (do shell script "dirname " & quoted form of inputFilePath)
set inputFileName to (do shell script "basename " & quoted form of inputFilePath)
set fileExtension to do shell script "echo " & quoted form of inputFileName & " | awk -F. '{print tolower($NF)}'"
-- Generate output path and ensure the output directory exists
do shell script "mkdir -p " & quoted form of outputDir
set outputFilePath to outputDir & "/Sources.xml"
-- Function to detect file format based on content
on detectFileFormat(filePath, extension)
try
-- Read the first few lines of the file
set fileContent to do shell script "head -n 10 " & quoted form of filePath
-- Check for specific tags or namespaces
if extension is "xml" and fileContent contains " " & quoted form of intermediateModsFilePath
do shell script bibutilsPath & "xml2wordbib " & quoted form of intermediateModsFilePath & " > " & quoted form of outputFilePath
do shell script "rm -f " & quoted form of intermediateModsFilePath
-- Step 2: Replace with in the output XML
do shell script "sed -i '' 's///g; s/<\\/b:Url>/<\\/b:URL>/g' " & quoted form of outputFilePath
-- Delete the selected file by moving it to Trash
do shell script "mv " & quoted form of inputFilePath & " ~/.Trash/"
-- Success message
display alert "Conversion Successful!" message "The original file (and intermediate file) has been removed:
" & inputFilePath & "
The converted file has been exported to:
" & outputFilePath buttons {"OK"} default button 1 as informational
on error errMsg
display alert "Conversion Error!" message errMsg buttons {"OK"} default button 1 as critical
end try
-- Code Ends --
Because for some reason, less attributes are exported (or imported) correctly using mods directly. Using the two step approach worked better.
I did an analysis using all supported formats of bibutils and using RIS resulted in the most attributes being transferred correctly.