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


Permlink Replies: 21 - Pages: 2 [ 1 2 | Next ] - Last Post: 17 Oct 24, 21:15 Last Post By: davidekholm Threads: [ Previous | Next ]
palthe

Posts: 24
Registered: 27-Dec-2009
Get absolute path to image file in output folder (for use in External Tool)
Posted: 11 Oct 24, 18:34
 
  Click to reply to this thread Reply
I'm creating an External Tool (.bsh) that copies image- and video-files from an Album to an external directory. For this tool I need two paths:

1) The absolute path to the original image file, e.g.
E:\My Pictures\~Testpictures jAlbum\film10526.jpg

2) The absolute path to the (processed) image file in the output folder, e.g.
E:\My Jalbums\Testalbum\album\egypte\slides\film10526.jpg


After much trial and error I devised the following code (ao is the album object):
// Get filename
String itemFileName = ao.getName();	// = filename and extension
 
// Get path to original file
// example: E:\My Pictures\~Testpictures jAlbum\film10526.jpg
String pathToOriginal = ao.getFile().getAbsolutePath();
 
// Get path to processed file (NB: not correct for gif, tif and bmp files!)
// Example: E:\My Jalbums\Testalbum\album\egypte\slides\film10526.jpg
String pathToProcessed = rootOutputDirectory + '\\' + ao.getPathFromRoot().replace('/', '\\');
pathToProcessed = pathToProcessed.replace(itemFileName, 'slides\\' + itemFileName);

The code for the path to the original file seems OK.

The code for the path to the processed file is ugly. It is complicated and it doesn't always gives the correct path: gif-images are not put in the slides-subfolder and bmp- or tif-images are converted to jpg-files.

Surely there must be a better solution?

Note: I'm not a real programmer, but most of the time I can get some working code by copy/paste from this forum or from the internet, and a lot of trial-and-error. So a ready-to-use solution would be much appreciated :-)
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 11 Oct 24, 20:39   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
First, Beanshell is deprecated and you should consider switching to Groovy, only a few differences, see https://jalbum.net/help/en/Groovy_Scripting/Groovy_Sample_scripts

If you are using recursion then you can use 'outputDirectory' and then use a switch/case to get the path to the file. 'engine.getSlideDirectory() + "/" + itemFileName' for those in the slides directory and just 'outputDirectory + itemFileName for those in the output folder.

Why do you want to copy the original and output files to another disc? If it is for backup there are easier ways to do it.
palthe

Posts: 24
Registered: 27-Dec-2009
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 11 Oct 24, 23:02   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
Thanks for the reply.

When I use
outputDirectory + "\\" + engine.getSlideDirectory() + "\\" + itemFileName
I get 'void\slides\film10526.jpg' as result. Did you mean 'rootOutputDirectory'? Then I get E:\My Jalbums\Testalbum\album\slides\film10526.jpg. But then I'm still missing the sub-folder 'egypte' where this photo is located. That is why I used the 'ao.getPathFromRoot()' variable.

I also considered to use switches for the different image types, but I hoped there would be some kind of jAlbum-variable that would handle the exceptions automatically.

To explain the purpose of my tool:

This External Tool copies all photos and videos from the album to an external directory, while each filename is preceded by a sequence number which reflects the order in which the files are ordered in the album (custom ordering).
When I use these numbered files in a slide-show program or on my TV, the photos are displayed in the same order as in my album (my TV displays the photos ordered by filename).

Initially my tool copied the original files, but then I wanted to add an option to use the processed files, which have a smaller file-size (loading faster), but still have enough quality to display on a TV.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 12 Oct 24, 00:48   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
palthe wrote:
Thanks for the reply.

When I use

outputDirectory + "\\" + engine.getSlideDirectory() + "\\" + itemFileName
I get 'void\slides\film10526.jpg' as result. Did you mean 'rootOutputDirectory'? Then I get E:\My Jalbums\Testalbum\album\slides\film10526.jpg. But then I'm still missing the sub-folder 'egypte' where this photo is located. That is why I used the 'ao.getPathFromRoot()' variable.

I also considered to use switches for the different image types, but I hoped there would be some kind of jAlbum-variable that would handle the exceptions automatically.

To explain the purpose of my tool:

This External Tool copies all photos and videos from the album to an external directory, while each filename is preceded by a sequence number which reflects the order in which the files are ordered in the album (custom ordering).
When I use these numbered files in a slide-show program or on my TV, the photos are displayed in the same order as in my album (my TV displays the photos ordered by filename).

Initially my tool copied the original files, but then I wanted to add an option to use the processed files, which have a smaller file-size (loading faster), but still have enough quality to display on a TV.

