Tag Archive: “applescript”

Save Selection and Compare Selected Files scripts for BBEdit

Here are a pair of handy scripts for BBEdit (or TextWrangler). Paste them in to Applescript Editor, save them in your BBEdit Scripts folder, and assign shortcut keys in BBEdit > Preferences > Menus & Shortcuts.

Save Selection as New File

Select some text and invoke this script to create and save a new file containing the selection. (Add close doc to the end of the block if you don’t need to edit the file.)

tell application "BBEdit"
	set txt to contents of selection
	set doc to make new document with properties {contents:txt}
	save doc
end tell

This is pretty similar to choosing File > New > Text Document (with selection) and then choosing Save, but it combines the two into one step, which is helpful if you need to do this repeatedly.

Compare Selected Project Files

Use this script to compare two files selected in the files list of a disk browser or project window.

tell application "BBEdit"
	set selectedProjectItems to selected items of project window 1
	if (count of selectedProjectItems) is not 2 then return
	set comparisonResults to compare first item of selectedProjectItems against second item of selectedProjectItems
	if not differences found of comparisonResults then
		display alert "No differences found:" message reason for no differences of comparisonResults
	end if
end tell

You can accomplish the same thing with Search > Find Differences, but if you’ve already selected some files this is quicker.

Posted on Thursday, March 29th, 2012.

Reading List integration for Vienna

Vienna is a desktop newsreader for OS X. Here is an AppleScript to add the article currently selected in Vienna to Safari’s Reading List:

tell application "Vienna"
	set the_article to current article
	try
		set article_URL to the link of the_article
		set article_title to the title of the_article
		set article_intro to paragraph 1 of (body of the_article as text)
		tell application "Safari" to add reading list item article_URL with title article_title and preview text article_intro
	end try
end tell

Here’s a downloadable version. Stick it in your script runner and read some articles. Requires Mac OS X Ten Point Recent.

Posted on Thursday, February 16th, 2012.

XML Parsing in AppleScript

I recently wrote a script that posts bookmarks from Yojimbo to Pinboard. Now that I have a nice system for posting links, I find that I’d like even more integration. For instance, I’d like to be able to keep Yojimbo in sync by downloading any links I post to Pinboard via other interfaces.

The Pinboard/Delicious API provides a variety of methods for retrieving link information, but there isn’t a straightforward way to parse the results in AppleScript. In my posting script, I just search the response for code="done". This is sufficient to tell if the add method succeeded, but methods that return more detailed information need more rigorous handling.

Incidentally, AppleScript supports direct interaction with SOAP or XML-RPC web services; see the documentation here. However, the Pinboard API uses a different architecture, REST (sort of).

Essentially, a query is represented as a URL, with parameters, and the XML results are returned by the server like the content of any other web page. (I use do shell script and curl to make the request and get the raw results; I don’t know if there is a better AppleScript idiom for general HTTP transactions. URL Access Scripting is dusty and requires writing temporary files.) The task is to extract the relevant bookmark information from the response.

As it turns out, the System Events scripting dictionary includes an XML suite, pictured above. Some experiments based on the examples given in this discussion at MacScripter confirm that it can be used to parse data returned by the Pinboard API. So, all the pieces necessary to implement a more sophisticated AppleScript bookmark syncing system are available.

Posted on Saturday, February 20th, 2010.

Safari, NetNewsWire, Yojimbo, and Pinboard

Bookmark Page in Yojimbo

This is a script for Safari. It’s a variation on my Bookmark in Yojimbo script. The original “Bookmark & Edit” option has been replaced with “Bookmark & Post to Pinboard”. I rarely want to edit a bookmark or its properties, but soon I might start sharing some links on Pinboard.

Bookmark News in Yojimbo

This is a script for NetNewsWire, a free feed reader for Mac OS X. It works just like the above script for Safari. It saves a bookmark to the current news item, and provides an option to spread the news by posting the bookmark to Pinboard. This is what I’m going to do instead of sharing items in Google Reader.

