Here is the “short old-fashioned” SLOW version.
use AppleScript version "2.4" # requires at least Yosemite
use scripting additions
use framework "Foundation"
script o
property targetFile : missing value
property filesMoved : 0
property filesNotMoved : 0
end script
# The main code
set theFolder to choose folder with prompt "Please select a folder containing MP3/AIFF files" # ADDED
set theFiles to its listFolder:(POSIX path of theFolder) # ADDED
set o's targetFile to ((path to desktop as text) & "report Mp3_3pMtroper.txt") as «class furl»
# Creates the file reporting files not moved
my writeto(o's targetFile, "", «class utf8», false)
repeat with aFile in theFiles
my treatThisFile(aFile)
end repeat
# Write the count of moved and not moved files in the text file "report Mp3_3pMtroper.txt"
my writeto(o's targetFile, linefeed & o's filesMoved & " were moved." & linefeed & o's filesNotMoved & " were not moved !", «class utf8», true)
#===== Below are the handlers
on treatThisFile(theFile)
set posixFilePath to the quoted form of (POSIX path of theFile)
set genreTag to (do shell script "mdls " & posixFilePath & " -name kMDItemMusicalGenre")
set artistTag to (do shell script "mdls " & posixFilePath & " -name kMDItemAuthors")
set tempoTag to (do shell script "mdls " & posixFilePath & " -name kMDItemTempo")
set keyTag to (do shell script "mdls " & posixFilePath & " -name kMDItemKeySignature")
set theGenre to text ((offset of "= " in genreTag) + 3) through -2 of genreTag
set theArtist to text ((offset of "= " in artistTag) + 9) through -4 of artistTag
set theTempo to text ((offset of "= " in tempoTag) + 2) through -1 of tempoTag
set theKey to text ((offset of "= " in keyTag) + 3) through -2 of keyTag
#SOME MP3 TAGS ARE NOT PROPERLY FILLED OUT- THIS BELOW PART DOESNT WORK AS SCRIPT PROCEEDS??????. # problem solved below
if ((count theGenre) ≤ (count "(null)") and theGenre contains "null") or ((count theArtist) ≤ (count "(null)") and theArtist contains "null") or ((count theTempo) ≤ (count "(null)") and theTempo contains "null") or ((count theKey) ≤ (count "(null)") and theKey contains "null") then
# The file can't be moved. Write it's path in the dedicated text file
my writeto(o's targetFile, theFile as text, «class utf8», true)
set o's filesNotMoved to (o's filesNotMoved) + 1
else
# There was no need to extract this info if one of the four other ones is null
set titleTag to (do shell script "mdls " & posixFilePath & " -name kMDItemTitle")
set theTitle to text ((offset of "= " in titleTag) + 3) through -2 of titleTag
# For some files, theTitle is returned as "null" or "(null)" so it may generate duplicates # Edited
# I choose to use the original name deprieved of the extension
if (count theTitle) ≤ (count "(null)") and theTitle contains "null" then # EDITED
tell application "System Events" # ADDED
set theTitle to text 1 thru -5 of (get name of file (theFile as text))
end tell
end if
set renamedTitle to ("{" & theTempo & "-" & theKey & "} " & theArtist & " - " & theTitle)
set DestFolder to ("/Volumes/Production/test/" & theGenre & "/" & theArtist)
--set DestFolder to ("/Volumes/Macintosh HD/Users/Important/Test/" & theGenre & "/" & theArtist)
my createFolder:DestFolder
set DestFolder to POSIX file DestFolder
set theFile to (theFile as text) as alias # ADDED. Finder refuse to get extension or rename the object returned by the ASObjC handler
tell application "Finder"
set theExtension to name extension of theFile
set name of theFile to renamedTitle & "." & theExtension #AS MAYBE AIFF OR MP3
try
move theFile to DestFolder
set o's filesMoved to (o's filesMoved) + 1
-- log ">>>>> " & theFile & linefeed & DestFolder
on error errMsg
-- display dialog renamedTitle & errMsg # IF ALREADY EXISTS WILL PROMPT
my writeto(o's targetFile, (theFile as text) & errMsg, «class utf8», true)
set o's filesNotMoved to (o's filesNotMoved) + 1
end try
end tell
end if # (moveitGenre is false) or.
end treatThisFile
#=====
on listFolder:POSIXPath # handler using ASObjC
set fileManager to current application's NSFileManager's defaultManager()
set aURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
set theOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set theEnumerator to fileManager's enumeratorAtURL:aURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value)
set theFiles to theEnumerator's allObjects()
set theFiles to theFiles's filteredArrayUsingPredicate:(current application's NSPredicate's predicateWithFormat:"(lastPathComponent ENDSWITH '.mp3') OR (lastPathComponent ENDSWITH '.aiff')")
return theFiles as list # if we work upon files
--return (theFiles's valueForKey:"path") as list # if we work upon POSIX paths
end listFolder:
-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolder:POSIXPath
set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
set theFileManager to |⌘|'s NSFileManager's defaultManager()
set {theResult, theError} to theFileManager's createDirectoryAtURL:theFolderURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
end createFolder:
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
--set targetFile to targetFile as «class furl» # no need, the calling instructions pass a «class furl» object
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write (theData & linefeed) to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
I hope that I didn’t forgot a change to match your settings after testing it with mines.
Added code writing the count of moved and the count of not-moved files in the file : “report Mp3_3pMtroper.txt”
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) jeudi 20 avril 2017 19:49:29