How to support this blog?

To support this blog, you can hire me as an OmegaT consultant/trainer, or you can send translation and project management jobs my way.

Search the site:

Showing posts with label Automator. Show all posts
Showing posts with label Automator. Show all posts

Automator: Finder → Launch Application

I don’t know why it never occurred to me, but there is an Automator action from Finder that’s called Launch Application and that you can use to:

  • register an Applescript (or any other) application in the Services menu,
  • assign a shortcut to it,
  • launch it without having to call it with Spotlight.

That saves the necessity to separately manage your AppleScript code, plus debugging from Automator is really not fun. . .




And it also gives you the ability to automatically call the application instead of using either Spotlight or the running application navigator (I don’t know what it is called, the thing that you trigger when you hit Command+Tab).


Anyway, now I have my Emacs Capture app from this weekend directly available with a shortcut, the only problem now being to make sure that the shortcut is particular enough that it does not conflict with existing application shortcuts. . .



Haha! That shortcut seems to work anywhere but in text fields, where it calls the spellchecker dialog...


Et voilà!


ps: Shortcuts has something similar, but it looks like it won’t accept applications that are not installed in /Applications, which is a pity.

Transcription software for free?

Fast forward to 2023...

Most of what I wrote below is correct. But unless you have lots of time on your hands (or work on very difficult recordings), I would strongly suggest that you use Whisper, the OpenAI solution to automatic transcription. It requires basic command line skills, but nothing that you can’t learn in a 10-minute video tutorial. Your machine specs will also make a difference: my 2.8 GHz Quad-Core Intel Core i7 2015 MBP with 16gb of RAM requires about 1.5~2x the length of the soundtrack to produce a very good transcription (of Japanese interviews) that I will later have to edit to produce something useable.

The github page is here: openai/whisper, and includes plenty of links that you might want to read.


About 4 years ago I got a job where I needed to transcribe about 40 hours of interviews. I wrote most of this article then and let it to rest until now. The solution I propose is a workable solution for transcribing audio/video and can also can be used as a practical introduction to Applescript. I just tested everything in High Sierra, with the current versions of all the mentioned applications.


This article also demonstrates how a few macOS technologies can be put together to create a very robust and integrated solution in a number of very easy steps. The idea is:

  1. Find a process that you need to automate
  2. Use Applescript to code the automation
  3. Use Automator to create a system-wide service to access the automation
  4. Use System Preferences to assign a shortcut to the service, either available system-wide, or only in a given application
Step 3. and 4. are just one way to access the automation. There are many other ways.

Update (the day after...): a comment on reddit says that the title is not accurate because I do not propose speech recognition. For people not familiar with transcription, plain speech recognition is not a solution because it requires two things: good sound quality and that the software be used to the voice. That is not the case with most transcription situations. But, it is possible to dictate the audio that you are listening to, in which case you'll need the same tools as described here, and you just have to add macOS dictation to the workflow if you want to stick to macOS bundled software.

Homemade transcription software...


Transcribing 40 hours of interviews is a lot of time in front of the machine and even though the only thing you need is a text editor and an audio file player the lack of default integration between the two can make you waste a lot of time on tedious manipulations.

The problem is going from the text editor to the audio player each time you need to pause the stream, step backward, or start the stream again. Then you need to get the Time Code, write it at the beginning of the line and start transcribing again.

There seems to be plenty of professional solutions for this where you can start/pause/rewind the audio player with foot pedals directly from the text editor.

However, if you’re used to your keyboard, doing that directly from the text editor of your choice using shortcuts and without ever leaving the editor would also be a totally satisfying solution.

It is possible to use the media buttons available on your keyboard (except for inserting the Time Code). You can work in the editor and pause/resume and do a few other things directly with the keys that Apple has provided. Although, There are a few problems with that.

The first is that you may have set your keyboard to ignore the Apple supplied keys and directly access the Function keys instead. If you’ve done that, you’ll have to hit the fn key and the corresponding Function key to access the media function you need.

Another problem is that the media keys are not conveniently located on the keyboard. Accessing them often will require that you move your hand from your basic typing position and this will slow you down.

