YouTube embedding macro

One of the things I dislike about a lot of Mac workflows written by others is that they rely on the user putting some critical information on the clipboard before the workflow is invoked. Something I dislike more is when I do the same thing, even though I know better.

I sometimes include YouTube videos here, and I like to have the embedded video centered. YouTube makes the embedding code easy to get at: click the Share button (the one with the swoopy arrow next to it), then the Embed button, and the embedding code will appear, already selected for you to copy and paste into the source code of your web site.

YouTube embedding code

For the example video, the embedding code is

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/npjOSLCR2hE?showinfo=0" frameborder="0" allowfullscreen></iframe>

(The width and height may differ from one person to another, depending on preferences you set, as will other settings like “privacy-enhanced mode.” These settings can be changed by clicking the “Show More” button below the video and will persist if you’re signed in.)

Since I like to center the videos, I created a center CSS class in my style sheet and add that to the embedding code. So the code I’d embed here is

<iframe class="center" width="560" height="315" src="https://www.youtube-nocookie.com/embed/npjOSLCR2hE?showinfo=0" frameborder="0" allowfullscreen></iframe>

After doing this by hand several times, I decided to automate the process. But my first pass at the automation wasn’t very smart. It assumed I had copied the embedding code from YouTube onto the clipboard, and it simply added the class="center" part through a search-and-replace operation.

This was foolish, exactly the sort of thing that bothers me when I see it in other people’s workflows. There’s no reason to do the clicking and copying on the YouTube page. Everything in the embedding code is boilerplate except the ID string of the video itself (npjOSLCR2hE in the example), and that can be extracted from the URL of the page through AppleScript—no need for me to do any clicking or copying.

Here’s the Keyboard Maestro macro that does the work:

YouTube embedding macro

You can download and adjust it to fit your needs.

The first step is an AppleScript one-liner that gets the URL of the frontmost Safari document. If you use Chrome, you’ll have to change the AppleScript to something like

tell application "Google Chrome" to get the URL of active tab of front window

The URL is put into the variable YTID, and the second step does a regular expression search-and-replace to leave YTID with just the video’s ID string. In a YouTube URL, the ID string is the value of the parameter v. The macro’s second step uses a find regex of

.+[?&]v=([^&]+).*

to capture the v parameter. The URL is then replaced with just the captured ID string.

After we have the ID string, it’s a simple matter to spit out the boilerplate code with the ID string in its proper place. Because this is relatively long string, I use “Insert text by pasting” instead of “Insert text by typing.”1 This leaves the embedding code on the clipboard, which is messy, so the final step deletes it, putting the clipboard back to what it was before the macro was invoked.

Using this macro does require some preparation—the video you want to embed must be the frontmost tab of the frontmost Safari window. But this is usually the case. Even when it isn’t, a single ⌘-click on the video’s tab will put Safari in the proper state without leaving your text editor. Either way, it’s less work than clicking around in Safari and using copy/paste.

Does the macro work? Let’s see.

 


  1. I generally prefer Keyboard Maestro to insert text via simulated typing even though it’s slower, because some text fields don’t allow pasting. In this case, pasting won’t be a problem because I’ll be using the macro from within a text editor, where pasting is always allowed. 


Path expansion in LaTeX

On Linux, your home directory is typically /home/username; on OS X, it’s /Users/username. If you work on different operating systems, or if you have different usernames on different computers, and you want certain things to work the same way across all your machines, these differences can lead to small annoyances. Recently, I learned a way to eliminate one of those annoyances when working in LaTeX.

On both of my computers, I have a PDF of my signature stored in a file called, cleverly enough, signature.pdf. It’s in the same subdirectory of my home directory on both machines, but because I use different usernames on these machines,1 it’s /Users/name/graphics/signature.pdf on my office computer and /Users/drdrang/graphics/signature.pdf on my notebook.

In shell scripts that access the signature file, this difference poses no problems. The file can be addressed as ~/graphics/signature.pdf on both computers, because the tilde expands to the home directory. Similarly, in Python scripts, it’s os.env['HOME']/graphics/signature.pdf on both computers.

The problem is in LaTeX files. I have a signature macro defined this way on my MacBook Air:

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{/Users/drdrang/graphics/signature.pdf}\\
  \vspace{-.125in}My Real Name}

Because it uses an absolute path, it has to be defined slightly differently on my iMac at the office. It would be nice if I could define it the same way on both machines, but

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{~/graphics/signature.pdf}\\
  \vspace{-.125in}My Real Name}

doesn’t work because the tilde has special meaning (nonbreaking space) in LaTeX. You might think escaping the tilde with a backslash (\~) would fix the problem, but it doesn’t.

The solution, which I found in an answer to this Stack Exchange question, is to use the primitive \string command to get the underlying TeX engine to treat the tilde literally rather than as a special character. My signature command is now

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{\string~/graphics/signature.pdf}\\
  \vspace{-.125in}My Real Name}

