Mit Apple Script (nicht Automator) MP3 Datei(en) umbenennen und ID3 Tags verwenden

W

wiseguy1

Mitglied
Thread Starter
Dabei seit
10.02.2010
Beiträge
19
Reaktionspunkte
0
Hallo,

ich möchte in meinem AppleScript (Ordneraktion) eine bzw. mehrere MP3 Dateien umbenennen. Dazu möchte ich die ID3 Tags der jeweiligen MP3 Datei verwenden.

Meine komplette Ordneraktion habe ich unten angefügt. Bisher klapt der Import nach iTunes, das grundsätzliche umbenennen danach und das Anzeigen der betroffenen Dateien, falls der Import doch nicht gewählt wurde.
Wie ich die ID3 Tags auslese um sie in Variablen für die Umbenennung zu speichern weiß ich leider nicht. Ich hätte ja gerne bei Doug's Apple Scripts nachgeschaut, wie man das macht. Aber die Scripts kann ich nur ausführen aber nicht editieren :cry:
Und dann stehe ich noch vor einem Problem: Lässt sich unkompliziert in dem Script die Ordneraktion für die umbenannte Datei deaktivieren? Schließlich werden vorhandene Dateien ja auch nicht (mehr) von der Ordneraktion erfast. Aber dafür hätte ich notfalls auch den Workaround, dass ich die Dateien in einen anderen Ordner verschiebe.

Nun aber mein Script

AppleScript:
(*
add - new item alert

Dieses Ordneraktion-Script wird immer dann ausgelöst, wenn dem angehängten Ordner Elemente hinzugefügt werden.
Das Skript zeigt eine Warnung mit der Anzahl der hinzugefügten Objekte an und bietet dem Benutzer
die Möglichkeit, die hinzugefügten Objekte in die Music App zu importieren.

Copyright © 2002–2007 Apple Inc.
Angepasst von wiseguy1

*)

-- die Zeitspanne in Sekunden festlegen, nach welcher Dialoge automatisch antworten
property dialog_timeout : 300

on adding folder items to this_folder after receiving added_items
    try
        -- den Namen des Ordners ermitteln
        tell application "Finder"
            set the folder_name to the name of this_folder
        end tell
        
        -- herausfinden, wie viele neue Objekte in den Ordner gelegt wurden
        set the item_count to the number of items in the added_items
        
        -- die angezeigte Meldung erstellen
        set alert_message to ("Meldung von Ordneraktion:" & return & return) as Unicode text
        if the item_count is greater than 1 then
            set alert_message to alert_message & (the item_count as text) & " neue Dateien liegen "
        else
            set alert_message to alert_message & "Eine neue Datei liegt "
        end if
        set alert_message to alert_message & "in dem Ordner " & «data utxt201C» & the folder_name & «data utxt201D» & "."
        set the alert_message to (the alert_message & return & return & "Soll automatisch nach \"Music\" importiert werden?")
        
        -- die Meldung anzeigen und auf Bestätigung warten (Automatische Wahl ist "Ja")
        display dialog the alert_message buttons {"Ja", "Nein"} default button 1 with icon 1 giving up after dialog_timeout
        set the user_choice to the button returned of the result
        
        if user_choice is "Ja" then
            -- "Ja" gedrückt: Den Import durchführen
            tell application "Music"
                add added_items
            end tell
            -- Anschließend die Originaldateien für die Webseite umbenennen
            tell application "Finder"
                repeat with one_item in added_items
                    -- Die folgenden Werte bräuchte ich aus den ID3 Tags
                    set id3_year to "Jahr aus ID3 Tag"
                    set id3_tracknr to "Titel Nummer aus ID3 Tag"
                    set id3_artist to "Künstler aus ID3 Tag"
                    set id3_titel to "Titel aus ID3 Tag"
                    -- Der Dateiname wird zusammen gebaut
                    set new_filename to id3_year & "-KW" & id3_tracknr & "_-_" & id3_artist & "_-_" & id3_titel
                    set name of one_item to new_filename
                end repeat
            end tell
        else
            -- "Nein" gedrückt: Stattdessen eine Meldung ausgeben und...
            set alert_message to "Der Import wurde nicht durchgeführt!" & return & return & "Hinweis:" & return & "Die Datei wird künftig nicht mehr beachtet. Die im Ordner liegenden Daten sollten überprüft werden."
            display dialog the alert_message buttons {"Ok"} default button 1 with icon 2 giving up after dialog_timeout
            -- ...Den Ordnerinhalt anzeigen
            tell application "Finder"
                --zum Desktop wechseln
                activate
                --Den Ordner öffnen
                open this_folder
                --Die betroffenen Dateien auswählen
                reveal the added_items
            end tell
            
        end if
    end try
