From 58b18ee78f68311c3f3faa581b30aeca5195e450 Mon Sep 17 00:00:00 2001 From: Daniel Lazar Date: Sat, 20 Jul 2024 11:12:05 +0200 Subject: [PATCH] fix(apple-music): broken list items with comma When we had track, album or playlist items with a comma the regex used for creating table removed necessary parts of the string. This commit fixes this issue by using osascript's `-s` flag with a value `s` which returns a recompilable source form. The new type of return then can be processed into a table with `loadstring`. --- lua/apple-music/init.lua | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/lua/apple-music/init.lua b/lua/apple-music/init.lua index aa260d3..4d911b1 100644 --- a/lua/apple-music/init.lua +++ b/lua/apple-music/init.lua @@ -240,18 +240,15 @@ end ---Get a list of playlists from your Apple Music library ---@usage require('nvim-apple-music').get_playlists() M.get_playlists = function() - local command = [[osascript -e 'tell application "Music" to get name of playlists']] + local command = [[osascript -e 'tell application "Music" to get name of playlists' -s s]] local handle = io.popen(command) local result = handle:read("*a") handle:close() - -- Split the result into a table of playlist names - local playlists = {} - for playlist in result:gmatch("([^,]+)") do - playlist = playlist:match("^%s*(.-)%s*$") - table.insert(playlists, playlist) - end + -- Convert table string into table + local result_chunk = "return " .. result + local playlists = loadstring(result_chunk)() return playlists end @@ -295,16 +292,14 @@ end ---Get a list of albums from your Apple Music library ---@usage require('nvim-apple-music').get_albums() M.get_albums = function() - local command = [[osascript -e 'tell application "Music" to get album of every track']] + local command = [[osascript -e 'tell application "Music" to get album of every track' -s s]] local handle = io.popen(command) local result = handle:read("*a") handle:close() - -- Split the result into a table of album names - local albums = {} - for album in result:gmatch("([^,]+)") do - album = album:match("^%s*(.-)%s*$") - table.insert(albums, album) - end + + -- Convert table string into table + local result_chunk = "return " .. result + local albums = loadstring(result_chunk)() local unique_albums = remove_duplicates(albums) @@ -336,16 +331,15 @@ end ---Get a list of tracks from your Apple Music library ---@usage require('nvim-apple-music').get_tracks() M.get_tracks = function() - local command = [[osascript -e 'tell application "Music" to get name of every track']] + local command = [[osascript -e 'tell application "Music" to get name of every track' -s s]] local handle = io.popen(command) local result = handle:read("*a") handle:close() - -- Split the result into a table of album names - local tracks = {} - for track in result:gmatch("([^,]+)") do - track = track:match("^%s*(.-)%s*$") - table.insert(tracks, track) - end + + -- Convert table string into table + local result_chunk = "return " .. result + local tracks = loadstring(result_chunk)() + return tracks end