This Week's Sponsor:

DEVONTHINK

Store, Organize, and Work the Smart Way


Posts tagged with "safari"

Getting Safari’s Selection on iPad As HTML With A JavaScript Bookmarklet

I modified this bookmarklet posted by “Tim Down” on StackOverflow to send selected text from Safari to Drafts as HTML. The result is the following code:

javascript:(function(){var%20h="",s,g,c,i;if(window.getSelection){s=window.getSelection();if(s.rangeCount){c=document.createElement("div");for(i=0;i<s.rangeCount;++i){c.appendChild(s.getRangeAt(i).cloneContents());}h=c.innerHTML}}else%20if((s=document.selection)&&s.type=="Text"){h=s.createRange().htmlText;}window.location='drafts://x-callback-url/create?text='+(h);})()

So let’s say you want to grab the first paragraph in this post. Normally, in Safari for iPad you’d end up with the plain text fetched by window.getSelection:

This is a fantastic report with lots of data points for any developer trying to get their apps featured by Apple. Dave Addey’s highly interactive regional graphs and notes are very well done. Be sure to check out Dave Addey’s other works on his main blog.

As you can see, formatting and hyperlinks have been removed. With the bookmarklet above, you’ll receive the HTML version of the selection – which looks like this. But what’s the point?

My idea was that I wanted to be able to automate the process of capturing rich text from iOS’ Safari; I wanted to achieve the same kind of functionality I have on the Mac, where rich text can be dragged from Safari or Chrome and dropped into Evernote, preserving styles, hyperlinks, and images. I thought that combining HTML output with an Evernote Append action (with the “Send as Markdown HTML” option turned on) would let me receive valid HTML content in Evernote starting from an iOS workflow. And, for the most part, I was right, because the workflow does mostly work.

As it turns out, Evernote is extremely cautious with the HTML tags they accept, and the ones that are supported follow the XHTML guidelines as ENML is a superset of XHTML. This means that my bookmarklet will work for something as simple as selecting a single paragraph, but may easily fail with multiple selections, inline images, complex styles, and so forth. When that will happen, Drafts will return an error when trying to append HTML to Evernote; obviously, this will work just fine with Dropbox, which doesn’t care about the kind of text you’re using in your actions. Even better, this should work very well with Textastic’s just-released update that supports x-callback-url.

I guess the solution would be to build a Pythonista-based converter for Evernote-approved XHTML tags and place it between Safari and Drafts, converting HTML tags Evernote won’t like to compatible ones. If you’re interested, my birthday is August 10.

Permalink

Enhancing Reminders with AppleScript and Macros

As The Omni Group keeps working on OmniFocus 2 for Mac and Apple continues seeding new betas of iOS 7 and OS X Mavericks to developers, I have been reconsidering Reminders’ simplicity and enjoying the built-in iCloud sync, which, unlike other types of iCloud, is working fine for me. However, two things I miss from OmniFocus are the possibility to integrate the app with a web browser through bookmarklets and the system-wide Quick Entry panel; I use both tools on a daily basis to easily save a browser’s tab into OmniFocus’ Inbox, or to bring up a text field where I can jot down an idea and know that, no matter the app I’m using, it’ll be saved into OmniFocus. Luckily for me, Apple’s Reminders app comes with a good AppleScript Dictionary, which is likely something that Reminders’ core mainstream audience won’t ever care about, but that we can leverage to extend the app’s capabilities and input areas beyond Mountain Lion’s leather-and-paper window.

Read more


Many Tricks’ New Safari Extensions

Cool new Safari extensions by Many Tricks, developers – among other apps – of Name Mangler (which I plan on covering soon). I like the vBulletin one:

This extension is for those who use vBulletin forum sites. It adds a contextual menu that lets you open all unread article links in new tabs, with a single click. As of now, it only works for vBulletin, but if you use forum sites based on other systems, we may be able to get it working if you can give us a URL to look at.

It’d be nice to have Chrome versions, though.

(via Dan Frakes)

Permalink

A Better Chrome To Safari Bookmarklet

pythonista

pythonista

In January, I tried to put together a bookmarklet to send the webpage currently open in Google Chrome for iOS to Apple’s Safari. That turned out to be a surprisingly complex effort as Google didn’t think offering an “Open In Safari” option would be a good idea, and the app’s URL scheme produced some interesting results when opening and closing Chrome.

I was reminded of the bookmarklet this morning by reader @CNWLshadow, and I realized that I never posted the solution I settled with. It consists of a browser bookmarklet and a Pythonista script, and it works with just one tap.

Read more


Chaining Multiple Apps Together with Drafts

