View Single Post
Old 07-12-2023, 02:16 PM   #1125
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Question: Is there a method to add a file to the extra data folder similar to SFE on 'formats'?

Context: I have a chain that copies the cover path to clipboard then follows it with the 'add data file' Calibre action where I paste it in. It's a little clunky though.
You can do this with a python template. It would probably be better as an Action Chains python action but I didn't feel like doing that.

Code:
python:
def evaluate(book, context):
	import os
	from shutil import copy
	from calibre.db.constants import DATA_DIR_NAME

	db = context.db
	cache = db.new_api
	# Get the normalized library path
	library_path = cache.backend.library_path
	
    # Construct the full path to the book folder
	path = os.path.join(library_path, cache.field_for('path', book.id).replace('/', os.sep))

	# Ensure the data directory exists
	data_dir = os.path.join(path, DATA_DIR_NAME)
	if not os.path.exists(data_dir):
		os.mkdir(data_dir)

	cover_file = os.path.join(path, 'cover.jpg')
	if os.path.exists(cover_file):
		# It does. Copy it to the data directory. The 'copy' method takes a
		# directory as a target.
		copy(cover_file, data_dir)
		return 'copied'
	return 'not copied'
chaley is offline   Reply With Quote