Post Bookmark to Pinboard

This is a script for Yojimbo, the program I use to save bookmarks and other bits of information. It posts the selected bookmark items to Pinboard. This script is invoked [and thus required] by the “Bookmark & Post to Pinboard” option in both scripts listed above.

Before posting a bookmark, the script prompts you to enter a description. The initial value of the description is based on the Yojimbo item comments. If you click “Post”, the bookmark will be posted with the given description but the Yojimbo item comments will not be changed; “Post & Update Comments” will change the item comments to match the entered description.

To help you keep track of what you’ve shared, items posted to Pinboard are added to a “Pinboard” collection, which will be created if it doesn’t already exist. As an example, here’s what my collection looks like at the moment:

And here’s how it looks on Pinboard:

Limitations

These scripts don’t deal with tags, labels, stars, or other features offered by Yojimbo or Pinboard. These scripts aren’t likely to be very useful if you’re not me. I don’t know exactly what happens if you post the same bookmark to Pinboard twice; I think it’s updated automatically.

There are bugs.

Download

Download all three scripts here (24 KB)

Configuration

Some assembly is required. I recommend installing the scripts in the following locations (create the directories if they don’t already exist). Run them with FastScripts or the regular script menu.

~/Library/Scripts/Applications/Safari/Bookmark Page in Yojimbo.scpt
~/Library/Scripts/Applications/NetNewsWire/Bookmark News in Yojimbo.scpt
~/Library/Scripts/Applications/Yojimbo/Post Bookmark to Pinboard.scpt

Edit the _pinboard_script_path property at the top of both “Bookmark in Yojimbo” scripts to identify the full path to your copy of the Pinboard script. In my case, this reads:

property _pinboard_script_path : "/Users/anoved/Library/Scripts/Applications/Yojimbo/Post Bookmark to Pinboard.scpt"

Lastly, edit the _API_username and _API_password properties at the top of the Pinboard script to reflect your Pinboard login information. Since the Pinboard API is essentially a clone of the Delicious API, it should be straightforward to modify this script to work with Delicious – but unfortunately I haven’t quite got it working. Don’t hold your breath for an update, but please do share any fixes or improvements.

Acknowledgements

The code to post to Pinboard is derived from this NetNewsWire to Delicious script by Larry and Andrew. My sketchy URL encoding code is lifted from this example at Mac OS X Automation.

Posted on Monday, February 15th, 2010.

yjnote

yjnote is a Mac OS X command-line tool for creating note items in Yojimbo. You can use it to pipe the output or usage notes of other programs into Yojimbo for reference or logging purposes.

Download

Download yjnote.zip (1.7 KB)

Tested, minimally, with Yojimbo 2.1 on Mac OS X 10.6.2. yjnote is written in Perl with a pinch of AppleScript. Further testing, bug fixes, and improvements are welcome (and likely necessary).

Usage

You can display this documentation at any time with yjnote -help.

NAME
       yjnote − Create Yojimbo notes from the command line.

SYNOPSIS
         yjnote [options] [FILE...]

DESCRIPTION
       yjnote creates a Yojimbo note by concatenating the contents of the
       given files (or by reading standard input, if no files are given).

OPTIONS
       −title TEXT
           Sets the note item title.

       −comments TEXT
           Sets the note item comments.

       −label TEXT
           Sets the note item label. Ignored if the label does not exist.

       −tags TAG[,TAG,...]
           Sets the note item tags. Separate tags with commas.

       −flagged
           Flags the note item.

       −reveal
           Reveals the newly created note in Yojimbo. By default, the new note
           is not displayed.

Posted on Sunday, December 13th, 2009.

Bookmark in Yojimbo compatible with Yojimbo 2.0

The current version of my Bookmark in Yojimbo script is compatible with the recently-released Yojimbo 2.0.

Posted on Wednesday, September 2nd, 2009.

FastScripts 2.4

