Applescript to read mp3 metatags and sort file into folder

Oops

One instruction was missing.
I added it.

I’m back while the Wifi Hotspot is active.

The script using ASObjC doesn’t use the handler Writeto.
It rely upon ASObjC instructions which were available in the script.

	
	set theCounts to (|⌘|'s NSString's stringWithString:(linefeed & filesMoved & " were moved." & linefeed & filesNotMoved & " were not moved !"))
	(newArray's addObject:theCounts)
	set outDoc to (newArray's componentsJoinedByString:linefeed)
	set outPath to (path to desktop as text) & "report Mp3_3pMtroper.txt"
	set outPath to (|⌘|'s NSString's stringWithString:(POSIX path of outPath))
	set theResult to outDoc's writeToFile:outPath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

Due to the missing instruction, the counter was not incremented so your “report Mp3_3pMtroper.txt” file was displaying something like :

0 were moved.
xx were not moved !

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 25 avril 2017 14:42:33

It seems now that it doesn’t create the txt file. I have added the missing instruction down the bottom of mainbusiness??

Yet the script still moves the file to the destination. I also tried taking away the defined properties (targetfile, filesmoved, filesnotmoved) but it does the same thing.

the other issue I get is when I select a folder that has “multiple” files within, I get a error at line:


		-- stop and get the first NSMetadataItem (there can only be one in this case)
		theQuery's stopQuery()
		set metaItem to (theQuery's resultAtIndex:0)

Error


error "*** -[NSMetadataQuery resultAtIndex:]: index (0) out of bounds (0)" number -10000

When I apply the script of message #53 on a folder containing 15 mp3 files,
as I posted in my late message, 11 are moved, 4 aren’t.

I compared the content of your last message to the one in message #53.

Below is the list of oddities available:

# Prepare the counters of treated files # YOU DROPPED IT
set filesMoved to 0 # YOU DROPPED IT
set filesNotMoved to 0 # YOU DROPPED IT

# You have.
set my destinationRootPath to "/Volumes/Production/test6" --YOU NEED TO CHANGE DESTINATION PATH 
# the comment is incorrect since several days. 
# The instruction is designed to define the path used on MY machine.
# it MUST be:
set my destinationRootPath to "/Volumes/Macintosh HD/Users/Important/Test/" --"/Volumes/Production/test3"

set filesNotMoved to filesNotMoved + 1 # YOU DROPPED IT

# The comment to the instruction below is WRONG. It's not mine. The variable theTitle is not set to the file name !!!!
set theTitle to (metaItem's valueForAttribute:(|⌘|'s NSMetadataItemTitleKey)) --SET THE NEW FILE NAME


set filesMoved to filesMoved + 1 # YOU DROPPED IT


set filesMoved to filesMoved + 1 # ADDED on 2017/04/25 -- YOU DROPPED IT

--if (newArray's |count|()) as integer > 0 then # WAS REMOVED WHEN I ADDED THE COUNTERS


-- end if # WAS REMOVED WHEN I ADDED THE COUNTERS


Please use the script in message #53 without changing anything except the path used on your machine defined by the instruction :

property destinationRootPath : “/Volumes/Production/test/” # I assume that it’s the one you need # Is silently changed when the script run on my machine

If you want to work upon Test6, it’s this instruction and only this one which must be edited.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) jeudi 27 avril 2017 11:56:01

ok, I ran the script from #53 WITHOUT editing anything. It worked for one folder! which was the first time I ran the script.

However the second time I ran the script I selected another folder, and got the same error again.


error "*** -[NSMetadataQuery resultAtIndex:]: index (0) out of bounds (0)" number -10000

At line:
– stop and get the first NSMetadataItem (there can only be one in this case)
theQuery’s stopQuery()
set metaItem to (theQuery’s resultAtIndex:0) <<<<got error at this line.

ok now it didn’t error this time, but only moved one file? which is strange.

The source folder is located on my desktop not on the Production drive- but that shouldn’t matter.

Also the source folder is a already sorted mp3/aiff folder structure with the genre>artist from the script. Inside some artist folders are mp3s, so It should find them and move them, but instead throws this error.

When the script moved only one file, what is reported in the text file ?
You are supposed to see the list of the files which were not moved.
More,the script is carefully designed so that the history displayed in the dedicated window is supposed to report something like :
[format]
(Don’t move : /Users/???/Desktop/111 autre/Ah ! Vous dirai-je maman.mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = false, moveItKey = false, moveItTempo = false
)
(Don’t move : /Users/???/Desktop/111 autre/Aldo Romano, Louis Sclavis, Henri Texier - live 2005 - 3-3.mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = false, moveItKey = false, moveItTempo = false
)
(Don’t move : /Users/???/Desktop/111 autre/Ancient_Beijing.mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = true, moveItKey = false, moveItTempo = false
)
(Don’t move : /Users/???/Desktop/111 autre/Anne Sylvestre - Une sorcière comme les autres .mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = false, moveItKey = false, moveItTempo = false
)
(Don’t move : /Users/???/Desktop/111 autre/Bach_Two_Part_Invention_in_C_Major.mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = false, moveItKey = false, moveItTempo = false
)
(Don’t move : /Users/???/Desktop/111 autre/Barbara (1962) les boutons dorés.mp3
because at least one of these flags is false → moveItArtist = false, moveItGenre = false, moveItKey = false, moveItTempo = false
)
[/format]

According to what you asked, a file is not moved if at least ONE of the 4 flags is set to false because the related metadata is unavailable.

No idea about what you get.

With the folder “1dossier” I got:
[format]/Users/???/Desktop/1 dossier/Anne Sylvestre - Mes amis d’autrefois (Live).mp3
/Users/???/Desktop/1 dossier/Anne Sylvestre en studio _ Des calamars à l’harmonica 2.mp3
/Users/???/Desktop/1 dossier/Anne Sylvestre en studio _ Des calamars à l’harmonica.mp3
/Users/???/Desktop/1 dossier/Jolie bouteille Sacrée bouteille.mp3
/Users/???/Desktop/1 dossier/Mouloudji chante Boris Vian le politique.mp3
/Users/???/Desktop/1 dossier/MOUSTAKI Maman Papa.mp4.mp3
/Users/???/Desktop/1 dossier/Romano-Sclavis-Texier - live 2005 - 33.mp3
/Users/???/Desktop/1 dossier/Serge Reggiani - Ma fille (Lyrics).mp3
/Users/???/Desktop/1 dossier/Serge Reggiani -Le Petit Garçon.mp3
/Users/???/Desktop/1 dossier/Serge Reggiani Le petit garçon.mp3
/Users/???/Desktop/1 dossier/Supplique pour être enterré sur une plage de Sète.mp3
/Users/???/Desktop/1 dossier/The Future of Jazz_ Billy Taylor_George Russell_Bill Evans.mp3
/Users/???/Desktop/1 dossier/Un été.mp3

11 were moved.
13 were not moved ![/format]

With the folder “111 autre” containing 89 files in 4 subfolders I got :

[format]
89 were moved.
0 were not moved ![/format]

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) jeudi 27 avril 2017 16:25:04

ok I’ve uploaded a screen capture video showing the issue. You will get a idea of the source folder structure and files I’m trying to move.

https://youtu.be/z75mEF-ivUY

I just tried a artist folder from within that “Deep House” folder and it worked fine. Got the text file with the following text.

2 were moved.
0 were not moved !

However when I get this error, it doesn’t create a text file.

I apologize but your video doesn’t help.
With my 73 years old eyes I’m quite unable to read what is displayed.

I’m always unable to guess what fails on your machine.
I made new tests with a folder containing 21 folders and didn’t got the described error.

It would have been more useful to reproduce what is displayed in the history window.

I replaced the content of message #53 by an edited version.
This time it write infos about not saved file one by one like the old fashioned script.
I inserted a lot of log instructions.
When you will get the infamous error, looking in the history window will tell you which is the last instruction correctly executed.
If you pass this info it would be easier to try to understand what failed.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) samedi 29 avril 2017 18:14:03

