Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Devices

Notices

Reply
 
Thread Tools Search this Thread
Old 11-29-2016, 10:14 PM   #1
wishingstar
Enthusiast
wishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheese
 
Posts: 33
Karma: 1032
Join Date: Nov 2016
Device: Kobo Aura Edition 2, Lenovo Android 10
Question Send to device based on Tag

Hi,

I have the latest calibre (2.73.0) and a standard android 7" tablet (Android 4.4.2). I want to send my fiction books to ebooks/Fiction/ on the SD card, and my non-fiction books to ebooks/Non-Fiction/. I'm okay with the standard title-author file names inside those folders. I tried to play around with the template editor to get this done, after a few tries, here's where I am (it's still not doing what I want):

Code:
lookup(tags:Fiction,{Fiction/{title} - {authors}},{Non-Fiction/{title} - {authors}})
wishingstar is offline   Reply With Quote
Old 11-30-2016, 04:24 AM   #2
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: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
There are several problems with the template. First, in Single Function Mode you cannot use template functions outside of a template item ({ ... }). Second, in Single Function Mode you should not use template items embedded in other template items ({foo {bar}}). Third, lookup doesn't take a search, instead taking metadata field names. Finally, given what you are trying to do, lookup isn't the best function to use.

Given the complexity of what you are trying to do, you should use General Program Mode. That mode gives you much more control over function arguments, in particular permitting nested function calls. Also, I think that the str_in_list function is a better match than lookup.

This GPM template does what I think you want.
Code:
program:
	str_in_list(
		field('tags'), 
		',', 
		'Fiction', 
		template('Fiction/{title} - {authors}'), 
		template('Non-Fiction/{title} - {authors}'))
Just for completeness, the following GPM template is more efficient because it computes the title/author portion of the template one time and avoids using the rather slow function 'template'.
Code:
program:
	ta = strcat(field('title'), ' - ', field('authors'));
	str_in_list(
		field('tags'), 
		',', 
		'Fiction', 
		strcat('Fiction/', ta), 
		strcat('Non-Fiction/', ta))
Finally, the "template tester" helps enormously when developing complex templates. It lets you edit a template and gives you immediate feedback on the resulting value or any errors. The tester is a "hidden" feature, not usually shown. I used Preferences / Toolbars to add the tester to the "Context menu for the books in the calibre library". That way the tester is available by right-clicking on a book. NB: pressing OK instead of Cancel makes the tester remember the last template so you can easily try it on another book
chaley is offline   Reply With Quote
Advert
Old 11-30-2016, 08:44 PM   #3
wishingstar
Enthusiast
wishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheese
 
Posts: 33
Karma: 1032
Join Date: Nov 2016
Device: Kobo Aura Edition 2, Lenovo Android 10
Thanks Chaley! I appreciate the help.

Since you said the second is a more efficient template, I am using that one. I cleared my device and started copying some books over. I'm seeing a strange issue. One book, a collection of novellas, specifically: The 13 crimes of science fiction by Asimov and others, does not accurately copy to the Fiction subfolder, instead, it created this folder structure:

Code:
{rootLibraryFolder}/es/Fn/{title} - {authors}
I'm not sure why it's doing that, or where the es and Fn come from, I'm using your template as-is so I'm wondering if there's an additional argument to be added to the GPM template to account for certain irregular books? It's showing up in the correct structure in the template tester (Thanks for that great tip BTW!) which is even weirder!

I only copied a handful of books so far, but I'm double-checking in case there's an issue that should be fixed before more books are copied to the wrong folders

Last edited by wishingstar; 11-30-2016 at 08:46 PM. Reason: More info
wishingstar is offline   Reply With Quote
Old 12-01-2016, 03:53 AM   #4
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: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Calibre enforces a maximum path length when sending to devices. IIRC that max is around 225-250 characters. If a path is longer than that then calibre takes characters out of the middle of each path segment until the path "fits". In your case "Fiction" became "Fn". I suspect that the "es" was "ebooks".

You can use the "shorten" template function to ensure that the length of {title} - {authors} is less than some amount. Perhaps you might want to apply shorten separately to each of title and authors. Another possibility is to use only the first author. Up to you.
chaley is offline   Reply With Quote
Old 12-02-2016, 12:08 AM   #5
wishingstar
Enthusiast
wishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheese
 
