Section 9.7. Recalling Passwords


9.7. Recalling Passwords

If there's one downside to this whole Internet concept, it's the sheer number of passwords you have to remember. You've got a password for your email address, one for your banking Web site, a bunch for connecting to computers on your network, one for eBay, Amazon.com, .Mac, PayPalthe list goes on and on. How on earth are you supposed to remember all of them?

Well, if you're like many people, you try to use the same password for everythingthereby sparing yourself the trouble of remembering several hundred different passwords. Or perhaps you've realized that that method is very insecure (since if hackers get one of your passwords, they have access to everything), and you've decided to use a bunch of different passwords instead. Either way, you're risking something: either forgetting a password and locking yourself out, or inadvertently exposing your whole life to a malicious hacker.

Figure 9-10. Top: The script asks you to pick a file to upload.Middle: Using AppleScript's special choose URL command (basically, a simplified version of the Finder's Go Connect to Server command), the script asks you to pick the server you want to upload to. It can be a computer on your network, your Web site's FTP server, or even a Unix mainframe. (To make AppleScript remember the server in the future, click Add to Favorites.) Bottom: The script asks for your user name and password. If you want to connect anonymously, try entering "anonymous" in the Name field and your email address in the password field. And, if you want AppleScript to remember your name and password in the future, turn on Add to Keychain.


That's why Mac OS X comes with a feature called Keychainsspecial clusters of passwords that you have access to simply by logging into your Mac. Introduced with Mac OS 9, Keychains are the perfect compromise: you only have to remember one password (your account's Keychain password) to automatically unlock all the other passwords you use. And no hacker can get access to your passwords unless they're sitting right in front of your Maca fairly remote possibility.

Of course, this book wouldn't even mention Keychains if you couldn't AppleScript them. Here's a simple example to list all the passwords being saved in your Keychain:

tell application "Keychain Scripting"     set allKeys to the name of every key of keychain 1 end tell choose from list allKeys --Lists all the keys for you

Gem in the Rough
Open Location

You've got Safari for opening Web sites, Mail for creating email messages, the Finder for connecting to Web severs, and any number of other programs for handling your other Internet jobs. The problem is, there's no single command for handling any Internet jobor so you might think.

Luckily, AppleScript includes the helpful open location command, which comes pretty close. For instance, you can use this command to open a Web page, without even using a tell statement:

open location "www.apple.com"

Of course, as shown on Section 9.2, you can direct an open location command at a particular Web browser, if you want a particular browser to load the page. However, if you omit a tell statement, AppleScript locates your default Web browser (the one you set in Safari Preferences General) and opens the Web page in that browser.

Perhaps even more conveniently, though, you can use open location to create a new email message in your preferred email program. Here are a few possibilities:

--Create a blank email message: open location "mailto:" --Create a message addressed to someone: open location "mailto:stevejobs@apple.com" (* Create a message with an address and a subject: *) open location ¬ "mailto:stevejobs@apple.com?subject:G6 Mac"

(For even more fun with mailto links, take a look at http://www.ianr.unl.edu/internet/mailto.html.)

Finally, you can use open location to connect to serversboth on the Internet and on your own network. If there were a Mac in your house named Santa, for example, you could open it over your network with a command like this:

open location "afp://Santa.local/"

Whether you want to open a Web page, email message, or network server, therefore, your first AppleScript instinct should always be to try the open location command.


This script searches through your Keychain and displays every key (or password-protected item) your Mac is set to remember. If you have a lot of passwords in your Keychain, this script could take a few seconds to runand display a hugely long dialog box. If you've never used the Keychain Access utility before (Applications Utilities), chances are you'll be amazed when you see how big this list is.

As with the script on Section 9.6, this script may not work properly if you have Mac OS 9 installed on your computer. To work around this problem, delete Keychain Scripting from Mac OS 9's System Folder Scripting Additions folder, and then empty the Trash can.

One thing you'll notice, however, is that when you select one of the keys in the dialog box and click OK, nothing happens. And while seeing a plain list of your keys is useful for geek bragging privileges, that won't do much if you forget a password.

Luckily, you can modify your script to tell you the password to a key if you happen to forget it, like this:

tell application "Keychain Scripting"     set allKeys to the name of every key of keychain 1 end tell --Part 1: set keyName to (choose from list allKeys) --Part 2: tell application "Keychain Scripting"     tell keychain 1         set chosenKey to the first key whose name is (keyName as string)

Power Users' Clinic
Using Web Services with SOAP and XML-RPC

If you've spent some time picking around in your computer's built-in scripts, you may have noticed that the ones in your Library Scripts Internet Services folder are pretty unique. Not only do those scripts connect to the Internet to get information for you (a stock quote, for example), but they do it without ever opening Safari, Mail, or any other Internet programs. To an everyday AppleScripter, this seems like magic. To you, intrepid scripter, this is simply an obstacle on your quest to complete AppleScript enlightenment.

The trick to these scripts is a special kind of Internet communication, commonly referred to as Web services. AppleScript can use two types of Web servicesSOAP (Simple Object Access Protocol) and XML-RPC (Extensible Markup Language Remote Procedure Call)but both types are essentially just a way to communicate between your computer and various online databases.

In AppleScript, you use either of two commands to interact with Web services: call soap or call xmlrpc. And to locate Web services on the Internet that you can use with AppleScriptfrom global thermometers to foreign-language dictionariesbrowse a site like www.xmethods.com.

Unfortunately, the complex AppleScript syntax for Web services is far too involved to explain in this book (just take a look at your Internet Services scripts for proof). Thankfully, Apple has written an online book that explains everything you need to know to make AppleScript work with Web services; the document is available from http://developer.apple.com/documentation/AppleScript/InternetWeb-date.html.


        set chosenPassword to the password of chosenKey     end tell end tell --Part 3: display dialog "The password to the key you chose is " & chosenPassword

Here's how the new code works:

  • Part 1 lets you choose one of the keys, and then puts your choice into the keyName variable.

  • Part 2 locates the key you chose (all your keys are stored in keychain 1, in AppleScript's mind), and then finds the key's password (chosenPassword). While this part of the script is running, AppleScript asks you to authorize Script Editorjust to make sure some remote hacker isn't trying to get a password without your permission (Figure 9-11).

  • Part 3, finally, displays a dialog box with the password for the key you chose.

Figure 9-11. Whenever you try to access a password from your Keychain, you see this dialog box. In fact, if you run the script from Script Editor, you actually see this dialog box twice (don't worryit's just Mac OS X being its typical safety-freak self). If you want to proceed with the script, press Allow Once (to authorize Script Editor to display a password just this oncethe most secure method). Or, if you're into convenience, click Always Allow, and Script Editor will never ask you to authorize it again. Of course, to prevent AppleScript from displaying your password at all, just click Deny.


Now that you know how convenient Keychains are, make sure to click Add to Keychain in any program's dialog box that has a password you want Mac OS X to remember. Then, if you forget that password in the future, you can run this script to retrieve it



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net