ok I ran the script from #53
Got the error again, seems to be stopping at point 27. A text file was created on the desktop but nothing in it.

This is from log history window - replies -


tell application "Script Editor"
	choose folder with prompt "Please select folder"
		--> alias "Macintosh HD:Users:mattsquires:Desktop:SOURCE:Deep House:"
end tell
(*point 000*)
tell current application
	path to desktop as text
		--> "Macintosh HD:Users:mattsquires:Desktop:"
	(*point 010*)
	path to home folder as text
		--> "Macintosh HD:Users:mattsquires:"
	(*point 014*)
	(*point 015*)
	(*point 016*)
	(*point 017*)
	(*point 018*)
	(*point 019*)
	(*point 020*)
	(*point 021*)
	(*point 022*)
	(*point 023*)
	(*point 024*)
	(*point 025*)
	(*point 026*)
	(*point 027*)
Result:
error "*** -[NSMetadataQuery resultAtIndex:]: index (0) out of bounds (0)" number -10000


ok I’ve found the issue, it was a mp3 file with some odd characters in its names.

Once I took that out of the folder it worked fine.

When the script sorted it previously, it put some weird stuff in the title.

I’ve uploaded the original and sorted mp3 here, so you can try & see why it would stop the script from working.

https://www.dropbox.com/sh/twxdfvgxm4emi9y/AABLch6GBfOQodiNJNQK5dt7a?dl=0

