在applescript中重命名文件

问题描述 投票:0回答:1

我对代码有所了解,但正在摸索 AppleScript。

我有一个非常小众的问题。我有一堆(数千个)音乐文件,按如下方式排列:

Music WAV Files -> Artist -> Album -> tracks

曲目全部命名如下:

"[track no] [Artist name] - [Track Title]"

我想快速从标题中删除艺术家姓名,因此格式为

"[Track No]-[Track name]"

我已经设法弄清楚如何读取文件和文件夹列表,但我偶然发现了最后一点 - 重命名。

set grandparent to alias "Macintosh HD:Users:Michael:Music:Music WAV files"
-- set top level folder

set artists_list to ""

tell application "System Events"
    set artists_list to get the name of every folder of grandparent
    -- creates a list of all artists in directory
end tell

set total_artists to (get count of items in artists_list)
-- number of artists

-- first loop works through artists
repeat with i from 1 to total_artists
    set artist_name to item i of the artists_list
    
    log artist_name
    set next_folder to alias grandparent & artist_name as text
    set artist_dir to alias next_folder
    -- sets directory to artist
    log artist_dir
    
    set artist_albums to ""
    
    tell application "System Events"
        set artist_albums to get the name of every folder of artist_dir
        -- gets the list of artists albums
    end tell
    
    set total_albums to get count of items in artist_albums
    -- sets the total numbers of artists albums
    
    -- second loop goes into tracks
    
    repeat with j from 1 to total_albums
        set album_name to item j of artist_albums
        -- log album_name
        set next_folder to alias artist_dir & album_name as text
        set album_dir to alias next_folder
        -- sets directory to album
        -- log album_dir
        
        set tracks to ""
        
        tell application "System Events"
            set tracks to get the name of every item in album_dir
            log tracks
            -- gets list of tracks
        end tell
        
        set total_tracks to get count of item in tracks
        -- log total_tracks
        -- counter for tracks
        
        set track to ""
        set original_name to ""
        repeat with k from 1 to total_tracks
            set track to item k in tracks
            -- bypass folder.jpg and .ds store
            if track = ".DS_Store" then
            else
                if track = "folder.jpg" then
                else
                    
                    -- log track
                    set prefix to text 1 thru 3 of track
                    set suffix to text (offset of "-" in track) thru item -1 of track
                    --  log prefix
                    --  log suffix
                    set newname to prefix & suffix
                    log newname as text
                    -- set name of track to newname
                    set name of track to newname
                    
                end if
            end if
        end repeat
    end repeat
end repeat
applescript rename
1个回答
0
投票

选择包含所有 WAV 的文件夹(即所有艺术家文件夹)后,您只需循环遍历艺术家文件夹,并为每个文件夹获取所有文件(而不是文件夹)以从文件名中删除艺术家。这就是下面的脚本的作用:

-- Path: Wav / Artist / Album / tracks
-- track name:  number Artist title --> should become number title
      
set WavF to choose folder "Select WAV folder"
tell application "Finder"

set TheArtists to (every folder of folder WavF)

repeat with anArtistF in TheArtists
    set Artist to name of anArtistF & " " -- add a space at the end to remove it from title
    set mytracks to every file of entire contents of anArtistF
    repeat with atrack in mytracks
        set TrackName to name of atrack
        set AppleScript's text item delimiters to {Artist}
        set NameParts to text items of TrackName
        if (count of NameParts) = 2 then
            -- the name is made of 1 part before Artist and 1 part after
            -- the new name is part 1 and 2 (excuding Artist !)
            set name of atrack to item 1 of NameParts & item 2 of NameParts
        end if
    end repeat -- next track
end repeat -- next artist folder

end tell
© www.soinside.com 2019 - 2024. All rights reserved.