在苹果脚本中重命名文件

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

(我对代码了解一点,但在 AppleScript 中摸索)。

我有一个非常小众的问题。我有一堆(数千个)音乐文件是这样排列的

WAV 文件 -> 艺术家 -> 专辑 -> 曲目。所有曲目的命名如下:“[曲目编号] [艺术家姓名] - [曲目标题]”

我想快速从标题中删除艺术家姓名,因此格式为“[曲目编号]-[曲目名称]”

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

很想知道我哪里错了......

将祖父母设置为别名“Macintosh HD:Users:Michael:Music:Music WAV files” -- 设置顶级文件夹

将艺术家列表设置为“”

告诉应用程序“系统事件” 设置艺术家列表以获取祖父母的每个文件夹的名称 -- 创建目录中所有艺术家的列表 结束告诉

将total_artists设置为(获取artists_list中的项目数) -- 艺术家数量

--艺术家的第一轮循环作品 重复 i 从 1 到total_artists 将艺术家名称设置为艺术家列表的第 i 项

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

结束重复

file directory 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.