Blog

How to generate the date in different languages with TextExpander

Text expansion is a great way to save time, especially with repetitive tasks like typing the date, an email address, etc.

If you don’t know what text expansion is, you can find a quick explanation at the beginning of this article:

How to generate a random number with TextExpander

I work in both (American) English and French. The problem is that sometime, some snippets should give language specific outputs. For instance, date is not written in the same fashion in French and in English. For simple dates, TextExpander can output the day and the month in different order: 16/06/2018 in French, 06/16/2018 in English. But when it comes to output the month in plain text, it’s another story.

Until now, I had a pretty unfriendly snippet: my Mac is set to American English, so when I wanted to output the date with TextExpander, the month was always outputted in English. To overcome this, I created a snippet for French dates, where I had to manually choose the month in a predefined list. Not really user friendly, even if it did the job.

While researching how to generate random numbers in TextExpander, I found this page about dates in JavaScript. And I had an idea.

After a few minutes of testing, I created this JavaScript snippet (abbreviation is dus):

1
2
3
var event = new Date();
var options = { year: 'numeric', month: 'long', day: 'numeric' };
event.toLocaleDateString('en-US', options);

And I created another one (abbreviation is dfr):

1
2
3
var event = new Date();
var options = { year: 'numeric', month: 'long', day: 'numeric' };
event.toLocaleDateString('fr-FR', options);

Can you spot the difference?

I put those two snippets in a folder “Dates”, with d as a prefix 1Folder prefixes apply to all the snippets in a folder. For instance, the “real” abbreviation of “dus” is “us”. But as “dus” is in the folder “Dates” sets with the prefix “d”, “d” is automatically added at the beginning of “us”. Which leads to “dus”..

dus outputs June 16, 2018, while dfr outputs 16 juin 2018. Now, months are properly formatted: the langage, items order and capitals are all good. And I don’t need to manually choose the month anymore.

The really neat thing is that I only had to change the langage tag to completely alter the formatting. The difference between the two snippets is just a few letters in line 3: en-US was replaced by fr-FR.

In fact, en-US can be replaced by any BCP 47 language tag. I used fr-FR for the French date, but it is possible to set virtually any langage you want. You can even output in non-Latin alphabets!

If you want to further tweak the JavaScript snippet, you can find additional details here. For instance, you can output the day name in plain text, the date without 20, and so on. This small JavaScript utility is actually very flexible.

In the past, I used a shell snippet to expand language-specific dates. It worked on macOS, but shell snippets don’t work on Windows and on iOS. JavaScript snippets do, which is a really nice addition. That being said, it should be clearly understood that this article is NOT an endorsement of TextExpander. It is still an (insanely) overpriced utility with not-so-reliable sync.

How to generate a random number with TextExpander

While horribly priced (especially for new users), TextExpander is currently a key part of my daily work. In a nutshell, TextExpander is a text replacement app: you can define shortcuts, and when you type one of the shortcuts it will get replaced by a chunk of text you previously defined. It is a convenient way to save time.

For instance, I have a shortcut oscc that turns in Olivier Simard-Casanova: when I type oscc, it is automatically replaced by Olivier Simard-Casanova. This kind of text replacement is called text expansion.

TextExpander allows for some kind of automation, for instance you can automatically display the current date or time. But I needed to generate random numbers, something TextExpander does not offer – and their support pages are dreadful.

TextExpander can accept JavaScript code as snippet. I just needed to specify the snippet’s content is JavaScript, and the following code did the job:

1
Math.floor((Math.random() * 1000000000) + 1);

Now, every time I type rnumb, it is replaced by a random number between 1 and 1000000000.

This article is not a TextExpander endorsement

If you don’t use text expansion yet and want to give it a try, I strongly discourage you to go with TextExpander. 40$/year for a small utility like TextExpander is insane, especially knowing that their so-called “sync” engine is far from perfect – a.k.a. buggy. And don’t by the standalone apps, they basically don’t updated them anymore, only bug fixes and removal of key features like Dropbox sync…

The only advantage of TextExpander is to exist on Mac, iOS and Windows – and as my work machine is currently a PC… You see why I stick with them. I also have a lifetime 50% discount on the yearly pricing – but even at 20$/year, I still find Expander overly priced, especially considering how rare new features are added since they switched to subscriptions.

You should also be aware that they run their own sync engine. I don’t trust small companies that build a “cloud” as a side feature, especially for security reasons2This is why I am so reluctant to go with the 1Password subscription.. For instance Day One, that made some really bad design choices that lead to data breaches or frequent outages (compare with Dropbox). TextExpander took almost a year to encrypt the synced data stored on their servers, and to be honest, I don’t wonder if a data breach will occur on TextExpander sync, but when.

If you only have a Mac, I recommend to go with aText (one-time 5$ fee$!) or TypeIt4Me (one time 20€ fee, an iOS app is available too). I don’t know for Windows.

Science doesn’t have to be ugly

It’s a common assumption among scientists and researchers that the design of their scientific outputs doesn’t really matter. What matters most is what you have to say, not how you say it.

While reasonable and partially correct, this assumption is also quite naive.

Continue reading “Science doesn’t have to be ugly”

The blog is coming soon

Hi, and thanks for passing by!

For now, this blog has no posts, but some are about to get written and published. I’ll mostly write about economics and more specifically about agent-based models in economics.

I strongly believe this methodology can revive economic theory, and I have no doubts it worths to discuss it here.

If you’re interested in those future posts, feel free to subscribe to the newsletter right below. It’s completely free (and of course, I won’t sell your personal data to anyone).

You can also follow me on Twitter @simardcasanova.