Sunday, October 17, 2021

#1 2021-10-10 01:42:19 am

TheKrell
Member
Registered: 2020-03-08
Posts: 14

Importing file(s) to Music

There is something I don't understand re importing files to music w AppleScript:

Applescript:

tell application "Music"
   set toPlaylist to "NewTracks"    
   set newFile to (choose file with prompt "Select track you want to add...")
add newFile to playlist toPlaylist
end tell

The above works to to import one file into the playlist.

Then why doesn't the following work to import all the files in a chosen folder the same way?



Applescript:

set toPlaylist to "NewTracks"

tell application "Music"
   set file_list to every file of (choose folder with prompt "Select Folder to Import to Music")
   
   if not (user playlist toPlaylist exists) then
       make new user playlist with properties {name:toPlaylist}
   end if
   
   repeat with newFile in file_list
       add newFile to playlist toPlaylist
   end repeat
   
end tell


Filed under: music, folders, playlist, import

Offline

 

#2 2021-10-10 08:26:53 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 128

Re: Importing file(s) to Music

What does "doesn't work" mean?

Offline

 

#3 2021-10-10 09:00:58 am

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Importing file(s) to Music

Two things.

1) if the playlist already exist you might want to remove all of its tracks
It might not add them if they already exists.

2) log the file and log it’s class in each script.
You may need to get its posixPath or alias.

From another script I've found this:

Applescript:


set these_items to list folder this_folder without invisibles
   repeat with i from 1 to the count of these_items
       set this_item to alias ((this_folder as text) & ":" & (item i of these_items))

So you may not be getting a full path when you get the folder items, but a reference of that file only.  Which is why the above script creates a full path to each file

Last edited by technomorph (2021-10-10 09:17:45 am)

Offline

 

#4 2021-10-10 09:32:01 am

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Importing file(s) to Music

1st Script Add This:

Applescript:

log {"newFile is:", newFile}
   log {"newFile Class is:", class of newFile}

Output:

choose file with prompt "Select track you want to add..."
(*newFile is:, alias Kaptin:Users:kerry:Music:NML Tests:911 Is a Joke.mp3*)
(*newFile Class is:, alias*)


---------------------

Running Folder Script get this error:

iTunes got an error: Can’t get every file of alias "Kaptin:Users:kerry:Music:NML Tests:".

Then I changed it to this:

Applescript:


set file_list to list folder of (choose folder with prompt "Select Folder to Import to Music") without invisibles

and again it errors.  Here's the output of the same logging:

choose folder with prompt "Select Folder to Import to Music"
list folder alias "Kaptin:Users:kerry:Music:NML Tests:" without invisibles
list folder alias "Kaptin:Users:kerry:Music:NML Tests:" without invisibles


exists user playlist "NewTracks"
(*newFile is:, 911 Is a Joke.mp3*)
(*newFile Class is:, text*)
add "911 Is a Joke.mp3" to playlist "NewTracks"   ******* THIS ERRORS OUT



So yeah as it the other script you need to rebuild the complete file path.

Offline

 

#5 2021-10-11 10:55:21 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Importing file(s) to Music

Hi,

1) Choose folder command should be out of tell application "Music" block, because it is command of Standard Additions.

2) The files of folder doesn't exist for the Music.app until you add them to it. So, you should ask for the files of the folder the System Events, Finder, or the NSFileManager (AsObjC).

3) You can get directly the list of HFS paths instead of the files, to work with them later, inside the tell application "Music" block.

4) We should filter invisible files, like .DStore

5) we should avoid adding duplicates to the playlist

Applescript:


set toPlaylist to "NewTracks"

-- choose folder command out of tell application "Music" block
set aFolderHFS to (choose folder with prompt "Select Folder to Import to Music") as text

-- Get hfs paths list, using "System Events"
tell application "System Events" to ¬
   tell (files of folder aFolderHFS whose type identifier is "public.audio") to ¬
       set hfsPaths to path

