Extract source document from selected SDLXLIFF on the Desktop

EDIT: A working macro can be downloaded from the last post in this thread.

I am creating a macro to run a Python script (to extract a docx from an xliff) and it works fine, except for the fact that the output (the docx file) is placed in /users/hl.

How can I have it placed at ~/Desktop?

a.docx.sdlxliff.zip (125.2 KB)
decodebs.py.zip (1.2 KB)
Extract source document from selected SDLXLIFF on the Desktop.kmmacros (4.3 KB)


Is the file always created at ~/a.docx? If so, just use the Move a File action like:

That will not overwrite the file, so it will fail if you ran the macro twice.

If you want to always overwrite the file, then use the Trash a File action first to trash ~/Desktop/a.docx before moving it (turn off any error or abort options in the Trash a File action in the gear menu to ignore the failure to trash the file if it does not exist.

Yes it is. But the file name varies. a.docx was just a test example I used. I already had thought about detecting the newest file in /users/hl and move that to the desktop. I've contacted the developer of the Python script. Perhaps he can add the output directory as an extra parameter :).

This will get the Path of the newest file in the directory.

1 Like

Thank you. Can I run (launch) the Terminal hidden?

Stitching everything together:
Extract source document from selected SDLXLIFF on the Desktop.kmmacros (6.0 KB)
Image-000385

I had a bit of time this morning, and this looked like an intriguing project, so I took a look at the macro: It seemed like there was a lot going on that you could skip, with some changes to the code.

Caution: I used an AI to help modify the Python script to handle things differently. Use at your own risk, but it seems to work for me in testing with the one .sdlxiff file I could find on the entire web for testing!

Extract source document from selected SDLXLIFF - modified.kmmacros (5.7 KB)

This modified version is much simpler. It runs on whatever file (or files, it should work on more than one at a time, but I can't test that) you have selected, regardless of where they're located. It writes the extracted file back to that same location.

It doesn't require any temporary files or folders, and it doesn't require running Terminal as an app. Instead, the Python script is embedded in Keyboard Maestro and called directly. The script is modified to use the path and file as passed in Keyboard Maestro variables.


NOTE: You may have to change the first line of the script:

/Library/Frameworks/Python.framework/Versions/3.12/bin/python3 -c $'

Do which python3 in Terminal, and that's the path you'll want to use. Make sure you leave the ending single quote in place, though!


With these changes, the entire macro is two actions in a Finder Selection loop, and that's it. It seems to work in my testing, but I'd be interested to know if it works for you, given you probably have more than one file you could test it with :).

PLEASE test on a copy of your files, just in case it does some Really Bad Things.

-rob.

1 Like

I get these errors:

2025-02-12 22:29:59 Execute macro “Extract source document from selected SDLXLIFF - modified” from trigger The Status Menu is selected
2025-02-12 22:30:00 Action 16773026 failed: Execute a Shell Script failed with script error: text-script: line 2: import: command not found
text-script: line 3: import: command not found
text-script: line 4: import: command not found
text-script: line 5: import: command not found
text-script: line 6: from: command not found
text-script: line 7: syntax error near unexpected token `('
text-script: line 7: `def convert(doc_text, file_out):'
2025-02-12 22:30:00 Execute a Shell Script failed with script error: text-script: line 2: import: command not found
text-script: line 3: import: command not found
text-script: line 4: import: command not found
text-script: line 5: import: command not found
text-script: line 6: from: command not found
text-script: line 7: syntax error near unexpected token `('
text-script: line 7: `def convert(doc_text, file_out):' in macro “Extract source document from selected SDLXLIFF - modified” (while executing Execute Shell Script).
2025-02-12 22:30:09 Execute macro “Extract source document from selected SDLXLIFF - modified” from trigger The Status Menu is selected
2025-02-12 22:30:09 Action 16773026 failed: Execute a Shell Script failed with script error: text-script: line 2: import: command not found
text-script: line 3: import: command not found
text-script: line 4: import: command not found
text-script: line 5: import: command not found
text-script: line 6: from: command not found
text-script: line 7: syntax error near unexpected token `('
text-script: line 7: `def convert(doc_text, file_out):'
2025-02-12 22:30:09 Execute a Shell Script failed with script error: text-script: line 2: import: command not found
text-script: line 3: import: command not found
text-script: line 4: import: command not found
text-script: line 5: import: command not found
text-script: line 6: from: command not found
text-script: line 7: syntax error near unexpected token `('
text-script: line 7: `def convert(doc_text, file_out):' in macro “Extract source document from selected SDLXLIFF - modified” (while executing Execute Shell Script).

Since my version is working perfectly, you don't have to invest any more time in an alternative solution, as far as I'm concerned. Thank you for your effort, Rob!

Those errors all point to the path to Python being different. If you want to try to debug it, what's the output of which python3 in Terminal.

It not, no biggie.

-rob.