The sorted problematic file name was:

{125-Bbm} A.Squared \U00bb download-techno.com - Third Eye (Original Mix) » download-techno.com.mp3

Is there anyway to prevent this? Without having to go through hundreds looking to change any long filenames.

Would be fine to report which is the last point logged when you treat the infamous file.
This would help to know where a change would be welcome.
According to the given string, I may know that
theTempo is 125
theKey is Bbm
theArtist is A.Squared \U00bb download-techno.com
theTitle is Third Eye (Original Mix) » download-techno.com

It appears that theArtist isn’t encoded the same way than theTitle.
Alas, it’s not the encoding which I encountered on my side, encoding treated by the handler

# Called with sourceString defined as NSString
on decodeText(theText) # Handler written by Nigel GARVEY
   set |⌘| to current application
   --set theText to |⌘|'s class "NSString"'s stringWithString:(theText)
   -- If the string contains at least two consecutive 8+bit characters, assume it's a mangled result.
   if ((theText's rangeOfString:("[\\u0080-\\U0010ffff]{2}") options:(|⌘|'s NSRegularExpressionSearch))'s |length|() > 0) then
       set dataObj to (theText's dataUsingEncoding:(|⌘|'s NSISOLatin1StringEncoding))
       set theText to (|⌘|'s class "NSString"'s alloc()'s initWithData:(dataObj) encoding:(|⌘|'s NSUTF8StringEncoding))
   end if
   return theText as text
end decodeText

Alas I’m unable to create a general modified version of this handler.
If Nigel GARVEY see this thread maybe he would build such handler.
At this time I created one able to treat two characters

on decodeText2(theText) # Temporary transcoder handler
	set theText to current application's class "NSString"'s stringWithString:(theText)
	set theText to my replaceSeveralStrings:theText existingStrings:{"\\U00ab", "\\U00bb"} newStrings:{"«", "»"}
	return theText
end decodeText2

I inserted the enhanced script in message #53.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) dimanche 30 avril 2017 18:02:44

Browser: Safari 601.7.7
Operating System: Mac OS X (10.10)

I enhanced the handler decodeText2

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) lundi 1 mai 2017 14:12:13

Hi Yvan.

I only noticed this earlier this evening just before I had to go out.

I don’t understand the problem it’s supposed to cure. The MP3 in squigee90’s “Original” folder has these metadata, as noted by my own ‘log’ commands in the post #53 script:

Even with decodeText2() switched out, the script relocates the file as “{125-Bbm} A.Squared » download-techno.com - Third Eye (Original Mix) » download-techno.com.mp3” in a folder called “A.Squared » download-techno.com in a folder called “Deep House” in the destination folder set near the top of the code. It does not “put some weird stuff in the title” and change the first chevron to “\U00bb”.

There’s no file in squigee90’s “Problem” folder. However, renaming the good file in the manner described and putting it back does lead to the error when the script tries to access the metadata query’s resultAtIndex:0. Basically, the query returns an empty array. The strange file name stops it from returning any results at all, so there’s no point in having a special handler to massage the Artist result.

I don’t know why the file name causes the metadata query to fail. When the \U00bb is manually changed back to », everything works again ” on my system, anyway.

Thanks Nigel.

I get the same result than you but my understanding is that the problem is related to a locale setting on the asker’s machine.

I got the same kind of problem with a file when I exchanged with Shane about one of the files which I used for tests.

With a bit of luck, the asker will tell us which are his local settings.

I’m running in french.
On his side, Shane was running English but I don’t know which one. As you know Apple define several variants of English.
When it will be more logical to be awake I will try to retrieve which is the file which behaved differently for Shane and for me.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 2 mai 2017 02:07:09

I think that I am on the track.

In the files which I had to treat during these weeks, the metadatas are encoded in Utf8. They use one byte for a character. Some characters are using 2 or more bytes.
When we extract the author,we need to use the decoder handler designed by Nigel.
No need for that for the other extracted values.

In the asker’s file, the metadatas are encoded in Utf16 with the BOM FF FE.
In thisfile, there is a character “»” in the author string. Here with the french system it’s perfectly treated.
If I understand well it’s also correctly treated on Nigel’s English system.
The infos given by the asker let me think that the version of macOS which he use fails to treat this character encoded in Utf16.

Interesting enough, exactly as it does here, the system doesn’t treat the same way the author and the title so, the title is correctly deciphered but the author isn’t.

I’m curious to read what is the behavior of the old fashioned script - using do shell script - when it’s asked to treat this infamous file.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 2 mai 2017 16:44:53

I think the \U00bb comes from using the shell script version of the script:


-- With squigee90's file selected in the Finder:
tell application "Finder" to set selectedItem to beginning of (selection as alias list)

do shell script "mdls  -raw -name kMDItemAuthors " & quoted form of POSIX path of selectedItem
(*
--> "(
    \"A.Squared \\U00bb download-techno.com\"
)"
*)

do shell script "mdls  -raw -name kMDItemTitle " & quoted form of POSIX path of selectedItem
--> "Third Eye (Original Mix) » download-techno.com"

With the ASObjC version, it doesn’t happen.

Sure, the shell version behaves this way but I missed that the asker gave the history with the logged “point xx” which I inserted only in the ASObjC version.

It fails to execute the instruction :
set metaItem to (theQuery’s resultAtIndex:0)

Introducing a new filter change nothing to this problem.
I will remove it and will try to intercept the error.
I wrote “try” because as I can’t reproduce the problem I’m in the fog.

The script in message #53 is edited accordingly.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mercredi 3 mai 2017 10:54:57

Hi Yvan.

There are two different problems here, one caused by the other.

  1. The OP’s problem file is one on which he’d run the script previously:

I think the script he used was one based on the mdls shell script. It renamed the file {125-Bbm} A.Squared \U00bb download-techno.com - Third Eye (Original Mix) » download-techno.com.mp3.

  1. When the file has \U00bb in its name, the metadata query in the ASObjC script, for unknown reasons, doesn’t return any results for it, causing resultAtIndex:0 to error:

The cures are to correct the problem file’s name manually to {125-Bbm} A.Squared » download-techno.com - Third Eye (Original Mix) » download-techno.com.mp3 (its metadata are OK) and never to use the mdls script again but the ASObjC one.

OK. I didn’t understood the situation this way.
Described this way, there is no problem.
I will wait for confirmation from the asker before cleaning the script.
In fact I will be unable to do that before 2017/05/12.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mercredi 3 mai 2017 14:21:24