tell application "Music"
   -- Create new playlist if it doesn't exist already
   if not (user playlist toPlaylist exists) then make new user playlist with properties {name:toPlaylist}
   -- Get names of songs, which is already in the playlist
   try
       set trackNames to name of tracks of playlist toPlaylist
   on error
       set trackNames to {}
   end try
   -- Add files one by one, deleting duplicates
   repeat with i from 1 to count hfsPaths
       set newTrack to add file (get item i of hfsPaths) to playlist toPlaylist
       if (name of newTrack) is in trackNames then delete newTrack
   end repeat
   return tracks of playlist toPlaylist
end tell

NOTE: maybe, exists better way to avoid adding duplicate songs than 1) adding song, 2) getting its name from the app, 3) deleting if it is duplicate.

Last edited by KniazidisR (2021-10-11 01:47:21 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-10-11 05:26:30 pm

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Importing file(s) to Music

KniazidisR wrote:



NOTE: maybe, exists better way to avoid adding duplicate songs than 1) adding song, 2) getting its name from the app, 3) deleting if it is duplicate.



Yeah unfortunately that’s a problem with adding tracks via AppleSceipt
If you import via the Apps menu and the file is already in your library, then
It won’t add it.

When you add via AppleScript it will add duplicates.
  It appends a number to the file name.

Offline

 

#7 2021-10-11 10:01:15 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Importing file(s) to Music

In general, it is strange that Music.app lacks an import command with checking for duplicates, similar to the one that Photos.app has. This is an obvious flaw, which I hope will be fixed in future versions of Music.app.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2021-10-14 07:36:08 pm

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Importing file(s) to Music

KniazidisR wrote:

In general, it is strange that Music.app lacks an import command with checking for duplicates, similar to the one that Photos.app has. This is an obvious flaw, which I hope will be fixed in future versions of Music.app.



Like I said it may do it if you import it from the Music app.
But not via scripting.

The issue with Songs / Files / Tracks is that there's no way to quickly identify a duplicate.
Also it will depend on you import settings preferences. There are two options that complicate things.

1) Copy Files to Music Library
2) Keep Music Files Organized

When you import a song in the iTunes / Music and copy Files is Set:

1) It will copy that file to your Music Folder/iTunes/iTunes Media/Music folder
2) It will process the file and pull out any meta data that is embedded in the file
3) if you have Keep Music Files Organized then it will apply changes to the file name and
change it to the Name of the Song and Organize it into a Folders based on the Artist / Album Artist and Album Name

So problems with this are if your importing file has different meta data, even if one character
is different it will after the file name and where it gets placed

I think the only way it won't add a new file is if the file your trying to add is already in it's Library database library file / XML file


It is possible to try to do this in your script.  But you would have to :

1) Extract the AVMeta data from the file using AVURLasset.
2) Be able to load the iTunes / Music XML file (or using ITLibrary Framework)
3) Do a bunch of filtering with NSPredicate's , compare methods to search the loaded library for
matching tracks. 
4) If found don't add the track.
5) But again there are some many other variables that could come into place even if it finds a matching already loaded track.
- if you filters and found same Track Name, Artist, Album
- your new tracks might be different length, different bitRate, different format, etc.
- your new tracks might be remastered and sound better etc.


Yes this all sucks big time.   I'm a DJ and a programmer and I've been trying to come up
with many methods to Prevent this at the start.   But more so the solutions arise from having
to do the DeDuplication after the fact.
- you may want to add a better / higher quality file thats a duplicate
- you may want to REPLACE all your old duplicate
- but what you want is the NEW MASTER to replace all the old dupe tracks in all of
the playlists that they were in.   
- you may want to transfer over ratings and other meta data from the old files
- many DJ software have CUEs, bpm and other custom data stored in the files and also
their own playlists as well.     

All of this is very complicated.  I've got the iTunes side worked out but still working on
a workflow for Traktor my DJ software.

here's an article that cover a lot, the site is amazing for scripts and scripting info :
https://dougscripts.com/itunes/itinfo/duplicates.php

The best tool I've found for iTunes is one of dougs:
https://dougscripts.com/apps/dupinapp.php

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)