The last and most important problem is that you can’t easily and precisely rewind the file to go back to a moment you did not properly hear…

The media buttons are really made to easily play media, and not to do transcription, so we need a different solution.

One solution would be to have direct access to the appropriate functions of the media player while it runs in the background through shortcut keys while the text editor remains in the foreground.

Here is what I came up with, using the following bundled software/solutions:

  • TextEdit
  • QuickTime Player
  • Applescript / Script Editor
  • Automator
  • System Preferences

TextEdit will be used to type the text. It comes with everything you need to write plain text and you can also use it as a minimalist word processor. You can use anything else as a replacement. The only condition is that the software you use supports macOS Services. Unless you use a very exotic text editor or a virtual environment, that should not be an issue.

QuickTime Player supports a number of audio file formats and its functions can be accessed through Applescript. In the second part of this article I'll use VNC instead because it is more convenient.

Applescript is the Apple solution to link all the applications together. Script Editor is the Apple bundled editor where you can code in Applescript.

Automator is how we will create the “Services” to send commands to QuickTime Player while working in TextEdit.

System Preferences will allow us to assign a shortcut to access the services we just created.

Quicktime Player and Applescript


First of all, we need to play a document in QuickTime Player. Instead of pressing the Play button every time we want to start QuickTime Player, we're going to automate that with AppleScript.

1) Open Script Editor
2) Select File > Open Dictionary, then select QuickTime Player from the list of applications that is displayed

What you see now is the list of QuickTime Player functions that you can access from Applescript. You’ll notice a “QuickTime Player Suite” where player specific functions are documented. There, you can see that the “play” command requires a “document” to run.

3) Select File > New and type the following command:

tell application "QuickTime Player" to play front document

Now, open an audio file with QuickTime Player (do not start playing it yet), go back to your script and click the “Run” button in the Script Editor window (the grey arrow pointing right in the toolbar). You should now see the QuickTime Player start playing your file.

Pretty cool right?

If we just replaced clicking on "Play" in QuickTime Player by clicking on "Run" in Script Editor, we would not have advanced much. We now need to package all that in a way that removes the need to click on a button.

Automator


4) Open Automator and select File > New. Select “Service” and in the search field on the left, above the list of possible actions, enter “Applescript”. You should end up with the command “Run AppleScript”.

5) Drag that command to the right side of the Automator panel. You can now see a “(* Your script goes here *)” where you paste what you just typed in the AppleScript Editor (“tell application "QuickTime Player" to play front document”).

6) Now, there are a few things you need to tweak to make that run properly. Above the command block you just created there is a “Service receives selected [text]” drop menu. Click on [text] and go down the list to select [no input]. Indeed, your code does not need any input to run, so [no input] is the right choice. Leave the “in [any application]” as is since you want to be able to use that service from any application that supports services, not only from TextEdit.

We’re almost done. Once you save the service (give it a name like “QuickTime Play” or anything that makes sense to you), it should appear in the “Services” menu that’s available from the Application menu next to the Apple menu (top left of the screen).

So, the service is available, you can work in your text editor and select it from the menu and it will play the front QuickTime Player document, but just like you want to avoid having to click around, you want to access that service by using a keyboard shortcut so that you don’t have to leave the keyboard while typing.

System Preferences and Services shortcuts


7) At the bottom of the Services menu you’ll find a “Services Preferences” item. Select it and you’ll find yourself in the Keyboard Shortcut section of the System Preferences. Down at the bottom of the list, in the “General” section, you’ll find your newly created service with an “add shortcut” button on the right. Click that button and enter the shortcut you want, but be careful not to use something that’s already used in your text editor. The best way to check that is to open a file in your text editor and to try the shortcut you’re thinking about in various contexts (on selected text, between letters, etc.) If nothing happens, it means you can assign it to the service.

8) Enter TextEdit or the editor you're working with and hit the shortcut. Your audio file should be playing in the background, and you have not left the editor.

After "Play": "Pause" and "Rewind"


We have created a “Play” service. Now we need to have a “Pause” service and a “Rewind” one. The only difference is the AppleScript command that we’ll use.