Posts: 33
Karma: 1032
Join Date: Nov 2016
Device: Kobo Aura Edition 2, Lenovo Android 10
That made a lot of sense, thanks Chaley!

Here's how I worked around the problem, if anyone needs to do this or something similar.

I used chaley's second suggestion for the template in General Program Mode:

Code:
program:
	ta = strcat(field('title'), ' - ', field('authors'));
	str_in_list(
		field('tags'), 
		',', 
		'Fiction', 
		strcat('Fiction/', ta), 
		strcat('Non-Fiction/', ta))
I then created two custom columns, hidden from the main interface, but used in the template:

1st column:
Lookup name: firstauth
Column Heading: First Author
Type: Built from other columns
Template:
Code:
{authors:sublist(0, 1, & )}
2nd column:
Lookup name: serind
Column Heading: Series Index
Type: Built from other columns
Template:
Code:
{series_index:0>5.2f}
finally, I replaced the first line of the program with:
Code:
ta = test(field('series'),strcat(field('series'),'/',field('#serind'),' - ',field('#firstauth'), ' - ',field('title')), strcat(field('#firstauth'),' - ',field('title')));
Now here's what the entire program does:
1- checks if the book has a series, if yes, go to step 2, if not, go to step 4
2- if there is a series, it puts the book in a subfolder with the series name, the subfolder is inside Fiction or Non-Fiction depending on the tags.
3- name the file by using a five-digit number with leading zeroes, a decimal point, and 2 decimal places for the series index, then add a dash, the name of the first author, another dash, then the book title. End program!
4- if no series, the file goes into the main folder (fiction or non-fiction depending on the tags)
5- the file name is now simply the first author's name, a dash, and the book title. End Program!

Hope this help anyone who wants to do a similar thing in calibre. And thanks again chaley for the great insights into the inner workings of Calibre

Last edited by wishingstar; 12-02-2016 at 12:11 AM. Reason: realized i had the wrong flowchart in the program steps, added "End program" to clarify
wishingstar is offline   Reply With Quote
Advert
Old 03-15-2017, 09:33 AM   #6
wishingstar
Enthusiast
wishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheesewishingstar can extract oil from cheese
 
Posts: 33
Karma: 1032
Join Date: Nov 2016
Device: Kobo Aura Edition 2, Lenovo Android 10
Sorry to resurrect an old thread (i'll create a new one if this is against the rules). I recently had to wipe my hard drive and forgot about backing up Calibre config folder. I'm trying to recreate this but the template editor isn't recognizing the fields. I'm using Calibre 2.81.0

When I try to use the template, exactly as I posted above, I get the message:
The template contains no {fields}, so all books will have the same name. Is this OK?

The template doesn't look at any fields, it uses "Series" for what should be the book series, "Author" and "Title", and for some reason, series index defaults to 3.

Any pointers would be appreciated,
wishingstar is offline   Reply With Quote
Old 03-15-2017, 10:05 AM   #7
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: 11,774
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by wishingstar View Post
Sorry to resurrect an old thread (i'll create a new one if this is against the rules). I recently had to wipe my hard drive and forgot about backing up Calibre config folder. I'm trying to recreate this but the template editor isn't recognizing the fields. I'm using Calibre 2.81.0

When I try to use the template, exactly as I posted above, I get the message:
The template contains no {fields}, so all books will have the same name. Is this OK?

The template doesn't look at any fields, it uses "Series" for what should be the book series, "Author" and "Title", and for some reason, series index defaults to 3.

Any pointers would be appreciated,
Ignore the message. The 'constant value' test doesn't understand GPM templates. I will fix this.
chaley is offline   Reply With Quote
Reply

Tags
android, file management, send to device, template language


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Tags or tag-like column in Save To Disk/Send to Device BookJunkieLI Library Management 34 05-03-2015 05:45 AM
Send tag to device only if tag has more than 1 book? eosrose Calibre 0 01-29-2013 07:46 PM
Automatically tag based on column info? Iocane Library Management 3 09-10-2012 12:40 PM
Different Send to Device routine based on the library. iatheia Library Management 10 01-24-2012 12:06 PM
Send to device based on Metadata DavidTC Calibre 0 09-18-2011 01:18 PM


All times are GMT -4. The time now is 09:14 PM.


MobileRead.com is a privately owned, operated and funded community.