A few weeks ago, I took a look at the automation possibilities opened by Drafts, Agile Tortoise’s multi-purpose text app. In the article, I mentioned how a bug prevented Drafts from “linking to itself” more than once:

Therefore, my idea for cross-posting was: I can link to Drafts itself, and if the first action is successful, I can link to Drafts itself again. Essentially, I wanted to leverage the built-in App.net and Twitter actions to avoid the use of any third-party app. Unfortunately, there’s a bug in the current version of Drafts that doesn’t make that kind of action work.

With an update to Drafts released yesterday, Greg Pierce has brought various improvements to the app, including support for more customizable timestamps and dates using strftime, date and time tags for file names and URL actions, and a new way to encode strings with curly brackets.

Seemingly minor, the option to more easily URL encode strings is actually a very welcome addition: like in the latest Mr. Reader, instead of forcing the user to encode a URL into a longer string, you can simply put a URL inside {{ }} and let Drafts take care of encoding it. It means I can now experiment with building more complex workflows that contain actions for more external apps and, more importantly, for “sequential” tasks in Drafts itself. Easier encoding means we construct URLs that will tell Drafts “do this, and then do that” in a single workflow.

Those who follow me on Twitter know that I’ve been trying since yesterday to see how many apps I could chain together in a workflow, mainly out of curiosity and as a “proof” of concept. First, I tweeted about a Mr. Reader -> Drafts -> Poster workflow that would take selected text from an article, convert its Markdown to HTML, and then send it to Poster; the workflow consisted of three apps chained together, but I knew I could try to accomplish something a bit more ambitious. I kept on experimenting with Drafts URLs, and eventually I managed to build a single workflow with 3 apps and 4 different tasks involved. I’m posting it here for two reasons: a) I believe it’s a quite useful workflow; and b) it can serve as an example of what Drafts can do when you understand how to properly link multiple apps together. Read more


Chrome for iOS: Send A Webpage Back To Safari Via Bookmarklet

Sort of. Here’s a fun experiment.

Today, I wanted to quickly send a URL from Chrome for iOS – my default browser – back to Safari. I know there are ways to do Safari-to-Chrome, but I wanted the opposite: from Chrome back to Safari. I needed to install some custom Mr. Reader actions, and Chrome was giving an error when tapping on the downloadable files. I figured I could make a bookmarklet to take the current webpage in Chrome and send it to Safari.

Not so fast. There’s no documented URL scheme on iOS for opening web links in Safari, except, well, the http:// scheme itself. In testing various bookmarklet ideas, I thought that replacing googlechrome with http in Jon Abrams’ bookmarklet would force Chrome to send a link to Safari. But as It Turns Out™, doing this sort of trick in Chrome for iOS:

javascript:window.open('http'+location.href.substring(4));

…simply opens a new tab in Chrome.

What I ended up using is a hack – and a very curious one – to leverage Chrome’s support for x-callback-url to open a link back into Safari. I was inspired by Cormac Relf’s script, which I discovered yesterday when he showed me another script he made for Pythonista.

javascript:window.location='googlechrome-x-callback://x-callback-url/open/?url='+encodeURIComponent(location.href)+'&x-source=Safari&x-success='+encodeURIComponent(location.href);

As you can see above, we’re telling Chrome to open a new tab using…itself. The trick, at least theoretically, is to use an encoded location.href string to call back Safari, which is registered for the http:// scheme that Chrome, in this case, opens “externally”. Displaying x-source is needed per Google’s URL scheme specification; the name you give to x-source will be displayed as a “back” button in Chrome (as shown in the image above).

This is a profoundly inelegant and ultimately flawed solution. To make this “work” you have to:

  • Type the bookmarklet’s name, because Chrome has no bookmarks bar;
  • Nothing will happen.
  • Close Chrome;
  • Re-open it;
  • A wild new tab appears!
  • Tap the Safari button. It’s super-effective.
  • Safari will launch the link, closing the additional tab Chrome decided to open.

What is going on, exactly? Via JavaScript, we’ve forced Chrome to open a tab in itself, but doing so with x-callback-url inside a bookmarklet creates, for some reason, quite a strange behavior: the tab isn’t opened unless you close and re-open Chrome, therefore partially defeating the whole purpose of this bookmarklet, which is to quickly open a webpage in Safari. But, in spite of the clunky process, a new tab with a “Safari button” is created nevertheless, allowing you to tap it to launch Safari and close Chrome’s extra tab.

My conclusion is that we have three solutions: a) it’s not possible to create a straightforward Chrome-to-Safari bookmarklet; b) it’s possible in another way that I haven’t explored; or c) it’s possible with the x-callback-url hack, but in a different way.

If you have ideas, ping me on Twitter.


“Open In” and Mobile Safari

