applescript with Ruby
ふと昨日、itunesをRubyでいじりたいと思って、あえて、流行りのRubyCocoaではなく、applescriptをRubyでいじる方法を調べてみる。
Rubyクックブックには、applescriptというラッパーを使うみたいに書いてあったんだけど、どうもrb-appscriptのほうがいいかんじ。結構、付属のドキュメント(当然、英語だけど)がしっかりしているので助かる。
例えば、iTunesの現在再生している曲名を取得し表示するapplescriptは、こんな感じになるんだが,
tell application "iTunes" set cname to name of current track end tell display dialog cname
このrb-applescriptを使えば、こんな感じ。
require 'rubygems' require 'Appscript' it = Appscript.app 'iTunes' puts it.current_track.name.get => Another Time / Another Story
applescriptの構造がわかっていれば、どうやらそれをrubyっぽく直せば、動きそうなかんじ。
ちなみに、Appscript.app 'iTunes'でみえるメソッドはこんな感じ。
(it.methods - Object.methods).sort => ["AS_aem_reference", "AS_aem_reference=", "AS_app_data", "AS_app_data=", "AS_new_reference", "EQ", "EQ_enabled", "EQ_presets", "EQ_windows", "ID", "URL_tracks", "[]", "_aem_application_class", "_resolve_range_boundary", "_send_command", "abort_transaction", "activate", "add", "address", "after", "album", "album_artist", "and", "any", "application", "artist", "artworks", "audio_CD_playlists", "audio_CD_tracks", "back_track", "band_1", "band_10", "band_2", "band_3", "band_4", "band_5", "band_6", "band_7", "band_8", "band_9", "before", "bit_rate", "bookmark", "bookmarkable", "bounds", "bpm", "browser_windows", "capacity", "category", "class_", "close", "closeable", "collapseable", "collapsed", "collating", "commands", "comment", "compilation", "composer", "container", "contains", "convert", "copies", "count", "current_EQ_preset", "current_encoder", "current_playlist", "current_stream_URL", "current_stream_title", "current_track", "current_visual", "data", "database_ID", "date_added", "delete", "description", "device_playlists", "device_tracks", "disc_count", "disc_number", "does_not_contain", "does_not_end_with", "does_not_start_with", "download", "downloaded", "duplicate", "duration", "eject", "elements", "enabled", "encoders", "end", "end_transaction", "ending_page", "ends_with", "episode_ID", "episode_number", "eq", "error_handling", "exists", "fast_forward", "fax_number", "file_tracks", "finish", "first", "fixed_indexing", "folder_playlists", "format", "free_space", "frontmost", "full_screen", "gapless", "ge", "genre", "get", "grouping", "gt", "id_", "index", "is_in", "is_not_in", "items", "keywords", "kind", "last", "launch", "launch", "le", "library_playlists", "location", "long_description", "lt", "lyrics", "make", "method_missing", "middle", "minimized", "modifiable", "modification_date", "move", "mute", "ne", "next", "next_track", "not", "open", "open_location", "or", "pages_across", "pages_down", "parameters", "parent", "pause", "persistent_ID", "play", "played_count", "played_date", "player_position", "player_state", "playlist_windows", "playlists", "playpause", "podcast", "position", "preamp", "previous", "previous_track", "print", "print_settings", "printer_features", "properties", "quit", "radio_tuner_playlists", "rating", "refresh", "reopen", "requested_print_time", "resizable", "resume", "reveal", "rewind", "run", "sample_rate", "search", "season_number", "selection", "set", "shared", "shared_tracks", "show", "shufflable", "shuffle", "size", "skipped_count", "skipped_date", "smart", "song_repeat", "sort_album", "sort_album_artist", "sort_artist", "sort_composer", "sort_name", "sort_show", "sound_volume", "sources", "special_kind", "start", "start_", "start_transaction", "starting_page", "starts_with", "stop", "subscribe", "target_printer", "time", "track_count", "track_number", "tracks", "unplayed", "update", "updateAllPodcasts", "updatePodcast", "update_tracks", "user_playlists", "version", "video_kind", "view", "visible", "visual_size", "visuals", "visuals_enabled", "volume_adjustment", "windows", "year", "zoomable", "zoomed"]
ちまちま、いじっていこうと思います。
あと、いい加減、.irbrcに
require 'rubygems'
の一行を追加しました。これで、gemで落としたライブラリも迷わず、irbにrequireできる。大学時代に、gemのrequireではまって以来、gemはrailsを使わない僕には、鬼門です。