This Week's Sponsor:

Collections Database

A Powerful Database with iCloud Sync


Posts tagged with "applescript"

Change Text Case With AppleScript

Last night, I was looking for a quick way to change a string of text from lowercase to Title Case, which is the format I use for headlines here at MacStories. Normally, I would recommend installing WordService by DEVONtechnologies, but that’s a system Service, and I don’t seem to be able to install those without logging out and back in (I didn’t want to log out).

As I’ve come to learn lately, when you’re looking for ways to automate your Mac, the solution has likely already been posted on MacScripter. Among all the possible combinations of AppleScript to change text to a particular case format, I like this one by forum member “kai”. Essentially, the script takes the someText property and transforms its text items through changeCase to four possible options: upper, lower, title, and sentence.

To customize the script to my needs, I set someText to get the contents of my clipboard, change the case, then turn the result over to the clipboard again. In this way, I can select any text, copy it (so the original version is available in ClipMenu’s history), change the case, and paste back. To run AppleScripts with a keyboard shortcut, I use either Keyboard Maestro or Alfred.

Check out the AppleScript here.


Send Favorite Tweets To OmniFocus’ Inbox

In my daily “social networking workflow”, I use the “favorite” feature of Twitter as a todo list of sorts. I couldn’t find a way to add favorites to OmniFocus without leveraging email as a bridge, so I built a solution myself.

Using IFTTT, a single line of bash, Hazel, and AppleScript, I created a simple way to turn a favorite tweet into an OmniFocus task in the application’s inbox, ready for future processing. As an extra, I have also created a more “advanced” version that adds Automator to the mix to only extract URLs from favorite tweets. Read more


Turn URLs and Webpages Into PDFs In Your Dropbox

I stumble across a lot of interesting webpages on a daily basis. Sometimes it’s a video I want to watch later; sometimes it’s an article I don’t have time to read right away. Other times, I find a webpage that I want to keep around for future reference. For me, there’s a difference between articles to read later and reference material: whereas a new item added to Instapaper has a short life span in terms of attention (read, share, archive), a webpage I want to keep around forever needs to be turned into a document I can read anywhere, highlight, annotate, and carry around between platforms and devices. For that, I like PDFs.

I keep a “PDFs” folder in my Dropbox that contains all the documents I check upon regularly for work and personal purposes. They can be eBooks, tutorials, or guidelines from Apple that are essential to my writing online. Thanks to the increasing support for cloud services in apps like PDF Expert, GoodReader, and iAnnotate, I can keep a single copy of a PDF in my Dropbox, use the app I want to annotate the document with, and forget about duplicates thanks to sync. Furthermore, I’m fairly sure that, due to their popularity, PDFs will still be readable and supported 20 years from now, so I don’t have to worry about data preservation and file formats.

Lately, I have become obsessed with turning longer articles I find on the Internet also into PDFs for long-term archival. For as much as I like Instapaper, I can’t be sure that the service will be around in the next decades, and I don’t want my archive of longform and quality content to be lost in the cloud. So I have come up with a way to combine Instapaper with the benefit of PDFs, Dropbox, and automation to generate documents off any link or webpage, from any device, within seconds.

(Disclaimer: what follows is an explanation of a hack I created for personal use. It uses publicly available tools and apps to fill a personal need. You shouldn’t create PDFs off websites and redistribute them – you should support the sites you read instead).

In short, I use the Instapaper Text bookmarklet to fetch a webpage’s text and images (while preserving hyperlinks and great typography) and I convert the resulting page to PDF using wkpdf. Created by Christian Plessl, wkpdf is a command line tool that uses WebKit and RubyCocoa for rendering HTML content to PDF. Since wkpdf uses WebKit’s HTML rendering, it can generate good-looking PDFs that maintain most CSS2 and CSS3 stylings and properties. I have tried another command line tool for file conversion, Pandoc, but I like wkpdf better for straight HTML to PDF conversion. Read more


Save Safari Tabs As A List in OmniFocus

Save Safari Tabs As A List in OmniFocus

This is a nice tip to save the tabs of the frontmost Safari window as a list in OmniFocus (via Shawn Blanc). Through AppleScript, tabs are saved as webpage titles and URLs in a note in the OmniFocus inbox; the note is named with the “URL List from Safari Tabs” + timestamp format.

