Obstmusic
Obstmusic is a Java API for the Apple Music app (macOS) based on JaplScript.
Installation
Obstmusic is released via Maven. You can install it via the following dependency:
<dependency>
<groupId>com.tagtraum</groupId>
<artifactId>obstmusic</artifactId>
</dependency>
The Maven artifacts also contain sources and javadocs.
If you are using modules,
its name is tagtraum.obstmusic
.
Usage
To use the generated code, do something like this:
import com.tagtraum.macos.music.Application;
public class PlayPause {
public static void main(final String[] args) {
Application app = Application.getInstance();
// then use app, for example, to toggle playback (if a track is in the player)
app.playpause();
}
}
Or this:
import com.tagtraum.macos.music.Application;
import com.tagtraum.japlscript.Reference;
import com.tagtraum.japlscript.execution.JaplScriptException;
import com.tagtraum.japlscript.language.Tdta;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GetCurrentArtwork {
public static void main(final String[] args) throws IOException {
final Application application = Application.getInstance();
try {
final Track currentTrack = application.getCurrentTrack();
final Artwork[] artworks = currentTrack.getArtworks();
if (artworks.length > 0) {
final Artwork artwork = artworks[0];
final Reference rawData = artwork.getRawData();
if (rawData != null) {
// get image data
final Tdta tdta = rawData.cast(Tdta.class);
final byte[] imageData = tdta.getTdta();
// get image format
final String format = artwork.getFormat().getObjectReference().toLowerCase();
final String extension;
if (format.contains("png")) extension = ".png";
else if (format.contains("tiff")) extension = ".tiff";
else if (format.contains("gif")) extension = ".gif";
else if (format.contains("bmp")) extension = ".bmp";
else if (format.contains("pdf")) extension = ".pdf";
else extension = ".jpeg";
// write image file
final Path artworkPath = Paths.get("current_artwork" + extension);
System.out.println("Writing artwork to file " + artworkPath);
Files.write(artworkPath, imageData);
}
}
} catch (JaplScriptException e) {
System.out.println("No track loaded (we assume).");
}
}
}
Or this:
import com.tagtraum.macos.music.Application;
import com.tagtraum.macos.music.Track;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class AddArtworkToCurrentTrack {
public static void main(final String[] args) throws IOException {
// path to artwork should be the first argument
final Path newArtworkFile = Paths.get(args[0]);
final Application application = Application.getInstance();
// get currently playing track
final Track currentTrack = application.getCurrentTrack();
// add artwork
currentTrack.addArtwork(newArtworkFile);
}
}
API
You can find the complete API here.
Shipping
For information about shipping apps with this library, please see the corresponding notes about signing, notarization etc. in the JaplScript documentation.