Module: Musicapp::Script
- Defined in:
- lib/musicapp/script.rb
Defined Under Namespace
Classes: ScriptError
Constant Summary collapse
- ITEM_PROPERTIES =
%w( name ).map(&:freeze).freeze
- TRACK_PROPERTIES =
%w( album albumArtist albumDisliked albumLoved albumRating albumRatingKind artist bitRate bookmark bookmarkable bpm category cloudStatus comment compilation composer databaseID dateAdded description discCount discNumber disliked downloaderAppleID downloaderName duration enabled episodeID episodeNumber eq finish gapless genre grouping kind longDescription loved lyrics mediaKind modificationDate movement movementCount movementNumber playedCount playedDate purchaserAppleID purchaserName rating ratingKind releaseDate sampleRate seasonNumber shufflable skippedCount skippedDate show sortAlbum sortArtist sortAlbumArtist sortName sortComposer sortShow size start time trackCount trackNumber unplayed volumeAdjustment work year ).map(&:freeze).freeze
- FILE_TRACK_PROPERTIES =
%w( location ).map(&:freeze).freeze
- FULL_PROPERTIES =
(ITEM_PROPERTIES + TRACK_PROPERTIES + FILE_TRACK_PROPERTIES).freeze
- DEFAULT_PROPERTIES =
%w(album albumArtist artist discCount discNumber name trackCount trackNumber location).map(&:freeze).freeze
- READONLY_PROPERTIES =
%w( albumRatingKind bitRate cloudStatus databaseID dateAdded downloaderAppleID downloaderName duration kind modificationDate purchaserAppleID purchaserName ratingKind releaseDate sampleRate size time ).map(&:freeze).freeze
- WRITABLE_PROPERTIES =
(FULL_PROPERTIES - READONLY_PROPERTIES).freeze
Class Method Summary collapse
- .get_metadata(properties) ⇒ Object
- .next_track ⇒ Object
- .osascript(script, *args) ⇒ Object
- .pause ⇒ Object
- .play ⇒ Object
- .set_metadata(metadata) ⇒ Object
Class Method Details
.get_metadata(properties) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/musicapp/script.rb', line 122 def (properties) properties = case properties when :all FULL_PROPERTIES when :default DEFAULT_PROPERTIES else invalid = properties - FULL_PROPERTIES unless invalid.empty? raise ::Musicapp::Error, "Unknown properties: #{invalid.inspect}" end properties end script = <<~JS function run(argv) { var itunes = Application("Music"); var selection = itunes.browserWindows[0].selection(); var trackProperties = JSON.parse(argv[0]); var tracks = []; for (var i in selection) { var props = {}; var track = selection[i]; for (var j in trackProperties) { var prop = trackProperties[j]; switch(prop) { case "location": if (track.class() != "fileTrack") continue; props.location = track.location().toString(); break; default: props[prop] = track[prop](); } } tracks.push(props); } return JSON.stringify(tracks); } JS out, _err, _status = osascript(script, properties.to_json) JSON.parse(out) end |
.next_track ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/musicapp/script.rb', line 227 def next_track script = <<~JS function run(argv) { var itunes = Application("Music"); itunes.nextTrack(); } JS out, _err, _status = osascript(script) out end |
.osascript(script, *args) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/musicapp/script.rb', line 113 def osascript(script, *args) out, err, status = Open3.capture3("osascript", "-l", "JavaScript", "-e", script, *args) unless status.success? raise ScriptError, err end [out, err, status] end |
.pause ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/musicapp/script.rb', line 215 def pause script = <<~JS function run(argv) { var itunes = Application("Music"); itunes.pause(); } JS out, _err, _status = osascript(script) out end |
.play ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/musicapp/script.rb', line 203 def play script = <<~JS function run(argv) { var itunes = Application("Music"); itunes.play(); } JS out, _err, _status = osascript(script) out end |
.set_metadata(metadata) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/musicapp/script.rb', line 171 def () script = <<~JS function run(argv) { var itunes = Application("Music"); var selection = itunes.browserWindows[0].selection(); var metadata = JSON.parse(argv[0]); var track = null; for (var i in selection) { track = metadata[i] if (!track) continue; for (var j in track) { var prop = track[j]; selection[i][j] = prop; } } } JS invalid = .flat_map do |track| track.keys - WRITABLE_PROPERTIES end unless invalid.empty? raise ::Musicapp::Error, "Unknown or readonly properties: #{invalid.inspect}" end out, _err, _status = osascript(script, .to_json) out end |