This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 10 - Pages: 1 - Last Post: 14 Jun 21, 19:07 Last Post By: davidekholm
RobM

Posts: 3,820
Registered: 4-Aug-2006
Sorting of keywords
Posted: 12 Jun 21, 21:46
 
  Click to reply to this thread Reply
For projects with a lot of keywords it might be nice to be able to have them sorted.

It can be done in the system console, with a (modified version of one of your scripts) Groovy script
int count = 0
for (AlbumObject ao : rootFolder.getDescendants()) {
	if(ao.getCategory() == Category.image) {
		Set keywords = new TreeSet(ao.getKeywordSet())
		if(!keywords.isEmpty())  {
			count ++
			//replace the original keywords with the newly sorted keywords 
			ao.setKeywords(se.datadosen.util.StringUtil.setToString(keywords))
			println("Sorted keywords for " + ao)
		}
	}
}
println("Processed " + count + " objects")

However, for an object with no keywords, keywords.isEmpty() returns false, printing out the keyword list shows []. This results in all objects being shown as processed. Strangely, similar code works when run as an external tool. Am I missing the obvious again?

davidekholm

Posts: 3,474
Registered: 18-Oct-2002
Re: Sorting of keywords
Posted: 14 Jun 21, 12:33   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Are you getting a different behavior when running it from the system console vs as an external tool?
RobM

Posts: 3,820
Registered: 4-Aug-2006
Re: Sorting of keywords
Posted: 14 Jun 21, 16:32   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
Are you getting a different behavior when running it from the system console vs as an external tool?
It is the same result. I also converted the script to .bsh and get the same result.

The similar code that works is converting a LinkedHashMap to a set and then testing if the set is empty.
Map xmpKeywordMap = new LinkedHashMap();
xmpKeywordMap = processXmpDir(xmpDir, xmpKeywordMap);
 
//Create a set out of xmpKeywordMap, then check if it is empty 
Set set = xmpKeywordMap.entrySet();
if(set.isEmpty()) {

The xmpKeywordMap contains file name as a key and keywords string as value.

It is a coding mystery, to me at least, but is easily got around by not counting objects processed, just saying 'Script done'.

I'll mark this as answered, as the main reason for posting was to see if a built in option to sort keywords would be of use generally.

Postscript:
This got started as a result of this bug post and the final script that I made, which reads in an xmp file with keywords and appends them to the matching album object (same base file name). In that script, part of of which is the 'working' code, all keywords are sorted alphabetically A-Za-z.
davidekholm

Posts: 3,474
Registered: 18-Oct-2002
Re: Sorting of keywords
Posted: 14 Jun 21, 16:47   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Well, any Set that prints as [] should really report isEmpty() as true. That's all I can say
RobM

Posts: 3,820
Registered: 4-Aug-2006
Re: Sorting of keywords
Posted: 14 Jun 21, 17:02   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
Well, any Set that prints as [] should really report isEmpty() as true. That's all I can say
I tried using .size() too, and that returned 1 for empty or one entry. It’s a mystery :)
davidekholm

Posts: 3,474
Registered: 18-Oct-2002
Re: Sorting of keywords
Posted: 14 Jun 21, 17:43   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
RobM wrote:
davidekholm wrote:
Well, any Set that prints as [] should really report isEmpty() as true. That's all I can say
I tried using .size() too, and that returned 1 for empty or one entry. It’s a mystery :)

It contains one empty string member. Odd, but a bug. I'll address it.
RobM

Posts: 3,820
Registered: 4-Aug-2006
Re: Sorting of keywords
Posted: 14 Jun 21, 18:06   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
RobM wrote:
davidekholm wrote:
Well, any Set that prints as [] should really report isEmpty() as true. That's all I can say
I tried using .size() too, and that returned 1 for empty or one entry. It’s a mystery :)

It contains one empty string member. Odd, but a bug. I'll address it.

In the meantime, for my script I can just check for size > 1, since you can't sort one keyword :)
JeffTucker

Posts: 8,216
Registered: 31-Jan-2006
Re: Sorting of keywords
Posted: 14 Jun 21, 18:07   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Well, you can, but the results are usually just about as exciting as juggling one tennis ball.
davidekholm

Posts: 3,474
Registered: 18-Oct-2002
Re: Sorting of keywords
Posted: 14 Jun 21, 18:23   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Now fixed in v24b21
RobM

Posts: 3,820
Registered: 4-Aug-2006
Re: Sorting of keywords
Posted: 14 Jun 21, 18:47   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
Now fixed in v24b21
Thanks. Should this stay as a tool/console script or is it worth building in as an option. If the former I’ll post in the tools forum.
davidekholm

Posts: 3,474
Registered: 18-Oct-2002
Re: Sorting of keywords
Posted: 14 Jun 21, 19:07   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Keep it as an external tool for now. I've also made a small change so getKeywordSet returns the keywords in the same order as getKeywords
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums