Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Mac OS X Tiger Dashboard Widget Development (2006)

.pdf
Скачиваний:
17
Добавлен:
17.08.2013
Размер:
9.7 Mб
Скачать

SecureCopy Widget

function copyFrom() {

document.getElementById(“fromButtonImage”).src = “Images/Button_down.png”;

var pathOfFileToCopy = document.getElementById(“fileText”).value; var userName = document.getElementById(“userText”).value;

var hostAddress = document.getElementById(“hostText”).value;

var destinationFilePath = document.getElementById(“fileText”).value;

var scpCommand = “/usr/bin/scp “+ userName + “@” + hostAddress + “:\”” + pathOfFileToCopy + “\” \”” + destinationFilePath + “\””;

var sysCommand = widget.system(scpCommand, null);

}

The reverse of the copyFrom() function is the copyTo() function. The copyTo() function builds the command from the fields in the widget and copies the file to the remote machine.

//The copyTo() function is called when the Copy button is pressed.

//It uses the widget.system call synchronously to execute scp with the command built from the information in the widget fields.

function copyTo() {

document.getElementById(“toButtonImage”).src = “Images/Button_down.png”;

var pathOfFileToCopy = document.getElementById(“fileText”).value; var userName = document.getElementById(“userText”).value;

var hostAddress = document.getElementById(“hostText”).value;

var destinationFilePath = document.getElementById(“fileText”).value;

var scpCommand = “/usr/bin/scp \””+ pathOfFileToCopy + “\” “ + userName + “@” + hostAddress + “:\”” + destinationFilePath + “\””;

var sysCommand = widget.system(scpCommand, null);

Using SecureCopy

Whenever you are connecting to another computer using ssh, you are challenged for a password when you connect. The scp utility functions the same way. If you want to copy a file, you’ll have to enter your password to do so. This becomes an immediate problem with the widget.system because it cannot respond to a password challenge. Shell scripting doesn’t allow for many solutions to respond to a password request and they can break easily.

You can create public and private keys for the Macintoshes that you copy files between and your secure copy can run without the need to respond to a password request. To do this, you create the keys using the sys-keygen command in Terminal (Figure 14-4).

221

Chapter 14

Figure 14-4

Enter ssh-keygen –t rsa on the command line in terminal. The –t rsa switch tells the sys-keygen key generator what type of keys to create, in this case, RSA keys. When you are prompted to enter a passphrase, press the Return key. The keys are created in the SSH subdirectory (with .ssh extension) of your home directory. If you don’t have an SSH directory, ssh-keygen creates one. When you change to the SSH directory and view the contents, you’ll see the keys that you’ve created.

[desklamp:~/.ssh] pfterry% ls

 

 

authorized_keys id_rsa

id_rsa.pub

known_hosts

[desklamp:~/.ssh] pfterry%

 

 

The id.rsa file contains the private key, and the id.rsa.pub file contains the public key. You will need to copy the public key to the Macintosh to which you copy files and add it to the authorized_keys file. Copy the public key file only to the Macintosh; the private key stays on your local Macintosh.

If the authorized_keys file doesn’t exist, you can create the file with the copy command.

cp id_rsa.pub authorized_keys

If the authorized_keys file already exists, you’ll need to append this key to the end of it with the unix cat and redirect commands.

222

SecureCopy Widget

[offhook:~/.ssh] pfterry% cat ~/id_rsa.pub >> authorized_keys

[offhook:~/.ssh] pfterry% rm ~/id_rsa.pub

Now when you perform a secure copy, you are not prompted for a password.

Summar y

While most widgets may provide help about a particular scripting language or collect the latest RSS feeds from a popular website, widgets can also provide a user friendly interface to command-line utilities through the widget.system method. The SecureCopy widget is an example.

223

15

Amazon Album Ar t

iTunes, the music player that ships with OS X, is almost overshadowed by the popularity of the iPod and all of the marketing fuss it has generated. iTunes, however, is the only bridge between Apple’s iTunes Music Store; it is a requirement to load music on the iPod. iTunes is probably a stealth sales tool for the iPod.

One of the cool features in iTunes is that it displays the cover art from your iTunes Music Store purchases in the lower-left corner of the iTunes window (Figure 15-1). However, if you have ripped your CD collection into iTunes (or you only purchase music in shrink-wrapped jewel cases), those albums will not include the cover art.

You can fix this manually by searching for your album on Amazon or another website and downloading the cover art. You can then drag the cover art into the album art window inside iTunes. This is a reasonable — if clunky — workaround, but it is time-consuming and too manual. After all, we are working with a Macintosh and it is the most scriptable computing platform available if you count all of the Unix scripting tools and AppleScript. Fortunately, someone realized this and created a widget to automate the process of adding the cover art to all of your ripped albums

in iTunes.

Chapter 15

Figure 15-1

Amazon Album Ar t

As the name indicates, Simon Whitaker’s Amazon Album Art widget is able to retrieve album artwork from Amazon for you. Moreover, it simplifies the manual process from the beginning by getting the track listing from iTunes and searching Amazon for the album with just a button click. In practice, you can display Dashboard when a track is playing that you know you don’t have the artwork for, click the iTunes button, add the album art to the track, and close Dashboard.

The Interface

Like a couple of the other widgets that we’ve looked at, the Amazon Album Art widget has a minimized interface (Figure 15-2) in addition to a full-sized view displaying the artwork that you are searching for and a back side panel. The real advantage for small screens and laptop users is that you can leave the widget open all of the time without it taking up too much screen real estate.

Figure 15-2

226

Amazon Album Art

Clicking the info button displays the preferences on the back side of the widget (Figure 15-3). The options let you select from a Preferred Amazon store, that is, a store related to the country in which you live. The Search for menu lets you select between popular and classical music. The Operate on menu allows you to apply the “set as album art action” to the currently playing track, the whole album, or the current selection. You can also choose to save small images (240 × 240) to iTunes and use a local proxy server to connect to the iTunes Music Store.

Figure 15-3

You can query iTunes for the currently playing track by clicking the small iTunes icon (Figure 15-4). Amazon Album Art gets the track information from iTunes and then searches Amazon to get the cover art for the album.

Figure 15-4

When the widget finds the cover art, it expands to display the art in the lower portion of the widget (Figure 15-5).

Figure 15-5

227

Chapter 15

If you want to set the cover art for the track that is currently playing or for the whole album, you can click the “Set as album art in iTunes” banner at the bottom of the screen (Figure 15-6).

Figure 15-6

The widget also allows you to search Amazon for albums by an artist. You can enter the name of the artist in the search field and press the Return key. When an album cover appears in the widget, clicking the thumbnails icon in the upper-right corner of the album cover displays the covers of all of the artist’s albums (Figure 15-7). Clicking one of the covers displays a large image of the cover, and then clicking the cover takes you to the album’s page on Amazon.

Figure 15-7

If the Amazon Album Art widget can’t find the album on Amazon, it gives you the option of searching Google for the cover art (Figure 15-8).

This option doesn’t automate inserting the cover art in iTunes, but it does provide the artwork if it can be found.

228

Amazon Album Art

Figure 15-8

Amazon Album Ar t Internals

When you look inside the Amazon Album Art widget (Figure 15-9), you see that it looks like any of the widgets that you’ve worked with. The main default images are at the root level of the widget with the graphics for the logo, search box, back side, and the pieces that are used for the expanded window all placed in an Images folder. Because the widget can search on any of the country-specific Amazon stores, the localization strings are in separate folders for each language (German, French, Netherlands, and Russian, in addition to English).

Figure 15-9

229

Chapter 15

You’ll notice a couple of different things about the Album Art widget. The Apple Classes contains the AppleAnimator.js, AppleButton.js, and AppleInfoButton.js JavaScript files. You can also see two AppleScript files at the root level of the widget.

Info.plist

In the properties list for the widget, you see that three access keys are set. AllowFileAcessOutsideOfWidget is set because the widget needs to interact with iTunes. The AllowNetworkAccess key is set because the widget needs to fetch the cover art from Amazon.

<?xml version=”1.0” encoding=”UTF-8”?>

<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>

<plist version=”1.0”> <dict>

<key>AllowFileAccessOutsideOfWidget</key>

<true/>

<key>AllowNetworkAccess</key>

<true/>

<key>AllowSystem</key>

<true/>

<key>CFBundleDisplayName</key> <string>Amazon Album Art</string> <key>CFBundleIdentifier</key>

<string>com.widget-foundry.widget.albumart</string> <key>CFBundleName</key>

<string>Amazon Album Art</string> <key>CFBundleShortVersionString</key> <string>2.0</string> <key>CFBundleVersion</key> <string>2.0</string> <key>CloseBoxInsetX</key> <integer>8</integer> <key>CloseBoxInsetY</key> <integer>4</integer> <key>Height</key> <integer>56</integer> <key>MainHTML</key> <string>amazonart.html</string> <key>Width</key> <integer>216</integer>

</dict>

</plist>

Finally, the AllowSystem key is included because the widget needs to run the GetSelectionDetails and SetCoverArt AppleScripts in its bundle.

HTML/CSS

The HTML and CSS files for the Amazon Album Art widget are fairly standard. The HTML file imports the CSS file and the JavaScripts for the buttons, localization strings, and the main JavaScript for the widget. The portion of the script that controls the artwork display on the widget’s front is the slidearea, fbottom, and ftop <div>s.

230