Search code examples
javaapplescript

Unable to create Apple Music user playlist within playlist folder using Java Applescript library wrapper com.tagtraum.obstmusic


Trying to use https://github.com/japlscript/obstmusic to talk to Apple Music app on Macos with Java, I used to write native applescript and then java applescript library but that was removed from Java.

In this case I cant work out to create a user playlist within a folder playlist, when I run the code

    public void createPlaylist() throws Exception
    {
        FolderPlaylist songkongPlaylistFolder = getPlayListFolder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm");
        String playlistname = "SongKong:" + sdf.format(new Date());
        Application app = Application.getInstance();

        Map<String, Reference> properties = new HashMap<>();
        Object userPlaylist = app.make(TypeClass.fromClass(UserPlaylist.class), songkongPlaylistFolder, properties);
        ((UserPlaylist)userPlaylist).setName(playlistname);
    }

I get exception

04/04/2022 14.52.44:BST:OSXUpdateItunesWithChanges:updateItunes:SEVERE: *** Unable to run itunes update:{ NSAppleScriptErrorAppName = Music;
NSAppleScriptErrorBriefMessage = "Can’t make some data into the expected type."; NSAppleScriptErrorMessage = "Music got an error: Can’t make some data into the expected type.";
NSAppleScriptErrorNumber = "-1700"; NSAppleScriptErrorRange = "NSRange: {23, 93}"; } com.tagtraum.japlscript.execution.JaplScriptException: {
NSAppleScriptErrorAppName = Music; NSAppleScriptErrorBriefMessage = "Can’t make some data into the expected type."; NSAppleScriptErrorMessage = "Music got an error: Can’t make some data into the expected type."; NSAppleScriptErrorNumber = "-1700";
NSAppleScriptErrorRange = "NSRange: {23, 93}"; } at com.tagtraum.japlscript.execution.CocoaScriptExecutor.execute(Native Method) at com.tagtraum.japlscript.execution.CocoaScriptExecutor.executeImpl(CocoaScriptExecutor.java:28) at com.tagtraum.japlscript.execution.ScriptExecutor.execute(ScriptExecutor.java:102) at com.tagtraum.japlscript.ObjectInvocationHandler.executeAppleScript(ObjectInvocationHandler.java:476) at com.tagtraum.japlscript.ObjectInvocationHandler.executeAppleScript(ObjectInvocationHandler.java:468) at com.tagtraum.japlscript.ObjectInvocationHandler.executeAppleScript(ObjectInvocationHandler.java:464) at com.tagtraum.japlscript.ObjectInvocationHandler.invokeCommand(ObjectInvocationHandler.java:280) at com.tagtraum.japlscript.ObjectInvocationHandler.invoke(ObjectInvocationHandler.java:191) at jdk.proxy2/jdk.proxy2.$Proxy59.make(Unknown Source) at com.jthink.songkong.ituneshelper.OSXUpdateMusicWithChanges.createPlaylist(OSXUpdateMusicWithChanges.java:62) at com.jthink.songkong.ituneshelper.OSXUpdateItunesWithChanges.analyseFiles(OSXUpdateItunesWithChanges.java:246) at com.jthink.songkong.ituneshelper.OSXUpdateItunesWithChanges.updateItunes(OSXUpdateItunesWithChanges.java:126) at com.jthink.songkong.ituneshelper.UpdateItunesWithChanges.call(UpdateItunesWithChanges.java:184) at com.jthink.songkong.ituneshelper.UpdateItunesWithChanges.call(UpdateItunesWithChanges.java:33) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)

I think the problem is how I call app.make() but I cant work out how to do it.


Solution

  • Found it buried quite away down in the README.md file of the base project https://github.com/japlscript/japlscript

    Reference reference = app.make(UserPlaylist.CLASS, songkongPlaylistFolder, null);
    UserPlaylist userPlaylist = reference.cast(UserPlaylist.class);
    userPlaylist.setName(playlistname);