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


Permlink Replies: 17 - Pages: 2 [ 1 2 | Next ] - Last Post: 8 Nov 22, 16:56 Last Post By: davidekholm
hspagnolo

Posts: 101
Registered: 23-Mar-2016
Replace a keyword by another one into an entire album
Posted: 22 Apr 21, 14:43
 
  Click to reply to this thread Reply
Hi,

I am not at all able to write a .bsh file, so I am requesting here. Would it be possible to have one tool to replace a keyword by another one for all pictures of an entire album (or at least folder), eg: "color" by "b/w"?

Thanks and advanced and regards
Henri

Edited by: RobM on 22 Apr 2021, 19:46
Moved to General discussion forum, to keep the external tools forum just for actual tools and plugins
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Replace a keyword by another one into an entire album
Posted: 22 Apr 21, 18:48   in response to: hspagnolo in response to: hspagnolo
 
  Click to reply to this thread Reply
hspagnolo wrote:
Hi,

I am not at all able to write a .bsh file, so I am requesting here. Would it be possible to have one tool to replace a keyword by another one for all pictures of an entire album (or at least folder), eg: "color" by "b/w"?

Thanks and advanced and regards
Henri

Open the System console and in the top part paste
import net.jalbum.util.*;
 
MonitoredWorkQueue queue = new MonitoredWorkQueue(window, "Changing keywords");
queue.submit () -> {
  search = "Test";
  replace = "Tested";
  rootFolder.descendants.forEach (ao) -> {
    if (ao.isFolder()) queue.message = ao.parent.name;
    aoKeywords = ao.getKeywords();
   if(aoKeywords.indexOf(search) !=-1)  {
   	System.out.println(ao.getName() + " contains " + search + ", replacing with " + replace);
   	aoKeywords = aoKeywords.replaceAll(search, replace);
   	ao.setKeywords(aoKeywords);
   }
    if (queue.isAborted()) throw new OperationAbortedException();
  }
}
queue.awaitCompletion();

Make sure the 'Scripting language' is set to 'Groovy (Groovy scripting engine).
In the code above replace the search and replace strings (Test and Tested) with the keyword to be replaced and the replacement. Then click on the 'Execute' button to run the code.

The above is based on the 'Look up keywords.groovy' code provided by David. I tested it on the Sample Portfolio and it worked fine, you might want to do the same before trying it on your own project.

Edited by: RobM on 22 Apr 2021, 19:48
Added execute button!
hspagnolo

Posts: 101
Registered: 23-Mar-2016
Re: Replace a keyword by another one into an entire album
Posted: 22 Apr 21, 18:53   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Hi Rob,

Thanks a lot, I will tested it ASAP. This is really appreciated.

Best regards
Henri
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Replace a keyword by another one into an entire album
Posted: 22 Apr 21, 20:44   in response to: hspagnolo in response to: hspagnolo
 
  Click to reply to this thread Reply
Just be aware that the search and replace will affect other keywords that have the search term as a substring. That is, if you searched for 'colour' and replaced it with 'black and white' it would also affect 'coloured', which would become 'black and whiteed'.
hspagnolo

Posts: 101
Registered: 23-Mar-2016
Re: Replace a keyword by another one into an entire album
Posted: 23 Apr 21, 07:30   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Thanks for the warning Rob. The replacement should be OK as the keyword is in French and won't have any other declination.

Again thanks for this, you save me hours of work.
AndreasHGW

Posts: 50
Registered: 21-Sep-2014
Re: Replace a keyword by another one into an entire album
Posted: 10 Jun 21, 22:17   in response to: hspagnolo in response to: hspagnolo
 
  Click to reply to this thread Reply
Thanks for this script, in some cases it works fine. But in some cases I have problems.

I have keywords like 1963, 1963-2000, ...

I want to replace 1963 with 1961-1970. This works fine for pictures with keywords 1963 only.
But pictures with the keyword 1963-2000 will be replaced with 1961-1970-2000.

How can I prevent this?
davidekholm

Posts: 3,462
Registered: 18-Oct-2002
Re: Replace a keyword by another one into an entire album
Posted: 10 Jun 21, 22:45   in response to: AndreasHGW in response to: AndreasHGW
 
  Click to reply to this thread Reply