Daniel Jalkut has updated FastScripts, my utility of choice for running scripts or opening favorite files with a keystroke or a trip to the menu bar.

My FastScripts menu, as it appears in the Finder.

My FastScripts menu, as it appears in the Finder.

Notably, there is now just one version of FastScripts, which may be used for free or upgraded to allow more than ten keyboard shortcuts.

Posted on Monday, June 22nd, 2009.

FastScripts as Apple Menu

Back in the day, the System Folder contained an Apple Menu Items folder, the contents of which appeared in the Apple menu. Choosing an item from the menu opened the item from the folder. It was easy to customize the Apple menu by adding folders and aliases to the Apple Menu Items folder.

Here’s what it looked like by default in Mac OS 8.5:

Apple Menu Items folder

In Mac OS X, of course, the Apple menu is no longer customizable. It still contains some useful items, but it’s not quite the familiar place it once was. The Dock has taken its place as a quick way to access favorite files and folders.

So, if you want to access a bunch of applications from a tidy little menu, and you don’t find it useful to throw your whole Applications folder in the Dock, you turn to the world of third party launchers. Here’s an unlikely solution.

FastScripts is a program I recommend for running scripts with keyboard shortcuts. It’s a power user thing. However, a little-advertised fact about FastScripts is that it will open anything in your Scripts folder, not just scripts.

Choose Open Scripts Folder from the FastScripts menu, and put whatever you’d like—including subfolders—in the folder that appears. Now you can launch those files from the FastScripts menu:

FastScripts menu folder

The script items in the menu reside in a subfolder of the Scripts/Applications folder. The subfolders are named after specific applications, and appear in the menu only when that application is frontmost.

Posted on Saturday, September 13th, 2008.

Bricksmith Custom Categories

Here is a hack to add new categories to the Bricksmith part browser. The part catalog is normally organized into categories based on the type of each part (such as brick, plate, or tile). However, if you’re building a model based on a real set, it’s just as useful to present the parts from that set in one category.

Adding a category from a Peeron inventory

To create a category consisting of the parts from set 4891, enter 4891 and click Peeron Inventory:

Screenshot: Naming a category based on a Peeron inventory

The script will retrieve the corresponding list of parts and populate a new category containing them:

Screenshot: Browsing a category retrieved from Peeron

Adding a category from a file

To create a category based on an arbitrary group of parts, save a list of part file names, like this:

3005.dat
3004.dat
3003.dat
3001.dat

Then enter a name for your category and click From File. You’ll be prompted to select the list you just created.

Screenshot: Naming a category to be loaded from a file

The script will add the listed parts to the named category:

Screenshot: Browsing a category loaded from a file

Limitations

The script is awfully slow. AppleScript’s property list commands are not well suited for making many changes.

Parts listed in the file or inventory which are not already present in your part library will not be included in the category.

New categories are not visible until you restart Bricksmith. (I recommend quitting Bricksmith before running the script anyway.) Custom categories are lost when you reload the part list from Bricksmith’s Parts preferences.

No feedback is provided while the script is running.

Errors are not handled particularly well.

Mac OS X 10.5 is required.

It’s really slow.

Download

Bricksmith Catalog Custom Categories 26.4 KB

Posted on Saturday, June 21st, 2008.

Note Poster

Here’s a script to create a new post in MarsEdit from a note selected in Yojimbo. Why use a script when it’s already as easy as copy and paste? Well, maybe your computer is running out of paste.

tell application "Yojimbo"
	set _items to selected items of browser window 1
	if (_items is {}) or (_items is missing value) then return
	set _note to item 1 of _items
	if class of _note is not note item then return
end tell

tell application "MarsEdit"
	set _post to make new document
	set body of _post to contents of contents of _note
	set title of _post to name of _note
	activate
end tell

Click here to open the script directly in Script Editor. Save it in ~/Library/Scripts/Applications/Yojimbo and you’re ready to run.

Posted on Sunday, May 11th, 2008.