Applying a TextSoap filter and moving files silently

I had to dust off a macro for a new project. After changing the file locations to match the new way macOS references Google Drive folder locations, I am able to move the file correctly, but realize that the TextSoap filter is no longer being applied. I believe I am missing something obvious, like not passing the actual file to TextSoap. The script is also a Frankenstein's monster because I have bits (like opening the file via AppleScript) that I do not actually want to perform.

Here's what I am trying to accomplish:

  1. When I place one or more text files (in this case, a .vtt captions file downloaded from Vimeo) into Google Drive folder 1. imports,
  2. For Each file…
    2.1 place a copy of the unmodified file into folder 3. backup (I realize I do not have this in my script attempt below.)
    2.2 apply the TextSoap filter "Transcript Cleanup" to the whole file. My attempt opens the file successfully, but I don't need that step.
    2.3 move the cleaned/filtered/modified file to folder 2. exports

One issue that might be the culprit for 2.2 above is that even when I have the file open in TextEdit and TextSoapAgent running, I cannot apply the filter without selecting all the text contents. This might be implemented by the developer to prevent user error.

If I select all the text and apply my filter, I achieve the desired text output as a replacement, so I know that my TextSoap filter works. I am just trying to have this all done in the background.

Thanks in advance for any help!

It's unclear to me how the textsoapAgent is figuring out what file to clean…?

I see that now, thank you @peternlewis . I am asking for additional parameters on the TextSoap forum to specify an incoming file.

Is there an easy way to pass a file in Keyboard Maestro within a "For each item" clause, rather than just a file location or attribute?

You can get a file from a finder selection, or finding it within a folder, or by specifying it's path directly, any of which can then be pasted to the script.

How to do this depends on how you are selecting the file.

Thank you for the help, Peter. I heard back from the developer of TextSoap who clarified how to pass a file to his cleanFile command in AppleScript. After some testing, I was able to combine it with a script that I had pulled from an example in the forum here. The AppleScript works!

set kmInst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
	set filePath to getvariable "Local_FilePath" instance kmInst
end tell

--- filePath is the POSIX path to the file/folder ---
--  You may need to convert to Finder alias to use with Finder

(*
set fileAlias to POSIX file filePath as alias

tell application "Finder" to open fileAlias
*)

tell application "textsoapAgent"
	set myfile to filePath
	cleanFile myfile with "Transcript Cleanup"
end tell

Here is my current macro. I now only need help moving/copying files. Desired behavior:

  1. File is uploaded to a Google Drive folder path ending in 1. import which is monitored by Keyboard Maestro through Google Drive Desktop. [This trigger works!]
  2. Untouched file is copied to a second Google Drive folder path ending in 3. backup.
  3. File is moved to a third Google Drive folder path ending in 2. backup and the AppleScript is run on the file.

The following macro accomplishes steps 1 and 3. I have attempted adding step 2 but get file moving errors and removed that step to limit complexity.

I can accomplish steps 1 and 3 with the Debugging on. However, without debugging I get a duplicate file notice and the AppleScript doesn't work. Here is the log with the errors. My test files were called abc.vtt and def.vtt

2023-07-08 10:06:11 Execute macro “Clean and format transcripts from Vimeo” from trigger Anything is added to folder “~/Library/CloudStorage/GoogleDrive[…masked info…]/1. imports” (ignoring partial files)
2023-07-08 10:06:12 Execute macro “Clean and format transcripts from Vimeo” from trigger Anything is added to folder “~/Library/CloudStorage/GoogleDrive[…masked info…]/1. imports” (ignoring partial files)
2023-07-08 10:06:12 Action 9468985 failed: File action failed because destination already exists /Users/[…masked info…]/Library/CloudStorage/GoogleDrive[…masked info…]/2. exports/def.vtt
2023-07-08 10:06:12 File action failed because destination already exists /Users/[…masked info…]/Library/CloudStorage/GoogleDrive[…masked info…]/2. exports/def.vtt in macro “Clean and format transcripts from Vimeo” (while executing Move or Rename File “%Local_FilePath%”).

What is:

doing?

Local_FileName is not defined, and Local_filePath would then be undefined (probably empty, maybe unchanged, I don't really know).

You are getting two consecutive triggers for the file. Are two files being added, or just one?

Either way your macro is not really correct, you are getting the trigger for the file change (which has the path of the file in the TriggerValue token), but you are processing the entire folder.

You should process just the %TriggerValue% file in the macro.

If you are only adding one file, then the issue might be related to the behaviour of textsoap - it may be writing a temporary file for example.

I suggest you do something like:

  • Copy a File %TriggerValue% to /…/…3. backup`
  • Get File Attribute the name of %TriggerValue% to Local_FileName
  • Set variable Local_TempFileName to %RandomUUID token.
  • Move a File %TriggerValue% to /tmp/%Local_TempFileName%
  • Move a File /tmp/%Local_TempFileName% to /…/…2. backup/%Local_TempFileName%

Something like that.

Or if you are putting two files in, then just changing it to only process %TriggerValue% instead of the whole folder will perhaps be all you need to do - but operating on files within the folder, while watching the folder, is asking for trouble.

1 Like