I need to translate several sdlxliff files from a tricky folder structure that contains several other subfolders and file types besides the sdlxliff files that I don't need to touch.
The files may have identical names (but unique paths). I want to collect all the sdlxliff files in a single folder (with filenames changed if necessary). After translation, I need to put them back in their exact original location. How can I achieve this?
It's probably not too hard to do what you want, (by saving the folder names) but I think it may be more work than you need to do. May I ask, what do you mean by "translate"? What app are you using to translate?
I can think of ways using the macOS "find" command that might do what you want without moving files around.
I looked it up. It doesn't have a command line version, sadly. So perhaps your solution is indeed the best solution. I think I agree with your plan now.
I presume you want one macro to move them out, one macro to move them back, and no macro to invoke the translator app (I'm guessing you will do this manually, because the app is not a well behaved macOS app.) I assume that the files could be different each time, and that they all have an identical extension within a specific folder.
As you already seem to understand, the problem is that some of the files may have the same base name. That's what makes this tricky. I would replace your condition "with filenames changed if necessary" to "with all filenames changed to unique filenames." In order to accomplish that, I would create a KM variable (or temporary file) with a new filename and the original path, like this:
Once you create the above file, (probably using the find command in macOS) it's a fairly simple loop to process the paired items for the first half of the problem, which is to copy all the files into a single, temporary folder. Then a second macro could reverse the process.
Actually, the first argument (eg, "file1") isn't necessary at all, since the index of the line of the file could specify the filename itself. E.g., line 1 becomes "file1", etc. however for clarity it's probably better to have two arguments.
Have I set your path in the correct direction? Or do you need me to draft a couple of macros based on this design?
That would be very kind. Please note that there has to be a filter to process ".sdlxliff" files only, since there are also files with other extensions in the directory tree that don't need to be touched at all.
So they all need to be in the same folder to merge -- you can't provide a list of paths to different folders (even by driving the GUI "Open" dialog multiple times)?
Do the files that are moved to a single folder get changed and/or does this process generate new files that have to be put somewhere?
set counter to 1
for each file in source folder, recursively
if extension is 'sdlxliff'
append file path and linefeed to pathList
copy file to tempFolder, renaming to '<counter>.sdlxliff'
set counter to counter + 1
end if
end for each
process all the files in tempFolder, however you do that
set counter to 1
for each line in pathList -- remembering that each line is an original file's path
delete file at that path
move file 'tempFolder/<counter>.sdlxliff' to that path
set counter to counter + 1
end for each
Obviously, work on copies until you're happy, and you may want to write out pathList to disk after the first "For Each" in case you need to manually reset things...
You could use @Airy's find or similar to get the file list, but it's easier to prototype using what you know well -- if the "For Each" proves too slow you can replace it later.
The first macro he needs is one that will list all the files in a set of folders, which is as easy to do as this:
find "/Users/myname/Documents/." -type f \( -iname "*.png" \)
Of course, he will have to change his source path, and he will have to replace "*.png" with "*.sdlxliff". And then if he likes the look of the output, he will have to add something like this to the end:
> ~/myfilelist
That will place the list of files in a folder, which is what he needs before the next step.
The next step is to copy all those files into a temporary folder. I think that can be done all in a single step with the find command, but I don't want to get too tricky for a new user here. So it's probably better/clearer to complete the process like this:
In that code, I changed my approach slightly. Instead of sending the output to a file, I sent it to a KM global variable. This is probably clearer.
The above code works for me (as I said, using my own path and extension, he will want to change to his own).
He should test the above code thoroughly using his app on the newly created folder to process the folder before I write the next macro which is going to overwrite his original files. Once he is sure that his app works on the temporary folder above, I will complete the code to put the files back where they belong. (Actually, I need to make some breakfast now.)
I am trying to get this to work, but obviously I don't have the correct syntax:
set counter to 1
set source folder to "/users/hl/desktop/to_translate"
tell application "Finder"
set counter to 1
for each file in source folder, recursively
if extension is 'sdlxliff'
append file path and linefeed to pathList
copy file to tempFolder, renaming to '<counter>.sdlxliff'
set counter to counter + 1
end if
end for each
end tell
Why are you replacing slashes with pipes? Why are you splitting the file path?
Ultimately, how you do this will depend on how you are going to
process all the files in tempFolder, however you do that
If you can include that in this same macro it's quite straight forward. If that's a separate step then you'll need a macro to make the copies and a macro to move them back, plus a file or persistent global variable to keep a list of where the files came from.
What my pseudocode above does is copy each file to a temporary folder and rename it -- the first file copied will be 1.sdlxliff, the second 2.sdlxliff, etc. That guarantees you won't have a name clash, but assumes you don't need the file name metadata as part of your translation.
Because each original path is stored in order in pathList you can later work through that to put the files back -- the path in line 1 of pathList is the original location of 1.sdlxliff, line 2 is that of 2.sdlxliff, and so on.
You should be able to translate each line of pseudocode into a KM action, only having to drop your "process the files" into the middle bit.
Yes, but is that something you can do in this macro or will it have to be one macro to copy the files, you process the files, another macro to move them back?