If the “Play” command was:
tell application "QuickTime Player" to play front document

the “Pause” command will be:
tell application "QuickTime Player" to pause front document

and the “Rewind” command (let’s say 5 seconds backward) will be:
tell application "QuickTime Player" to step backward front document by 20

The “20” is 20 “steps” and after testing a bit it seems that one step is 1/4 of a second, so 5 seconds will be 20 steps.

We seem to be almost done, but there are 2 problems with the above commands.

1) The Rewind command also pauses the file. But if you want to step backward to re-listen a part you did not hear clearly, you want to have the command resume play right after you’ve rewound the file. The solution is, well, to ask QuickTime Player to resume playing after rewinding the file... The new “Rewind” command would look like this:

tell application "QuickTime Player"
step backward front document by 20
play front document
end tell

2) You’ll notice that there is a small time difference between the time you pause and the time you resume the play. Depending of when you stop and resume the playing, the time lag seems to vary between a few hundredth and a few tenths of second. It seems that’s the way QuickTime works and that’s a problem if you need to resume playing when somebody is talking: a 0.2 to 0.3 second gap is enough to miss a sound and misunderstand a word. Now that we know that “step backward” pauses the file, we can use it to pause the file at a satisfying time position, like 2 step before QuickTime Player actually paused (that should be enough). The new “Pause” command would now loo like this:

tell application "QuickTime Player" to step backward front document by 2

We already have a Service and a shortcut to access “Play”, so we just need to follow the same procedure to create the “Pause” and the “Rewind” services.

Now we can start working on our files…

Time codes...


But what about inserting time codes in your document ?

Here is the code from which you’ll be able to create your own service:

tell application "QuickTime Player" to set QTPTIME to (current time of front document)
set MIN to (QTPTIME div 60 as integer)
set SEC to (QTPTIME - (MIN * 60) as integer)

if MIN < 10 then set MIN to 0 & MIN
if SEC < 10 then set SEC to 0 & SEC

set TC to (("TC: " & MIN & ":" & SEC) as text)

set the clipboard to (TC as text)

current time” is the current time of the document being played. The value is given in seconds. So to  convert that into a time code we need some basic arithmetics, which we find in line 2 and 3.

The results are expressed as “normal” numbers and so, for numbers smaller than 10 we’ll need to add a “0” to the number so that the time code has always 2 digits, like “01:01” for “1 minute 1 second”. That is in line 4 and 5.

Then we need to create the time code strings, on line 6. The concatenation command is “&” as we saw in line 4 and 5.

The resulting string that is put into the clipboard would be something like:

TC: 01:01

The last line stores the time code into the OS clipboard so that you can paste it wherever (and whenever) you want in your document. You can use the standard Command+V shortcut to do so.

Now, if you’ve done everything right, you should have something like this in your Services menu:



Et voilà !

An strong alternative to QuickTime Player: VLC


There is an alternative to QuickTime Player that has none of the problems we just described. The software is VLC. It is Free Software and is available directly from the development site. You can make donations to contribute to the development too.

VLC can play a lot of media formats. If you open VLC’s AppleScript dictionary (step 2. above) you’ll see that the “play” command works on the current playlist item and also pauses the stream when it is running. Trying VLC, you can see that when it pauses, it resumes from the same position, so that you don’t have to think of a workaround like we did for the above “Pause” service. Also, the “step backward” command does not stop the stream. If the stream is playing then “step backward” just steps backward and proceeds with playing the file. If the file is paused, then stepping back will keep it paused.

Thanks to VLC’s behaviour we can reduce our 3 above scripts to 2. One would be “play/pause”, the other would be “Step backward”.

The code looks like that:

Play/Pause:
tell application "VLC" to play

Step Backward:
tell application "VLC" to step backward of 2

