I just thought I'd share some code from a script I worked on. The script takes lines of AppleScript code and tells the RVM to tell OSAScript to tell the app to run the code.
The script is different from RubyOSA because the main class, App, only has methods for running the AppleScript code that is passed to them. The carrying out of specific commands for each app is supposed to be taken care of by subclassing App and defining methods that send AppleScript code either to executeCommand or executeCommands. This design allows programmers to add support for other apps.
Below is the full script: (I a few subclasses of App, each with their own)
The script is different from RubyOSA because the main class, App, only has methods for running the AppleScript code that is passed to them. The carrying out of specific commands for each app is supposed to be taken care of by subclassing App and defining methods that send AppleScript code either to executeCommand or executeCommands. This design allows programmers to add support for other apps.
Below is the full script: (I a few subclasses of App, each with their own)
Code:
class App
def self.executeCommands(commands)
shellCommand = "osascript"
commands.unshift("tell application \"#{self.name}\"")
commands.each{|command|
shellCommand.gsub!(/$/," -e '#{command}'")
}
shellCommand.gsub!(/$/," -e 'end tell'")
pipe = IO.popen(shellCommand)
puts pipe.gets
end
def self.executeCommand(command)
self.executeCommands([command])
end
def self.open
self.executeCommand("open")
end
end
class Safari < App
def self.openURLInNewTab(url)
self.executeCommand("open location \"#{url}\"")
end
def self.openURLInCurrentTab(url)
self.executeCommand("set the URL of the front document to \"#{url}\"")
end
end
class Itunes < App
def self.playCurrentTrack
self.executeCommand("play")
end
def self.setSoundVolume(volume)
self.executeCommand("set sound volume to #{volume}")
end
def self.enableEqualizer
self.executeCommand("set EQ enabled to true")
end
def self.playSongsInPlaylist(playlist)
self.executeCommands(["set theSongs to every track of library playlist #{playlist}",
"repeat with aSong in theSongs", "play aSong", "end repeat"])
end
def self.playPlaylistSongsByArtist(playlist,artist)
self.executeCommands(["set theSongs to every track of library playlist #{playlist} whose artist is \"#{artist}\"",
"repeat with aSong in theSongs", "play aSong", "end repeat"])
end
end