Using OmniFocus’ AppleScript Dictionary, you could modify the script to assign a specific context to the task for easy retrieval of your tab lists with Perspectives or OmniFocus’ own URLs. For the latter option, you can use Launch Center on iOS to quickly jump to OmniFocus projects and contexts; also keep in mind that any OmniFocus item (a task, a project, a context) has a unique ID that you can get using the “Copy as Link” option from the app’s contextual menu.

Permalink

Sprinkle a Dash of Cocoa in Your AppleScripts

The framework AppleScriptObjC allows users to write scripts with an interesting fusion of the AppleScript and Objective-C languages. Specifically, Apple describes the framework as:

AppleScriptObjC lets AppleScript objects serve as Objective-C objects in the Cocoa runtime

Basically this means that you can use simplistic Objective-C code in an AppleScript with the seemingly easier to grasp syntax of AppleScript. If none of this makes any sense yet then just hang in there and I will explain in a very visual tutorial in which I will build a sample Cocoa-AppleScript app that simply sends a notification to the Notification Center and quits.

Read more


Sync Your iOS Device Wirelessly with AppleScript

Sync Your iOS Device Wirelessly with AppleScript

Doug Adams shares a great AppleScript to initiate a wireless iTunes sync session, set up so that it runs automatically once a day:

A while back, I retired my iPhone 3G to the bedside table after buying an iPhone 4. I use the 3G pretty much as glorified clock radio-iPod Touch. I have a few radio apps on it and the Digital Clock app. I also have it set to sync and back up over Wi-Fi to my main iMac so I manually initiate a sync when I need to update Podcasts and apps and what have you.

This manual syncing has become tiresome. (I mean, if I used a traditional clock radio, I wouldn’t have to update its content manually, right?)

It’s simple enough to write an AppleScript to sync a connected iPhone but I want the script to run on a regular basis without me having to fire it. I like to listen to Podcasts in the evening so sometime during dinner would be a good time to update the 3G with any Podcasts that have arrived during the day. For this, I can create a launchd agent to fire the AppleScript that syncs the 3G in the background.

I had cron already set up (I followed this post), and I can confirm the script works really well (as you can see, I changed the script with “4S” for my device). Obviously, you can use this script without cron as well – it’ll simply sync with iTunes, but I guess it sort of defeats the purpose of this tutorial (unless you have a way to launch AppleScripts remotely). Doug also posted a similar script for automatically updating expired podcasts, which you can check out here.

Permalink

Scripting BBEdit - Part I

I was recently listening to an episode of the popular podcast Build and Analyze where Marco Arment was discussing his experience of leaving the comfort of TextMate to test drive BBEdit for a week. The results of his experiment were similar to many others who have attempted the same feat and after that week, according to Marco, he had returned to TextMate. I once did the same experiment. A dedicated TextMate user who switched to BBEdit for no other reason than the lack of updates and BBEdit happened to be in the Mac App Store at a greatly reduced price. I jumped on the opportunity to try the legendary text editor with its newly added fullscreen, auto-save, and resume features.

The humorous part of listening to Marco describe his time with BBEdit was knowing that we shared identical opinions on all of the quirks that differentiate TextMate and BBEdit. I don’t think one is any better than the other, I just think they solve the same problem with two different methods. What features BBEdit lacks, it generally makes up for in scripting ability. It has one of the most detailed and feature-rich AppleScript dictionary I have ever seen. So as I came across quirks that I could change with AppleScript, I wrote a script for them and that is what I would like to share in this mini-series of posts. Please note these articles are intended for people that are familiar enough with BBEdit to be able to add a new script and assign a keyboard shortcut in the preferences.

Wrapping Text

The first thing that drove me absolutely crazy with BBEdit is when hitting the quotes or parentheses key while text is selected, BBEdit doesn’t wrap the text in that punctuation but instead writes over the selection.

