AppleScript_ The Definitive Guide - Matt Neuburg [223]
#!/usr/bin/ruby
class Histogram
def initialize
@tally = Hash.new(0)
end
def analyze(s)
s.split.each do |word|
myword = word.downcase.gsub(/[^a-z0–9]/, "")
@tally[myword] = @tally[myword] + 1 if !myword.empty?
end
@tally.sort { |x,y| y[1]<=>x[1] }
end
end
analysis = Histogram.new.analyze(File.new(ARGV[0]).read)
counter = 1
oneline = ""
analysis[0..29].each do |entry|
oneline = oneline + "set the value of cell 1 of " +
"row #{counter.to_s} to \"#{entry[0]}\"\n"
oneline = oneline + "set the value of cell 2 of " +
"row #{counter.to_s} to #{entry[1].to_s}\n"
counter = counter + 1
end
script = < activate tell worksheet 1 #{oneline} end tell select range "A1:B30" set chartSheet to make new chart sheet at beginning of active workbook set c to active chart tell c set chart type to bar clustered set has title to true set caption of its chart title to "30 Most Frequent Words" set has legend to false apply data labels type data labels show label without legend key end tell repeat with axt in {category axis, series axis, value axis} repeat with ax in {primary axis, secondary axis} try tell (get axis c axis type axt which axis ax) set has major gridlines to false set has minor gridlines to false set has title to false end tell set has axis c axis type axt axis group ax without axis exists end try end repeat end repeat end tell DONE `osascript -ss -e '#{script}'` Figure 25-1. Excel data and chart generated by a Ruby script The script is called from the Terminal command line like this: % histogram.rb somefile.txt Chapter 26. Triggering Scripts Automatically There are a number of distinct automatic locations built into Tiger, and there are many varieties of circumstance in which third-party applications may trigger your scripts. A complete compendium of the ways in which scripts can be triggered automatically would be impossible, so the chief purpose of this chapter is to awaken you to the range of possibilities. You may be surprised by the sorts of role AppleScript can play on your computer. Preparing a script to operate in an automatic location is different from simply writing a script and running it. The reason is that you're not in charge. Some other process is—the process that will actually call your script—and you must know and obey the strictures imposed by that process. In general, you'll need to know two things: How to position your script Some arrangement will have to be made so that the calling process knows where and when to find your script. The script may have to be in a particular folder. It may have to have a particular name. You may have to make some sort of preparatory arrangement, explicitly informing the process beforehand that when a certain thing happens, a certain script should be called. What the caller will say to your script Your script might simply be executed (its run handler will be called, just as if you executed the script from a script editor application); but this is very often not the case. Rather, a particular event or handler call may be sent to your
What causes a script to run? It might be that you deliberately run it—you press the Run button in a script editor application, for example, or you choose the script from the menu of a script runner environment. However, there is a whole class of situations where your script just sits there patiently and eventually some other process comes along and runs it, with no direct intervention or action on your part. I call this sort of milieu an automatic location (see Chapter 2). In an automatic location, AppleScript takes a passive instead of an active part. Instead of you personally setting a chain of events in motion, AppleScript operates in response to other events or activities taking place on your computer: someone inserts an audio CD, someone moves a file into a certain folder, a certain web page request appears at your server, a certain time of day arrives, the computer wakes from sleep, a certain hard drive is mounted, the telephone rings.