9.2. SafariFor years, Microsoft was the dominant company in Mac Internet programs. Internet Explorer (IE), Microsoft's Netscape-devouring Web browser, came preinstalled on all Macs, and with good reason: for a while, IE was the fastest, least crash-prone Web browser available on the Mac. However, as time went on, Microsoft slowed down development on Internet Explorer for Mac. Other Web browsers began to join the crowd: OmniWeb, Mozilla, Camino (formerly known as Chimera), Opera, and other similarly strange-named programs. Soon Apple decided to join the party, with its own browser named Safari. With emphasis on speed and simplicity, Safari quickly eclipsed Internet Explorer as a favorite of Mac fans worldwide. In fact, more than a million people downloaded Safari within the first two weeks of its existence. 9.2.1. Opening Web SitesThe simplest thing you can do with any Web browser is, of course, load a Web site. Thanks to AppleScript, however, you can load multiple Web sites, automatically and without fuss, by commanding Safari. That way, you can use AppleScript to load all your favorite Web sites as you're eating breakfast and come back to find all your pages just waiting to be read. Here's how: tell application "Safari" activate open location "http://www.apple.com/" open location "http://www.apricot.com/" open location "http://www.banana.com/" end tell The open location command lets you tell Safari what sites to load; if you have multiple open location commands, Safari will load multiple pages for you. (As described on Sidebar 9.5, open location has a number of other Internet-related uses as well.) 9.2.1.1 Opening Web sites within the same windowThe trouble with your current script is that Safari loads each Web site in a separate windowa recipe for massive screen clutter. The problem gets even worse if you add extra open location commands to the script, since Safari will have to open even more windows to load your Web sites. Luckily, Safari has a helpful feature, called tabbed browsing, to cut down on this window overload. When you turn on tabbed browsing (in Safari Preferences Tabs), you get to open multiple Web sites within the same window, with each site getting its very own "tab" at the top of the window. That way, you can browse your favorite 10 Web sites without opening 10 separate windows.
Normally, you create additional tabs inside a window with the File New Tab command (-N). Then you type the URL of the page you want to open into the Address bar, and press Return to load the page. (By repeating this process, you can create additional tabs inside the same window.) There's another timesaving way to use tabs, though. If you choose Safari Preferences General and pick "Open links from applications: in a new tab in the current window," your existing script will not only open multiple Web sites for you, but the script will open all the Web sites inside the same window. That way, when you run the script in the morning, you can come back from breakfast to find all your favorite Web sites openbut with only a single Safari window on the screen.
9.2.2. Viewing a Site's CodeWhenever you type a URL in the Address bar and press Return, Safari goes out and fetches that site's HTML codethe instructions that tell your Web browser how to present the Web site onscreen. If you're a budding Web designer, this code (also called source) can help you understand how to build Web pages. For example, by looking at a Web site's HTML source, you can see precisely how the Web designer laid out the page. Sometimes, you'll even find hidden notes that the Web designer embedded in the code, to remind himself why he coded something a particular way. If you have an interest in learning HTML, you ought to spend at least a little time browsing Web pages' source. Once you've opened a page in Safari, you can examine the page's source by choosing View View Source (Option--V). The trouble is, the code is uneditable in Safari; you can page through the commands, but you can't experiment by changing them. That's where AppleScript comes in. If you open Safari's AppleScript dictionary and look at the Safari Suite, you'll notice that every Safari document has a source property. With this information in hand, you can write a script that copies a Web page's source to a new TextEdit document, where you can edit the code:
--Part 1: tell application "Safari" set pageSource to the source of the front document end tell --Part 2: tell application "TextEdit" activate make new document at the front set the text of the front document to pageSource end tell Here's how your new script works:
9.2.3. Viewing a Site Without FormattingThere's a time for blinking Web ads, animated corporate logos, and flashy multimedia Web pages, but there's also a time for good old text. For instance, if you're looking at a page on a small screen, all the graphics can leave you with no room to read the actual content. Therefore, when you just want to get some quick information, it can be more helpful to have just a Web site's text than to have all the visual distractions that come along with it. That's the logic behind the following script, at least. When you run the script, Safari extracts just the text from the front-most window, and places that text into a new TextEdit document. You'll end up with a simple, uncluttered windowno buttons, no links, and most importantly, no flashy banner ads: tell application "Safari" set textVersion to (the text of the front document) --Gets just the text end tell tell application "TextEdit" activate make new document at the front --Put the text in TextEdit set the text of the front document to textVersion end tell Once you've got a plain-text version of a Web site with this script, it's easier to use AppleScript with the site, too. You can run the script from Section 8.2.2.1, for example, to speak your text-only Web site out loud. Another option would be to use AppleScript to print out that text document for you, using the print the front document command directed at TextEdit. Then, on your train ride to work, you could catch up on the day's news without any of the annoying graphical fluff that usually gets printed.
9.2.4. Running AppleScripts from SafariScript Editor is a great place to run your scripts, but you probably spend a lot more time in Safari every day than you do in Script Editor. Luckily, Safari has several different ways to run AppleScriptssome straightforward and some totally hidden. Using these convenient tricks, you won't have to flip back to Script Editor every time you feel the proverbial "tug of the AppleScript." 9.2.4.1 Scripts that appear on Web pagesIf you're browsing AppleScript Web sites (like those mentioned on Section C.1), you can simply select the AppleScript code you see on a Web page and choose Safari Services Script Editor Run as AppleScript (Section 4.8.3). In the background, Script Editor runs your script, and displays any dialog boxes along the way. That's a heck of a lot faster than selecting the script on the Web page, copying it from Safari, pasting the code in Script Editor, and then running the script from there.
If you want to modify a script you see on a Web page, though, you have no choice but to copy the code into Script Editor (Safari won't let you edit the text on Web pages). And while that might sound tedious, Safari's Services menu helps you out here, too. All you need to do is select the AppleScript code on the Web page and then choose Safari Services Script Editor Make New AppleScript. That command copies the AppleScript code and then pastes it into a new Script Editor window for you automatically. From there, all you need to do is compile and run the script to test it on your Mac and then make any code tweaks as you see fit. If the script runs to your satisfaction, just save it in your favorite location (like the Library Scripts folder). 9.2.4.2 Mini-scripts in the Address barIf there's a quick script you want to whip upsay, a single-line display dialog commandyou can type it straight into Safari's Address bar, using this format: applescript://com.apple.scripteditor?action=new&script=yourScriptCommands In this special kind of URL, everything before yourScriptCommands is always the same. To substitute your own code, you simply replace yourScriptCommands with whatever commands you want, like this (type it all on one line): applescript://com.apple.scripteditor?action=new&script=display dialog "My oh my" When you press Return in Safari's Address bar, a new Script Editor opens with your commands all written out for you. All you have to do is click Run, since Safari doesn't do that automatically (Figure 9-4). Of course, that doesn't seem too earth-shattering. "Why don't I just type the commands directly into Script Editor?" you wonder. As it turns out, you can use URLs in this format on your own Web pages. If you happen to maintain an online journal (or Weblog), for example, you can place an AppleScript URL like this right on a page, so that any viewers can click the link and see your code in Script Editor. In other words, the biggest benefit of AppleScript URLs isn't that you can use them on your own computerit's that you can put them on the Internet so you can share your code with other people. If you don't feel like memorizing that complicated URL format, though, you can simply type applescript:// and press Enter to jump right to Script Editor from Safari. Better yet, type applescript:// and don't press Enterinstead, drag the globe icon from the Address bar to your Bookmarks bar. Safari asks you to name your new bookmark (call it something creative, like "Launch Script Editor"). From then on, you can simply click your bookmark in Safari to jump right to Script Editor.
9.2.4.3 Saved scripts in the Favorites barIf you've saved a script as an Application (Section 2.2.2), you can run it from Safari's Favorites bar with ease. Here's how:
Now, whenever you want to run your script from Safari, simply click the script's name in the Bookmarks bar (Figure 9-5, bottom).
|