To alleviate this issue I wrote six AppleScripts (one for each commonly wrapped punctuation) and I assigned each one a keyboard shortcut. In this situation I think the keyboard shortcut is just as important as the script itself. It has to be easy to remember and close to the position your hand is already in when typing the punctuation key. So in five of the six shortcuts I simply added the command key and it felt as natural as typing the punctuation normally. For the single quotes script I settled with the Option + Command combo because it is on the same key as the double quotes.

  • Wrap in Asterisks: ⇧ + ⌘ + *
  • Wrap in Backticks: ⇧ + ⌘ + `
  • Wrap in Brackets: ⇧ + ⌘ + ]
  • Wrap in Parentheses: ⇧ + ⌘ + )
  • Wrap in Quotes: ⇧ + ⌘ + “
  • Wrap in Single Quotes: ⌥ + ⌘ + ’

All six scripts are variations of the same code shown below:

Download these scripts here: Wrap Text scripts

Save these scripts to the directory: ~/Library/Application Support/BBEdit/Scripts

Commenting Entire Line

The second feature of BBEdit that bugged me was the way commenting was implemented. Most of the time when I hit the “Comment” keyboard shortcut I actually wanted to comment out the entire line; instead, BBEdit starts the comment wherever the cursor is located. I wrote a script that reads the documents source language and comments out the entire current line using the correct syntax for that language.

For the keyboard shortcut I actually changed BBEdit’s default Un/Comment Selection to ⌥ + ⌘ + / so that I could configure my script with the default combination I was already used to: ⌘ + /

The script is configured to work with but is not limited to these languages:

  • Unix Shell Script
  • Python
  • Perl
  • Ruby
  • Java
  • Javascript
  • PHP
  • Objective-C
  • ANSI C
  • C++
  • HTML

If you don’t see the language you want it is very easy to add your own – just open the script and take a look at the code.

Download the script here: Comment Entire Line

Save these scripts to the directory: ~/Library/Application Support/BBEdit/Scripts

*Stay tuned for more posts on ways to really get the most out of BBEdit!


Extending Evernote: AppleScripts for Chrome, Safari, Instapaper and Alfred

Evernote is a cross-platform note taking solution that is adored by many for its ability to not just store snippets of information but also serve as an external brain. Among the many reasons users are drawn to this product is its extensibility. You may not realize this but Evernote has a very in-depth AppleScript dictionary that you can use to extend the feature set and make it do some pretty neat things you can’t do with it out of the box. I am going to show you how simple AppleScripting can add a few cool features to your Evernote workflow.

Note: These scripts work best when called with a global keyboard shortcut using an app like Keyboard Maestro, FastScripts, or Alfred.

This first AppleScript is used for storing a URL to whatever webpage you are currently viewing and it works with Safari, Chrome, and Chrome Canary. By default Evernote stores the entire webpage including images, navigational menus, and even advertising links. Having a snapshot of a page is great for some situations, but most of the time you just want to grab a link to the site and safely store it in Evernote so it can be easily retrieved. This script will check to see which browsers are running and it will grab the frontmost tab from the browser that is currently active; it’ll then neatly format the information into a note with the proper title and source url, and automatically sync Evernote.

Here is an example of a page I enjoyed and wanted to be able to find again at a later time. Everything is neatly formatted with no extra fluff.

URL to Evernote

URL to Evernote

Here is an example macro to launch it from Keyboard Maestro:

Download the script: Webpage Link to Evernote AppleScript Read more


Customize Your Mac’s “Paste and Match Style”

On the Mac, the paste function associated with the CMD + V keyboard shortcut has a well-known side effect: if you’re writing a document and you paste something from your clipboard, the document’s style won’t be automatically matched. Quite possibly a “feature” per Apple’s design choice, since I switched to the Mac I’ve always been annoyed by such “remember every style” behavior, and from the reaction I’ve seen on Twitter, it turns out quite a few people dislike when they end up with weird styles in their outlines or email messages as well.

But, you might argue, there is a way to paste and match style, and that’s represented by the ⌥⇧⌘V shortcut…which requires four fingers to execute, and it’s not always implemented in the same way in third party apps. Chrome, for instance, uses ⇧⌘V, and many other apps follow a similar trend to modify the default shortcut for Paste and Match Style.

In the past few days, I’ve come across some interesting solutions to unify Paste and Match Style with a simpler keyboard shortcut, and make it the default paste action on OS X if you never want to paste with “styles”. This can be particularly handy for writers, who often prefer a plain text environment or a styled one that’s, however, consistent throughout a document. Read more