Kpathsea, the system TeX and LaTeX use for searching paths, understands the tilde, so now I have just one signature definition on both computers.

Similarly, the LaTeX code for other graphic elements that I commonly use in reports—the company logo, for example—has been unified across machines, and I no longer have to keep two slightly different versions in sync with one another.


  1. Why have different usernames? Mainly because I do all my blogging from my MacBook Air, and it’s generally easier to maintain my pseudonymity if my username is drdrang on that machine. On my iMac at the office, it’s easier—and less weird looking—to have my real name as my username. Of course, I often do real work from my MacBook Air, and that’s where the annoyance described in this post arises. 


LeBron James and conditional probability

If you’ve been watching the NBA playoffs, you’ve seen this commercial. It, and another Twitter-centric one, show up several times in every game.

Maybe sponsors have decided that having more than two or three commercials in rotation at any given time dilutes their branding. Or maybe ad budgets are tighter than they used to be. But let’s not talk about trends in marketing, let’s talk about conditional probability.

As he walks out of his curiously appointed office, LeBron says

The probability of Event B occurring, given that Event A never occurs is obviously zero.

and the camera shifts focus to the nerd at the blackboard.

LeBron and conditional probability

Let’s ignore LeBron’s mistake—he was denied the opportunity to take a probability course in college. But what the hell is the nerd doing? In conventional notation, what LeBron said was

[P\:(B \;|\, \bar A) = 0]

but what the nerd wrote is

[P\:(A \;|\, B) = 0]

If the makers of the commercial have enough familiarity with conditional probability to get the vertical bar notation correct, how did they put the condition on the wrong side of the bar? And how did they miss using an overbar to represent “not A”? Did they try out a version of the commercial with the right equation and find that it was less funny? That’s hard to believe.

By the way, even if you ignore what LeBron says, the contention on the blackboard,

[\textrm{If } P\:(B) = 0, \textrm{then } P\:(A \;|\, B) = 0]

is wrong. Recall that

[P\:(A \;|\, B) = \frac{P\:(A \cap B)}{P\:(B)}]

So if [P\:(B) = 0], then [P\:(A \;|\, B)] has a division by zero, which makes it undefined, not zero.

Michael Jordan would’ve caught that error.


Associate

If I’m at my Mac and want to create an Amazon Associates link to whatever product I’m currently looking at in Safari, I run this little Keyboard Maestro macro:

Amazon Associates Keyboard Maestro macro

It gets the URL of the frontmost tab in Safari, extracts the Amazon product code (the ASIN), and constructs a new URL to the product with my Amazon Associates ID attached. If you click the link and buy the product, you pay the same amount you otherwise would, but I get a small sales commission.

This is a very easy way to make Amazon Associates links, and I’ve been using a system similar to this for years (it used to be a TextExpander snippet). I would never buy a special-purpose Mac app just for making these links.

But iOS is different. Yes, I’ve used tools like Launch Center Pro, Pythonista, and Drafts to create a handful of very useful (to me, anyway) workflows, but I’m generally not as enthusiastic about creating automated tasks on the iPhone as I am on the Mac.

So I’ve been looking forward to Associate, the new app for quickly making Amazon Associates links on iOS. It’s from Squibner, maker of the analogous app, Blink, for making affiliate links to items in Apple’s various iStores.

What I like most about Associate is how smoothly it works with the Amazon app. The items I link to are almost always things that I’ve bought myself, so they’re easily found by looking through my order history. Now, the Amazon app doesn’t have a Share button in its top toolbar, nor is there a Share item in the hamburger menu.

Amazon app item

Instead, there’s a big Share button down near the bottom of the item’s page.1

Amazon app share button

Tap it, and up pops the Share sheet, where you can choose the Associate action.

Share actions

This takes you into Associate, where the link is waiting for you.

Associate link

Associate can create links in a few formats, but I like the plain URL, because most of the links I make on my iPhone are for pasting into Twitter. I don’t write blog post on my phone; I write them on my computer, like a gentleman. (Lately, I haven’t been writing them at all.)

One thing you’ll have to remember to do is tap the More action button so you can flip the toggle that’ll make Associate appear in the Share sheet.

Add share sheet actions

I know all the usual suspects in the Apple world have been singing the praises of Associate today. When that happens, and you’ve just read your fifth post on the virtues of a newly released app, you start to wonder if the fix is in. Are the positive reviews just because the developer is in the inner circle, a friend of the reviewers? All I can say is I bought the app today, got it set up with my Associates ID, and put it through its paces on the kind of linking I want to do. It works well, it works quickly, and I’m glad I spent the $5.

Also, here are the pens I like.


  1. Like many apps, the Amazon app is basically a web view, although it’s better designed than most such apps.