Without seeing all of your code I can only suggest something like this
//The recursive process to process all images and videos.
def processFolder(AlbumObject folder, String path) {
  //Go through all files and folders within 'folder' (which starts with rootFolder)
  for (AlbumObject ao : folder.getChildren()) {
    //Declare variable for later use
    String oldPath;
    //If the Object is a folder process that folder
    if(ao.isFolder()) {
      //Variable to store entry path for recursive calls
      oldPath = path;
      //Update the path variable to add current folder
      path = path + folder.getName() + "/";
    //process folder ao
      processFolder(ao, path);
      path = oldPath;
    }
    else {
      //Not a folder so a file os some sort
     //Check if the file is an image or video
     if(ao.getCategory() == Category.image || ao.getCategory() == Category.video) {
      //Do your image or video copying, path will be the path to the 'current' output folder
    }
}
 
//To start processing
processFolder(rootFolder, rootOutputFolder.getFile().getPath().toString()); 


Hope that helps. You can always look at the code used for existing external tools, such as the attached or any from the forum.

Edited by: RobM on 12 Oct 2024, 08:43
Changed method process folder call from “” to rootOutputFolder.getFile().getPath().toString()
palthe

Posts: 24
Registered: 27-Dec-2009
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 12 Oct 24, 16:16   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
I tried your suggested code.

After adding two missing closing brackets and replacing rootOutputFolder by rootOutputDirectory, I still get an error message:

groovy.lang.MissingMethodException: No signature of method: java.io.File.getFile() is applicable for argument types: () values: []
Possible solutions: getName(), isFile(), getPath(), listFiles(), getBytes(), getText() in 0 Test.groovy

I give up.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 12 Oct 24, 22:52   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
Try the attached, it works on my Mac. I've added plenty of remarks to help you follow what I've done.

Changes needed:
I've used a 'prefix' variable to increment the prefix counter, starting with 1 and an underscore. You can change that in lines 28 and 33 if needed.

lines 27 & 28 & 33 I've used a fixed location to test, you need to change this to your variable/destination.

lines 53 & 54, you need to replace this if using a gui when running the tool, use your variable.
//Change this to suit your option of originals or slides
originals = true;
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 13 Oct 24, 20:53   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Attachment CopyToTV.groovy (3.4 KB)
A complete tool that asks for the destination directory and if it should copy originals or slides.
palthe

Posts: 24
Registered: 27-Dec-2009
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 13 Oct 24, 22:45   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
I appreciate your help, but my question was not to develop a complete tool. I had made one myself, but that failed for gif, bmp and tif files. I hoped there would be some kind of variable that would always point to the correct location and the correct filename of the slide-file. I'm sorry that I did not say that clear enough in my original post.

I think the tool will be too complex If we try to handle all possible exceptions (there are about 150 supported file-types!) with if-then-else or case statements. I now use a try-catch construction for my file copying and write the filename to the system console when an error occurs. As most of my albums only contain jpg-images that is not a big problem.

For me this post can now be closed.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 13 Oct 24, 23:54   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
I don't think there is an easy scripting method, it is the sort of thing a skin developer would have access to when processing template pages. Then you have access to variables like:
imagePath Path to the image that is shown in slides
originalPath Path to the original file. Only defined if one of the following is true:
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 14 Oct 24, 00:24   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Moving this to skin and tool development, trying to keep questions separate from actual tools posted for general use.
palthe

Posts: 24
Registered: 27-Dec-2009
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 14 Oct 24, 16:53   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Please rename header Skins to Skins and Tools to prevent future confusion. See screenshot.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 14 Oct 24, 17:22   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
palthe wrote:
Please rename header Skins to Skins and Tools to prevent future confusion. See screenshot.
The person (Jeff Tucker) that usually manages the forum settings is currently away. A while ago the forums were tidied up and some sections merged or removed. It might be better to move this forum outside of the ‘skins’ section and rename it to ‘development’. When Jeff is able to he will decide if a change is needed and what he thinks is the best way of doing it. I don’t have access to those settings.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 14 Oct 24, 21:03   in response to: palthe in response to: palthe
 
  Click to reply to this thread Reply
palthe wrote:
I think the tool will be too complex If we try to handle all possible exceptions (there are about 150 supported file-types!) with if-then-else or case statements. I now use a try-catch construction for my file copying and write the filename to the system console when an error occurs. As most of my albums only contain jpg-images that is not a big problem.

For me this post can now be closed.

One last comment, as I think it is not that complex.
GIF are always in the output folder.
You can check if images are forced to jpeg or not using engine.isForceJPEGImages()
If not set PNG stay as PNG, otherwise they are JPEG.
All other image types are converted to JPEG unless they are set to 'Use original'.
You can test for that using ao.isUseOriginal() where ao is the AlbumObject being processed, all originals appear in the output folder.
If video processing is checked, under Settings/Preferences/Advanced/Video support then they are all converted to MP4 and appear in the slides folder. You can check video processing using Config.getConfig().isVideoSupported();

Edited by: RobM on 17 Oct 2024, 20:12
corrected some texts
davidekholm

Posts: 3,486
Registered: 18-Oct-2002
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 15 Oct 24, 12:29   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
You can use the https://jalbum.net/api/se/datadosen/jalbum/AlbumSynchronizer.html to get the corresponding path in the album file structure for any album object. Use the "translate" method.
RobM

Posts: 3,845
Registered: 4-Aug-2006
Re: Get absolute path to image file in output folder (for use in External Tool)
Posted: 15 Oct 24, 13:12   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
You can use the https://jalbum.net/api/se/datadosen/jalbum/AlbumSynchronizer.html to get the corresponding path in the album file structure for any album object. Use the "translate" method.
I’d forgotten about that API call, actual usage was discussed here to delete output files.
Legend
Forum admins
Helpful Answer
Correct Answer

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