It's better to not manipulate strings but instead sets of strings. Here's a code that replaces "David" with "Sam" and avoids the problem you've mentioned. Just paste and run it from the System Console (F7). It uses Groovy as scripting language. If you wish to undo, just hit undo 2-3 times:
Work.on(rootFolder.descendants)
.forEach(ao) -> {
	Set keywords = ao.keywordSet
	if (keywords.remove("David")) {
		keywords.add("Sam")
		ao.keywords = StringUtil.setToString(keywords)
	}
}
AndreasHGW

Posts: 50
Registered: 21-Sep-2014
Re: Replace a keyword by another one into an entire album
Posted: 11 Jun 21, 17:45   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thank you, that works. Is it possible to add a line in the script to show in which pictures the script has replace keywords?

Andreas
davidekholm

Posts: 3,462
Registered: 18-Oct-2002
Re: Replace a keyword by another one into an entire album
Posted: 12 Jun 21, 09:25   in response to: AndreasHGW in response to: AndreasHGW
 
  Click to reply to this thread Reply
Sure,
Work.on(rootFolder.descendants)
.forEach(ao) -> {
	Set keywords = ao.keywordSet
	if (keywords.remove("David")) {
		keywords.add("Sam")
		ao.keywords = StringUtil.setToString(keywords)
		println "Updated " + ao
	}
}
AndreasHGW

Posts: 50
Registered: 21-Sep-2014
Re: Replace a keyword by another one into an entire album
Posted: 12 Jun 21, 22:31   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thanks again, that's great.

Last question: Is there any possibility to save the script for the next use?

best regards
Andreas
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Replace a keyword by another one into an entire album
Posted: 13 Jun 21, 21:48   in response to: AndreasHGW in response to: AndreasHGW
 
  Click to reply to this thread Reply
AndreasHGW wrote:
Last question: Is there any possibility to save the script for the next use?
You can save the script as a text file named 'Replace keywords.groovy' and place the file in the 'tools' subfolder of jAlbum's configuration directory - SHIFT + CMD/CNTRL + C to open it.

You will have to keep editing the file every time you want to change the search/replace keyword.

Or, you can save the attached file in the tools folder and use that. You will also need to add the other attached file (groovy-swing-3.0.5.jar) to an 'ext' directory inside the configuration directory.
This version lets you enter the search and replace keyword when it is run. Note, Groovy GUI scripts are a bit slow, so allow a couple of seconds for the GUI to appear.
AndreasHGW

Posts: 50
Registered: 21-Sep-2014
Re: Replace a keyword by another one into an entire album
Posted: 13 Jun 21, 22:24   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
OK, thanks. I copied the script into the tools directory and the groovy-swing into the ext directory. But I do not understand how to run it. I tried to start the groovy-swing at my windows 10 PC, but nothing happening (waiting several seconds). And also SHIFT + CTRL + C --> nothing happening.
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Replace a keyword by another one into an entire album
Posted: 13 Jun 21, 22:40   in response to: AndreasHGW in response to: AndreasHGW
 
  Click to reply to this thread Reply
AndreasHGW wrote:
OK, thanks. I copied the script into the tools directory and the groovy-swing into the ext directory. But I do not understand how to run it. I tried to start the groovy-swing at my windows 10 PC, but nothing happening (waiting several seconds). And also SHIFT + CTRL + C --> nothing happening.
From the jAlbum menu select 'Tools > External tools > Replace Keywords'
AndreasHGW

Posts: 50
Registered: 21-Sep-2014
Re: Replace a keyword by another one into an entire album
Posted: 14 Jun 21, 21:04   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Thanks, now it works. A little hint from me:

I have taken a look at the script. It'ts better to configure the preferredSize for both buttons 100,24 and not 80,24. Otherwise you can not read the text within the box.

Andreas
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Replace a keyword by another one into an entire album
Posted: 14 Jun 21, 21:16   in response to: AndreasHGW in response to: AndreasHGW
 
  Click to reply to this thread Reply
AndreasHGW wrote:
Thanks, now it works. A little hint from me:

I have taken a look at the script. It'ts better to configure the preferredSize for both buttons 100,24 and not 80,24. Otherwise you can not read the text within the box.

Andreas

Thanks, I have also added some bottom padding, to lift the buttons up a touch. Lines 49 and 60 become
insets: [5,5,5,5],
.

I'll soon post this script as an external tool in that forum.
Legend
Forum admins
Helpful Answer
Correct Answer

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