end adding folder items to

ich würde mich sehr über Hilfe freuen.
 
Ich hab vor gefühlt 100 Jahren mal id3 Tags 1.1 und 2.x direkt per Apple Script ausgelesen.

Ich nutze das noch um Artist - Album und Year - Album umzubenennen.

hier das Script für den Ordnernamen umbenennen in Jahr-Album

( ich hab das so organisiert:
-a-/AC-DC/1974-Jailbreak/

etc

die Handler sind natürlich universell, kannst alles Mögliche rausholen.

Ich bitte um Teilung des Ergebnisses

(Heute würde ich davon Vieles eleganter und in Shell Script bzw mit CLI Tools machen, aber warum ändern was schon ewig läuft. Der meiste Teil stammt noch aus Zeiten von OS9)


AppleScript:
property seven_bit_enc_1 : (2 ^ 0)
property seven_bit_enc_2 : (2 ^ 7)
property seven_bit_enc_3 : (2 ^ 14)
property seven_bit_enc_4 : (2 ^ 21)


global raw_data



--
--    Template script for a droplet which processes folders
--

on ProcessAFolder(aFolder)
    
    tell application "Finder" to set folder_list to every folder of aFolder
    --display dialog ((count folder_list) as string) & " folders  in " & (aFolder as string)
    
    repeat with folder_found in folder_list
        --display dialog "folder found: " & folder_found as string
        ProcessAFolder(folder_found) -- recursive, for sub-directories
    end repeat
    
    
    try
        tell application "Finder" to set the_file to the 1st document file of aFolder whose name ends with ".mp3"
        --display dialog "1st document file:" & (the_file as string) & " in " & (aFolder as string)
    on error
        --display dialog "no mp3 in: " & aFolder as string
        return
    end try
    
    
    
    
    
    
    set tags to Get_tags(the_file as alias)
    set the_year to year of tags
    if the_year is "0" then set the_year to "????"
    set the_album to Album of tags
    set new_folder_name to the_year & "-" & the_album
    
    if new_folder_name is "-" then return
    
    -- os9... in x we have longer folder names... if (count characters of new_folder_name) is greater than 31 then set new_folder_name to characters 1 thru 31 of new_folder_name as string
    
    
    try
        tell application "Finder" to set name of aFolder to new_folder_name
    on error
        error new_folder_name
    end try
    
    
end ProcessAFolder

on open of folderList
    
    --display dialog "rename the folders to year-album ?"
    --
    --    Drag and drop handler.  Process each folder dropped on the script.
    --
    
    repeat with aFolder in folderList
        ProcessAFolder(contents of aFolder)
    end repeat
end open

on run
    
    --
    --    Run handler.  Prompt the user for a folder to process.
    --
    
    set theFolder to choose folder with prompt "Folder to rename to yyyy-album given by 1st .mp3 tune (including sub-folders !) ?"
    ProcessAFolder(theFolder)
end run




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

on Get_tags(the_file)
    
    
    
    if the_file = "none" then return ¬
        {kind:"none", title:¬
            "", who:¬
            "", Album:¬
            "", track:"", year:¬
            "", remarks:¬
            "", style:¬
            "", encoder:""}
    
    try
        close access the_file
    end try -- for restarting after error
    
    try
        
        tell application "Finder"
            set fref to open for access (the_file)
            set the_header to read fref for 10 -- try to read  id3v2x0 header
        end tell
        
        
        if the_header begins with "ID3" then -- ID3V2.x
            
            set a1 to (ASCII number (character 7 of the_header)) -- Size  7-bit 1
            set a2 to (ASCII number (character 8 of the_header)) -- Size  7-bit 2
            set a3 to (ASCII number (character 9 of the_header)) -- Size 7-bit 3
            set a4 to (ASCII number (character 10 of the_header)) -- Size 7-bit 4
            
            set tag_length to (a1 * seven_bit_enc_4) + (a2 * seven_bit_enc_3) + (a3 * seven_bit_enc_2) + (a4 * seven_bit_enc_1) -- 7bit encoding
            
            tell application "Finder"
                set raw_data to read fref from 11 for tag_length -- read the tags
                close access fref
            end tell
            
            if (ASCII number (character 4 of the_header)) is 2 then -- ID3v2.2.0
                set the_remarks to (get_id3v2_2_0_tag("COM"))
                if length of (the_remarks) is greater than 4 then
                    set the_remarks to characters 5 thru -1 of the_remarks
                else
                    set the_remarks to ""
                end if
                
                -- strip language code
                return ¬
                    {kind:"ID3 v2.2.0", title:get_id3v2_2_0_tag("TT2") ¬
                        , who:get_id3v2_2_0_tag("TP1") ¬
                        , Album:get_id3v2_2_0_tag("TAL") ¬
                        , track:get_id3v2_2_0_tag("TRK") ¬
                        , year:get_id3v2_2_0_tag("TYE") ¬
                        , remarks:the_remarks ¬
                        , style:get_id3v2_2_0_tag("TCO") ¬
                        , encoder:get_id3v2_2_0_tag("TEN")}
            end if -- if (character 4 of the_header) is (ASCII character 2)
            
            
            if (ASCII number (character 4 of the_header)) is 3 then -- ID3v2.3.0
                set the_remarks to (get_id3v2_3_0_tag("COMM"))
                if length of (the_remarks) is greater than 4 then
                    set the_remarks to characters 5 thru -1 of the_remarks
                else
                    set the_remarks to ""
                end if
                return ¬
                    {kind:"ID3 v2.3.0", title:get_id3v2_3_0_tag("TIT2") ¬
                        , who:get_id3v2_3_0_tag("TPE1") ¬
                        , Album:get_id3v2_3_0_tag("TALB") ¬
                        , track:get_id3v2_3_0_tag("TRCK") ¬
                        , year:get_id3v2_3_0_tag("TYER") ¬
                        , remarks:the_remarks ¬
                        , style:get_id3v2_3_0_tag("TCON") ¬
                        , encoder:get_id3v2_3_0_tag("TENC")}
            end if -- if (character 4 of the_header) is (ASCII character 3)
            
            
            
        end if -- if the_header begins with "ID3" -- id3v2x0 indication
        
        -------------------------------
        -- here are credits for IMAC-Girl     
        -- for inspiration and sample script
        -- for reading ID3v1 Tags
        -- iMacGirl@NikoCity.de
        -------------------------------
        
        tell application "Finder"
            try
                set fref to open for access the_file -- after error
            end try
            set id3v1_x_position to (get eof fref) - 127 -- there it has to be
            set is_tag to (read fref from id3v1_x_position for 3) = "TAG"
        end tell
        
        
        
        if (id3v1_x_position > 0) and (is_tag) then -- ID3v1.x
            tell application "Finder"
                set raw_data to read fref from id3v1_x_position + 3 for 125 -- read from tag to end
                close access fref
            end tell
            
            
            if (character -2 of raw_data) is (ASCII character 0) then
                -- id3 v1.0 has on eof-2 hex $00
                -- id3 v1.1 has on eof-2 hex tracknumber, but remarks is one byte shorter
                return ¬
                    {kind:"ID3 v1.0", title:de_code_with_nul(characters 1 thru 30 of raw_data) ¬
                        , who:de_code_with_nul(characters 31 thru 60 of raw_data) ¬
                        , Album:de_code_with_nul(characters 61 thru 90 of raw_data) ¬
                        , track:"", year:(characters 91 thru 94 of raw_data) as string ¬
                        , remarks:de_code_with_nul(characters 95 thru 124 of raw_data) ¬
                        , style:("(" & (ASCII number (character 125 of raw_data)) as string) & ")", encoder:""}
            else -- it is ID3 v1.1
                return ¬
                    {kind:"ID3 v1.1", title:de_code_with_nul(characters 1 thru 30 of raw_data) ¬
                        , who:de_code_with_nul(characters 31 thru 60 of raw_data) ¬
                        , Album:de_code_with_nul(characters 61 thru 90 of raw_data) ¬
                        , track:ASCII number (character 124 of raw_data), year:(characters 91 thru 94 of raw_data) as string ¬
                        , remarks:de_code_with_nul(characters 95 thru 123 of raw_data) ¬
                        , style:("(" & (ASCII number (character 125 of raw_data)) as string) & ")", encoder:""}
            end if --     if (character -2 of raw_data) is (ASCII character 0)
            
        end if -- if (id3v1_x_position > 0) and (is_tag)
        
        ------------------------------------------------------------------------------   
        
        -- no ID3 v1.0, v1.1, v2.2.0, v2.3.0 found
        
        tell application "Finder" to close access fref
        
        return ¬
            {kind:"none", title:¬
                "", who:¬
                "", Album:¬
                "", track:"", year:¬
                "", remarks:¬
                "", style:¬
                "", encoder:""}
        
        
        
        ------------------------------------------------------------------------------       
        
        
        
    on error errortext number errornumber
        
        display dialog errortext
        
        try
            tell application "Finder" to close access fref
        end try
        return ¬
            {kind:"error " & (errornumber) as string, title:¬
                "", who:¬
                "", Album:¬
                "", track:"", year:¬
                "", remarks:¬
                "", style:¬
                "", encoder:""}
        
    end try
    
    
    
end Get_tags

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

on get_id3v2_2_0_tag(the_tag)
    
    set tag_position to get offset of the_tag & {ASCII character 0, ASCII character 0} in raw_data
    -- coding {ID1, ID2, ID3, nul, nul, (length+1 of the text), nul, {the text}}
    
    if tag_position is 0 then return "" -- is it not in there
    
    set tag_length to (ASCII number (item (tag_position + 5) of raw_data)) - 2
    -- convert that character into an integer
    
    set tag_data to characters (tag_position + 7) thru (tag_position + 7 + tag_length) of raw_data
    -- from (x x x nul nul length-1 nul) to end of that text
    
    set tag_data to de_code_with_nul(tag_data)
    
    return tag_data as string
    
end get_id3v2_2_0_tag

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

on get_id3v2_3_0_tag(the_tag)
    
    set tag_position to get offset of the_tag & {ASCII character 0, ASCII character 0, ASCII character 0} in raw_data
    -- coding {ID1, ID2, ID3, ID4, nul, nul, nul, (length+1 of the text), nul, nul, nul, {the text}}
    
    if tag_position is 0 then return "" -- is it not in there
    
    set tag_length to (ASCII number (item (tag_position + 7) of raw_data)) - 2
    -- convert that character into an integer
    
    set tag_data to characters (tag_position + 11) thru (tag_position + 11 + tag_length) of raw_data
    -- from (x x x x nul nul nul length-1 nul nul nul) to end of that text
    
    set tag_data to de_code_with_nul(tag_data)
    
    return tag_data as string
    
end get_id3v2_3_0_tag



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

on de_code_with_nul(to_test)
    
    
    set good_list to "1234567890abcdefghijklmnopqrstuvwxyzäöüABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜß'.-, ()&="
    set replace_list to {{"à", "a"}, {"é", "e"}, {"í", "i"}, {"ó", "o"}, {"ú", "u"}, {"à", "a"}, {"è", "e"}, {"ì", "i"}, {"ò", "o"}, {"ù", "u"}, ¬
        {"‰", "ä"}, {"ˆ", "ö"}, {"¸", "ü"}, {"ƒ", "Ä"}, {"÷", "Ö"}, {"‹", "Ü"}, {"fl", "ß"}} as string -- list just for better reading...
    
    
    
    set cleaned_string to ""
    repeat with s in to_test
        if s is in good_list then
            set cleaned_string to cleaned_string & s
        else --try to replace
            if s is in replace_list then
                set the_position to get offset of s in replace_list
                set the_replacement to character (the_position + 1) of replace_list
                set cleaned_string to cleaned_string & the_replacement
            end if -- if s is in replace_list
        end if -- if s is in good_list
    end repeat
    
    
    try
        if last character of to_test is space then set to_test to (characters 1 thru -2 of to_test) as string -- delete last space if present
    end try
    
    return cleaned_string
    
end de_code_with_nul

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
 
  • Gefällt mir
Reaktionen: mausfang
Zurück
Oben Unten