(the default is 10 seconds, the AppleScript dictionary gives 4 possible values which are, after testing “of 1” for 3 seconds, “of 2” for 10 seconds, “of 3 for 1 minute” and “of 4” for 5 minutes.

VLC makes it simpler to code the solution, but if you want to work only with macOS bundled software, it is also possible to work around QuickTime Player issues, as we've seen above.

There are a lot of areas where the above code can be improved, but the solutions we have work well enough and can be the basis for a lot of other relatively simple developments.


Update (11/9):

I've changed the code a bit after testing on a real job (in VLC).

First, the time code:

tell application "VLC" to set VLCTIME to (current time as integer)
set MIN to (VLCTIME div 60 as integer)
set SEC to (VLCTIME - (MIN * 60) as integer)
if MIN < 10 then set MIN to 0 & MIN
if SEC < 10 then set SEC to 0 & SEC
set TC to (("
" & "TC: " & MIN & ":" & SEC & "
") as text)
set TCstring to (TC as text)

return TCstring

What I changed here is the TC string: I added a line break at the beginning and at the end of the TC string, so that I don't have to insert them myself. That way, I can insert the time code at the end of any line I've just finished and I get

  • a line break → move to the next line
  • a time code → insert the time code
  • another line break → move to the next line

so that I can start to type right away.

I have changed the way the Automator service works too. Instead of feeding the clipboard, and pasting the time code myself, I ask the code to return the string (return TCstring) and I checked the [Output replaces selected text] box at the top of the Automator actions list.



That way the returned value is automatically pasted where I have the cursor.

Another modification, minor this one, in the Step Backward function: I changed the delay from 2 to 1, which is in fact way enough when you just need to clarify a sound. A delay of 2 requires that you wait too long before you can resume typing.

tell application "VLC" to step backward of 1

Now, you must really be careful about the shortcuts so that the don't interfere with normal navigation in the text.

I chose the following:

Control + ]  for "VLC Play+Pause"
Control + [  for "VLC Rewind"
Control + ↓  for "VLC Time Code"

I've just finished transcribing a short 6 minutes interview with this setting and everything worked like a charm.

Evolution...

This is a follow-up to the "Applescript everywhere" article that I published yesterday.

As I mentioned in the update at the end of the article, I had written on this blog, back in November 2007, "I have never found a practical way to use AppleScript in my workflows..."

That same year, 8 months earlier, I was talking about OmegaT in Tokyo. It was in one of the first editions of the "Open Source Conference", a local (Japanese) conference that moves around the country and takes place in Tokyo twice a year: "翻訳者を支えるオープンソースの翻訳メモリソフト". At the time, OmegaT did not have most of the features that make it now a professional CAT. I wanted to promote it in FOSS circles here because I thought they would be the best places to find potential developers. The strategy did not work, but that gave me a chance to meet amazing people from Hokkaido to Okinawa that I am still in touch with today.

2 years later, invited by one such person, I wrote a short presentation of OmegaT where I mentioned Applescript, it was published in issue 45 of the Journal of the "Asia-Pacific Association for Machine Translation". I put a copy here: "自由に翻訳!". At the time I was exploring ways to extend OmegaT with easy to write scripts because developing new features in Java was something I never felt I'd be able to do (I never was, and that's the main reason why I eventually left the project, last July).

The script I quote in the article is:
on run { }
  do shell script
  "sed \"s/<[^>]*>/ /g\" ~/Library/Preferences/OmegaT/script/source.txt | /usr/local/bin/pbcopy"
end run
Nothing very Applescripty there, I'm only calling a one line shell script that removes OmegaT tags from the source segment and pasting the result into the pasteboard. This one must have been using Automator, released 4 years earlier. It was a 6 layered "machine": Key shortcut → Automator service → Applescript handler → shell script → sed process → Cmd-P.

At the time, OmegaT had no "API" or any sort of scriptability to speak of. But it supported the OSX services because the Java UI framework it used (and still uses) allowed for that (unlike other Java applications that use other frameworks less "connected", if connected at all, to the OS). Marc Prior, then project manager, invested some money to have a "source, target and selection text export" feature. The feature still exists. There are 2 files that are automatically created in the OmegaT preference folder (source.txt and target.txt) and with a shortcut (Shift+Cmd+C) you can export the contents of the selection to selection.txt in the same location.

And OmegaT did not have any sort of tag management either. What I did as a workaround (obsolete now, thanks to new developments) was create scripts that would parse the source.txt when entering a new segment that I'd use as Automator services called by a shortcut. The script would basically call a "do shell script" as above to extract the tags from the source.txt file (opposite to the above) and output them so that I could paste them in target. The first version was relatively trivial (get all the tags), then I had a more complex one where the service would call a dialog where I'd enter the tag number and the service would paste that around the selection, inside the OmegaT target segment.

It was all relatively smooth and fun to develop, except that Automator was quite slow if I remember well. Now that I think about it, I'm pretty sure that it the starting point of my Applescript coding. Considering the fact that the article was published in March 2009 I must have started developing  that around the end of 2008.

From what I can see in my job archives, I was still using an ad-hoc folder hierarchy for my jobs in November 2008 while in January 2009 I was using a more structured version that I had scripted. The script was something relatively simple that took a start date, and end date, combined the 2 with the client name code and an "→" in between and created a folder with that as a name, and then a sub-hierarchy of folders where I'd put the client mail in a folder, the attachments to translate in another one, references if any, and 2 extra folders for the delivered files and the invoice. Some computer archeology shows that I was probably still saving the mails manually as RTF...

A few checks in my emails show that indeed, in 2006 I was writing to a friend "I have no idea what Applescript is" but in December 2008 I was sending a mail to the Applescript official list about word tokenization (https://lists.apple.com/archives/applescript-users/2008/Dec/msg00025.html) So, I guess all this shows conclusively that my first attempts at introducing Applescript and automation in general in my workflow date back from sometimes in late 2008.

I was writing yesterday about how I copy-pasted code from the web, but my main source of information, programmatic help and mental support came from the above mentioned and still existing Applescript User List hosted by Apple (https://lists.apple.com/mailman/listinfo/applescript-users). There is extremely little that I could have accomplished without list members' help. Which may confirm that Applescripting is not that trivial, or maybe that I'm just a little bit too thick, or maybe that I am not committed enough. In any case, (almost) 10 years later, I say as I did yesterday, that without the level of automation we have on the Mac, my professional life would not be what it is now.

Copy files to...

I may have missed something but I have yet to find an easy way to copy a set of files to an arbitrary place on my disk.

I think Windows people have this. They right-click on a file and they have a "send to" item so that they can copy the file(s) to any place they want.

I've been playing with Automator recently so I just spent an hour or so to create a "Copy Files To..." Automator application that I also saved as a Finder plug-in.

I've put the application into the File section of the Google group "attached" to this blog. I think you can open it in Automator (I created it on OSX 10.5.5) and use "File > Save As Plugin" to install it as a Finder contextual menu (in which case it will be saved under ~/Library/Workflows/Applications).

The application works like this:

1) You select items in the Finder
2) You call the application, either with Spotlight or with the context menu (bottom item: "More" > "Automator" > "Copy Files To...")
3) You select/create the destination (a folder)
4) The selected files are copied - and are not allowed to overwrite existing files
5) The destination folder opens


