Need some Automator, Keyboard Maestro, or shell script help


(Adam Engst) #1

With one of my other hats on, I direct local track meets. We’ve been forced to switch to horrible Windows software called HyTek Meet Manager because it should be more reliable than a terrible Adobe AIR package called MeetPro that we were using. Long story short, this app creates some text files (times) that I need to concatenate (so I can match them up with finishers). Here’s the workflow I’m looking to create:

  1. Select 1 or more files (usually between 2 and 20) and either click a Keyboard Maestro palette icon or drop them all on an Automator droplet in the Finder window’s toolbar.
  2. Potentially sort them by date created (they have weird names, like 001-0009.tm2 and 002-0010.tm2, and I don’t yet know if they’ll sort properly by name or not)
  3. Combine them all together into one file using the sorted order (Unix cat, or Automator Combine Text Files after some renaming)
  4. Replace any double line breaks with single line breaks (/r/r with /r)
  5. Change the line endings to CR/LF for Windows.
  6. Prompt for a new name for the combined file, making sure to save it with a .tm2 extension.
  7. Save the file in the same (Windows) directory as the originals. I’m using Parallels, so it’s mounting C: in Windows via SMB and it seems I can work with it in the Finder.

I’ve been able to get pretty close to this with Automator by renaming files to .txt, combining them with Combine Text Files and saving with New Text File, and renaming the output file again. But where I’m bogging down is that BBEdit’s Automator actions let me open the file, get its contents, do the grep search, and set the contents, but I can’t figure out any way in Automator to save and close the document that’s open in BBEdit.

I thought about doing it in Keyboard Maestro and a BBEdit text factory, but KM isn’t as good at working on selected files.

The real solution is probably a shell script invoked from either Automator or KM, but I really suck at shell scripts. It would probably take me an hour of fighting with how to pass variables and the like to get it working. So, if someone out there like @ccstone or @frederico or @peternlewis has a snippet I could try, I’d appreciate it!


(frederico) #2

i’m sure there a better solutions to follow, and you’re right, doing this in shell would be far more efficient. But use the tools you know.