“Open In” and Mobile Safari

Continuing the discussion about the “Open In” menu for iOS, David Chartier proposes “Open In” for Safari URLs:

Finally, Document Sharing in Mobile Safari would further promote an app-centric workflow on iOS. Bookmarklets are often designed to open another web service in a new browser tab, and let’s face it, working on the web is a crummy experience. But even if they’re wired to open an app, bookmarklets are still a colossal pain to install which cuts off most attempts at the knees. This largely confines Mobile Safari and its content to an island, making iOS’s URL-to-app workflow needlessly tedious for anyone brave enough to try it.

In its current form, Mobile Safari only supports “Open In” for documents displayed in the browser, such as PDFs that Safari can render. The (new in iOS 6) share sheet doesn’t come with options to send a URL around, but only to copy it to the system clipboard.

Bookmarklets were never meant to take off among consumers, because they require a minimal amount of knowledge (or steps) that average users don’t want to deal with. However, developers had to resort to using bookmarklets because it was the only way to provide something that worked to pass a URL from Safari to a third-party app/web service. Some developers have gone out of their way to provide an “Install Bookmarklet” experience that wouldn’t scare off the majority of users.

Overall, “Open In” for links doesn’t sound like a bad idea. Imagine being able to quickly send a YouTube video to Facebook or a link to the Twitter app with an Apple-sanctioned menu and not some JavaScript hack. There are aspects I don’t know how I’d solve right now (How do supported apps appear in the iOS 6 share sheet? Are they available in a dedicated page, or can users re-arrange them? Could Siri be told to perform such actions?), but, generally speaking, providing better web-to-apps communication would be a good start.

Two years ago, Marco Arment offered some ideas on a possible “Send To” panel for Safari. This is absolutely still relevant today, because it hasn’t gotten better.

Obviously, that would be far from my envisioned iOS automation for power users. I’ve been trying the beta release of Alfred 2 lately, and I like how the developers created a workflow visualization that is both powerful and intuitive in the way it connects visually triggers to actions and outputs. Ideally, I’d love to see Apple considering an “Automator for iOS” – the kind of feature that most users don’t care about but that would likely make a subset of them reconsider iPads as “real work” machines. Apple could even go as far as making that kind of user automation look “cool” with the right interface decisions and a powerful inter-app communication layer that is not limited to Apple apps (read: with an API).

I hope this kind of stuff is in the cards for iOS 7.

Permalink

Automatically Send Articles From Reading List to Instapaper

Two days ago, Ben Brooks asked on App.net if anyone had come up with a way to share Safari Reading List items to Instapaper. His question made me realize that it would be a fun project to find out, so in my free time I put together a workflow that runs automatically and in the background on my Mac mini.

Please note, what follows is a raw experiment. I have tested it, and it works, but it’s far from stable. It uses GUI scripting in AppleScript to mark Reading List items as read, and it heavily depends on iCloud, which, unfortunately, is far from reliable when it comes to bookmark syncing. Nothing should happen to your bookmarks (the script simply “reads” them), but backups are recommended, as usual. Read more


Omnikey Simplifies Website-Specific Search In Safari

When I search the web for something, it’s mostly about music and artist discographies. Therefore I am used to the following typing workflow: “cmd+L” for toggling the URL bar, “en” to bring up the Wikipedia bookmark, open the site, and search there. I’ve often wished for a simpler key combination for quick access to such searches. Earlier this year I reviewed Bang On for iOS – a small app for easy website search on the iPhone and iPad. Using “!” as a toggling prefix you could create search commands for any website you can imagine, from Wikipedia to Amazon. It worked pretty well. I really wished for a similar functionality on the Mac.

Now, Safari users can have exactly this feature through a new extension called Omnikey, developed by Mario Estrada. Install it, and afterwards you’ll just need to type “wiki [search query]” into the Safari address bar to automatically open up the search results to your query on Wikipedia.org.

The “wiki” is the “omnikey” for toggling this kind of search. The same system also works for YouTube, Amazon, and some other websites out of the box. Put a “!” in front of your URL bar search query and Safari will use the default Google search. You can also easily add new websites you want quick Omnikey search for: just open up the extension’s editing panel in the toolbar, click “Add Site”, select a key for toggling the site search and paste a dedicated search query URL into the panel. Now you just have to replace the search query in the URL with a “{search}“ wildcard (like a software placeholder) and you’re done. I tried it with the MacStories search powered by DuckDuckGo, and it immediately worked.

Omnikey is a very handy Safari extension. It is lightweight, fast, and easy to use. Setting up new search keys is easy and works flawlessly. For people like me, who often need to search specific websites for links and information, Omnikey is a great companion. Download it for free at the project’s GitHub page.