Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 11-29-2016, 11:14 PM   #1
wishingstar
Member
wishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-books
 
Posts: 10
Karma: 966
Join Date: Nov 2016
Device: Android Tablet
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
Advert
Old 11-30-2016, 05:24 AM   #2
chaley
CC Android & calibre dev
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: 8,528
Karma: 2569714
Join Date: Jan 2010
Location: France
Device: Many android devices
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
Old 11-30-2016, 09:44 PM   #3
wishingstar
Member
wishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-books
 
Posts: 10
Karma: 966
Join Date: Nov 2016
Device: Android Tablet
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 09:46 PM. Reason: More info
wishingstar is offline   Reply With Quote
Old 12-01-2016, 04:53 AM   #4
chaley
CC Android & calibre dev
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: 8,528
Karma: 2569714
Join Date: Jan 2010
Location: France
Device: Many android devices
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, 01:08 AM   #5
wishingstar
Member
wishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-books
 
Posts: 10
Karma: 966
Join Date: Nov 2016
Device: Android Tablet
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 01: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, 10:33 AM   #6
wishingstar
Member
wishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-bookswishingstar has learned how to read e-books
 
Posts: 10
Karma: 966
Join Date: Nov 2016
Device: Android Tablet
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, 11:05 AM   #7
chaley
CC Android & calibre dev
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: 8,528
Karma: 2569714
Join Date: Jan 2010
Location: France
Device: Many android devices
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:45 AM
Send tag to device only if tag has more than 1 book? eosrose Calibre 0 01-29-2013 08:46 PM
Automatically tag based on column info? Iocane Library Management 3 09-10-2012 01:40 PM
Different Send to Device routine based on the library. iatheia Library Management 10 01-24-2012 01:06 PM
Send to device based on Metadata DavidTC Calibre 0 09-18-2011 02:18 PM


All times are GMT -4. The time now is 04:20 PM.


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