It looks like Automator applications are relatively slow to launch so for a small application like that I am not sure this is the best technology...

I am reading a tutorial book to Applescript (Applescript Studioでゼンマイびゅんびゅん!!) and I'm sure I'll be able to write an equivalent thing directly in Applescript in a few days, if work allows for that...

Update

Thank you for the replies on the Google Group and comments here where OnMyCommand, but also Applescript solutions and also, simply, Cmd+C/Cmd+V inside Finder were suggested.

Automator applications, by the way they seem to be implemented, seem to be most useful when they are used a number of times in a row. WHen they are used once in a while, it seems that the time they load into memory (?) kinds of defeat the purpose of creating a simple action.

I still have a hard time figuring out what Automator can be best used for, since obviously, most file management tricks will be executed much faster from the Terminal...

Automation on the Mac ! - Python

An Automator action "Run Python script" is provided by toxicsoftware.com.

That comes in addition to the already existing "Run AplleScript" and "Run Shell Script" provided by Apple.

This Automator action is Free Software, released under the Modified BSD License.

Automation on the Mac !

AppleScript



Apple just updated the AppleScript section of its site.

I have never found a practical way to use AppleScript in my workflows... Hopefully the new version and better integration between applications and OS will make that a no-brainer now...

Automator



I have more hopes for Automator, especially the new version that has full access to the user interface (see here).

