by

A Neat Tridion Trick With the Pub Up Bookmarklet

Reading Time: 3 minutes

Just a short while ago, I announced a tiny lil’ Tridion bookmarklet called Pub Up!™. It was a very simple bookmarklet that left you in your current location in your folders, and just took you one publication higher. Well, when I announced this to the SDL Tridion MVP chat room, Nuno (AKA The King of Tridion), had a simple reply,

oh shiny
what about taking you to the owner publication?
select an item, get the item’s owning pub, go there

It’s been a while, so my memory is fuzzy on exactly how I responded, but it was something to the effect of, “programming is hard”.

With that, Nuno shared a neat trick. And, well, it sure is neat. Let’s discuss.

Have you met Anguilla?

Nuno shared a link to a Tridion Stack Exchange answer which he had provided not too terribly long ago. As it turns out, if you’re really polite and ask for it it, Tridion’s Anguilla framework can tell you what the owning publication is:

var x = $models.getItem("tcm:1-23");
x.load(); // Insert smart async stuff here
x.getOwningPublication().getId();

Have you used Anguilla in the console?

Now, there’s only one tiny downside to that piece of script: it doesn’t work.It’s not Nuno’s fault, though. It’s because the Tridion GUI is a crapload of frames. And frames are…well…keeping things in separate frames. So Nuno shared which frame you might need, if you needed to do Anguilla things:

window.top.frames[1]

That snippet will grab the frame where Anguilla lives.

So now, if you want to grab an item, you’d put these two snippets together, like so:

window.top.frames[1].$models.getItem(someTCM)

Get Happy with Anguilla…

If you use my Pub Up! bookmarklet, what it’ll do is load in my JavaScript file, navHappy.js. When the file loads, a navHappy object gets added to the window — and there’s quite a few useful methods on that object. One method in particular is, navHappy.getCurrentTCM(). That method returns the TCM ID (minus the prepended “tcm:”) by accessing and parsing a string from the Navigation object.

What this means, is that you can blend navHappy with Nuno’s neat trick about accessing the proper frame, and you can access the Anguilla object for whatever you have open in the Tridion GUI:

window.top.frames[1].$models.getItem('tcm:'
+ navHappy.getCurrentTCM());

So there you go. You can access whatever thing you have open in the Tridion GUI right there, and do all the Anguilla things you want to it.

If You Teach a Man to Fish…

You know the saying, “Give a man a fish, he eats for a day. Teach him to fish, he eats for a life time.” But, if you teach him to make fishing poles, he could probably come up with something better than fishing.

So, I made a fishing pole.

Introducing Anguilla Mediator

By fishing pole, I mean, I made a bookmarklet that does all of this fancy aforementioned hackery for you.

Anguilla Mediator

The Anguilla Mediator will use navHappy, and surface whatever the active item is for you as an object available on the window. I’ll even put it in the console for you, in case you want to inspect it.

currentItem is where you find all of the juicy bits
currentItem is where you find all of the juicy bits

Now, if you wanted to whip up your own bookmarklet, no big deal. You’ll find it as an object attached to the window:

Neat, isn't it?
Neat, isn’t it?

Some Code

If you wanted to look at the code, I put a copy of it over at Github. But, you can also just look at it over here: apis.frankmtaylor.com/frameMediator.js

In Conclusion

Bookmarklets would be way easier, and way smarter, if you used Anguilla. I made a thing to help you do that. Try it out here: Anguilla Mediator.

And if you wanted

Happy Bookmarkletting!