How about simply extracting the content of the document from BBEdit and doing a write to file in AppleScript? Or am I misunderstanding the problem? If writing to file will not work where you’re creating your own new file that is not the BBEdit document, but simply its contents ( use copy to clipboard if you have to) , you could also then use shell (do shell script “echo “ & contents & “> path/to/file/name.tm2” and create a written file that works; the just abandon the BBE doc.

Edit: Please forgive the typos and little bugs there but I think you get the idea. I can’t believe though that he be edit can’t do this job


(frederico) #3

Lol. Speech to text while waiting for your taxi on a noisy corner is very difficult.


(frederico) #4

So, re-reading the problem more closely, why not just use an AppleScript action in Automator to tell BBEdit to save and close the document? It could even be as ugly as get frontmost window, save and close.


(Adam Engst) #5

That could work. My AppleScript skills are weak too, but I’ll take a whack at it. My problem is that I always know the broad strokes of what to do, and then I spend hours trying to figure out how to pass a variable or get the command format correct. It’s the problem with needing to write a script only a couple of times a year.


(Robert Brenstein) #6

I don’t know whether it is a real option for you, but you can do all steps with a single click if you program your actions as a script in LiveCode. For those who don’t know it, it is a modern version of Hypercard and the community edition is free.

Robert


(Adam Engst) #7

I have LiveCode and used it once a few years ago to build a scorecard app for the game show that Tonya and I host at MacTech Conference every year, but it would be a steeper learning curve than fighting through the format of a shell script or AppleScript.


(frederico) #8

If you don’t wanna fool around with it today, wait till later, and I will write a snippet that grabs the contents of an open baby had a document (LOL! BBEdit); or I will write a snippet that can close and save that document which is what I assume you would prefer.


(Simon) #9

I’m quite clueless about Automator, but this sounds like stuff people doing data acquisition post-processing have to figure out all the time. Fortunately, there are a few UNIX tools that should make it very simple. Paste together in a shell script that then gets executed from an Automator “wrapper” (to provide interactive GUI) and you should be good to go.

I’d use Finder to rename the files according to creation date and then you get sort for free. For combining cat is certainly the simple way to go. Then use sed to get rid of double breaks and change to Windows format. I usually use a tiny perl script for that that I’d be happy to share, but I have to go the other way and get rid of pesky Win breaks that get inserted by proprietary instrument drivers (Win only, sigh).


(Paul Schinder) #10

The real solution is probably Perl or Python, but if you don’t know them, that isn’t an option. Everything you discuss (getting file creation dates, sorting by date, changing line endings, removing blank lines) would be pretty trivial in Perl, and it’d even run on both your Mac (macOS comes with perl and python) and Windows if you had to run it there. Perl is a marvelous text processing engine, and I use it a lot for that. Python’s reputation (never learned it) is that it’s equally as good.

But if you can get to the point you want with BBedit, then as others have suggested maybe you can get it to write out the file via Applescript, or maybe something (it’s been years since I’ve written any Applescript) “copy the contents of the front window” and writing it out somehow directly in Applescript would work.


(Adam Engst) #11

Thanks for all the ideas, folks, and I got it working with an AppleScript in Automator once I figured out that I had to tell BBEdit to save or close “text document 1.”

The only problem is that once I did that, I ran a more comprehensive test and discovered that the track meet software I’m using resorts all the times once they’re concatenated, rendering the entire project pointless. Augh!


(frederico) #12
tell application "BBEdit" to set TM2_data to contents of document 1
do shell script "echo " & (quoted form of TM2_data) & " > '~/Desktop/filneame.tm2"

(frederico) #13

Whoops. Missed your post. Do you want help with the project? There has got to be some sort of identifying object for each line, so they can be properly concatenated and sorted, yes? Or are you saying that your properly formatted file is being jumbled by the parent software? Is there not a way to coerce it to not do that?


(Adam Engst) #14

I’m good now, thanks. Down the road I may have to see if there’s automation software for Windows that would let me do what Keyboard Maestro can do, so I could open one of these text files and then simulate the times being typed into the right spots in the UI. But that’s a project for another day.


(Curtis Wilcox) #15

I’m sure there are many macro-making options for Windows but a number of years ago, I saw a presentation about how the libraries at the university where I work use Macro Express to automate quite a few varied tasks. However, when I look it up at alternativeTo, there are many, many “likes” for a free and open source tool called AutoHotkey.


(Adam Engst) #16

Thanks, Curtis. I figure I’ll fight through the process manually and then see if it’s worth automating. It’s always a tradeoff!


(Curtis Wilcox) #17

Obligatory XKCD comic:
"I spend a lot of time on this task. I should write a program automating it!"


([email protected]) #18

Sorry, just back from a week camping, so I might have missed some solutions, but here are the Keyboard Maestro details (if you want the full macro made for you, you could probably just ask on the Keyboard Maestro forum and someone there would probably create it for you, but it’s pretty easy.

This is relatively easily done, Keyboard Maestro’s For Each Folder Contents collection can sort by date created. You could then process them in order, or rename them with an incrementing index as required.

Easily done with the Read a File action, and then just append them all to a variable (pre-set the variable empty).

Easily done with a Search and Replace action.

Easily done with a Search and Replace action or a Filter action. Search and replace “\R” is useful (on 10.11+) as it matches any line ending (\r, \n or \r\n).

Easily done with a Prompt for User Input action or Prompt for File action, followed by a Write to a File action.

It sounds like you can ignore the windows side entirely and just use the shared folder on the Mac side - make sure the creation date is preserved on the Mac side so the sort order works correctly.


(Adam Engst) #19

Thanks, Peter! I didn’t end up needing this at all due to stupidity of the Windows software I’m working with, but I’ll keep this in mind for the next time I need to do some automated file munging.


([email protected]) #20

No problem. In all seriousness though, if you want to automate anything, even if you don’t use Keyboard Maestro, the Keyboard Maestro forum is a fantastic resource - the folks there are incredible scripters as well, whether it is AppleScript, shell script, JXA, or UI scripting, I am frequently blown away by what they can do, and how generous they are with their time.