Here is the state of the documentation about Automator on Apple's developer's site. It is not updated yet for Leopard but is a very good start (after the Help files themselves) for people who want to take a serious look at this technology.

It is also possible to create actions with AppleScript or other easy to learn languages.

What I like in OSX 10.5

Apple advertises over 300 new features in OSX 10.5. A lot of them have no immediate use for the translator but some of them really rock. 300+ New Features

Here is my current and most immediate pick. The list will grow as I use them in real life.

Spotlight



I never really used Spotlight, even on my relatively fast MacBook. Instead I had Butler as an application launcher and the standard Finder for file searches (even though the Finder used Spotlight technology).

Now, Spotlight is all this and more.


  • it displays applications first, so that you can really use it as an application launcher. Even though it is not as sophisticated as Butler, it does the job very properly.
  • searches are faster than on Tiger.
  • it calculates as you type a formula. I used PlainCalc for that before, and even though PlainCalc is much more powerful than Spotlight, the later is currently just what I need most of the time.
  • it acts as a (English) dictionary by showing you the first line of the definition of the word you type, clicking on the result will bring you to the Dictionary application.


Spotlight on Apple's "New features" page.

Dictionary



Dictionary now has a set of 3 Japanese dictionaries (Shogakukan) and also accesses Wikipedia in the main available languages. It is faster than launching Safari to find a reference online. The only drawback, when compared to Safari, is that Dictionary does not display the language versions links of the article you are reading which does not allow the user to jump to the target language in which the reference is sought.

Dictionary on Apple's "New features" page.

Automator



Automator now has a "Record" function that allows it to remember mouse activity on any application's menus. That allows for quick "macro" recording in applications that are not well integrated with OSX and/or that do not support AppleScript. Java applications for translators come to mind !

Automator on Apple's New features" page.

Popular, if not outdated, posts...

.docx .NET .pptx .sdf .xlsx AASync accented letters Accessibility Accessibility Inspector Alan Kay alignment Apple AppleScript ApplescriptObjC AppleTrans applications Aquamacs Arabic archive Automator backup bash BBEdit Better Call Saul bug Butler C Calculator Calendar Chinese Cocoa Command line CSV CSVConverter database defaults Devon Dictionary DITA DocBook Dock Doxygen EDICT Emacs emacs lisp ergonomics Excel external disk file formats file system File2XLIFF4j Finder Fink Font français Free software FSF Fun Get A Mac git GNU GPL Guido Van Rossum Heartsome Homebrew HTML IceCat Illustrator InDesign input system ITS iWork Japanese Java Java Properties Viewer Java Web Start json keybindings keyboard Keynote killall launchd LISA lisp locale4j localisation MacPorts Mail markdown MARTIF to TBX Converter Maxprograms Mono MS Office NeoOffice Numbers OASIS Ocelot ODF Okapi OLPC OLT OmegaT OnMyCommand oo2po OOXML Open Solaris OpenDocument OpenOffice.org OpenWordFast org-mode OSX Pages PDF PDFPen PlainCalc PO Preview programming python QA Quick Look QuickSilver QuickTime Player Rainbow RAM reggy regular expressions review rsync RTFCleaner Safari Santa Claus scanner Script Debugger Script Editor scripting scripting additions sdf2txt security Services shell shortcuts Skim sleep Smultron Snow Leopard Spaces Spanish spellchecking Spotlight SRX standards StarOffice Stingray Study SubEthaEdit Swordfish System Events System Preferences Tags TBX TBXMaker Terminal text editing TextEdit TextMate TextWrangler The Tool Kit Time Capsule Time Machine tmutil TMX TMX Editor TMXValidator transifex Translate Toolkit translation Transmug troubleshooting TS TTX TXML UI Browser UI scripting Unix VBA vi Virtaal VirtualBox VLC W3C web WebKit WHATWG Windows Wine Word WordFast wordpress writing Xcode XLIFF xml XO xslt YAML ZFS Zip Zotero