(Automator / AppleScript)将文件重命名为文件夹名称,保存到不同的文件夹&添加前缀

时间:2014-12-18 10:28:52

标签: macos applescript automator

我真的需要你的帮助,我将非常感激。 我认为这对你来说应该没问题。

所以我的文件夹中包含不同的子文件夹。在子文件夹中是图像。 我需要的是:

  1. 将每个图像名称更改为其子文件夹名称+ 001,依此类推(«1stSubfolder001,1stSubfolder002,...»,«2ndSubfolder001,2ndSubfolder002,...»)
  2. 将所有子文件夹中的所有图像移动到一个文件夹(或至少移动到根文件夹)
  3. 为名称添加随机前缀号码。
  4. 我有第三个任务的脚本:

    tell application "Finder" 
        repeat with this_item in (get items of window 1)
            set name of this_item to ((random number from 0 to 99999) & name of this_item) as string
        end repeat 
    end tell
    

    但它只适用于前台打开的文件夹。也许有办法让一个脚本在后台运行它?

    非常感谢你提前。

    我在这里找到了很酷的自动贩卖应用程序,但我需要稍微纠正一下。 How do I use the current folder name without a path as a variable in automator in Mac?

    这个脚本可以完成我需要的一切但是它将所有文件重命名为一个文件夹名称,而不是每个文件都不同。

    这是另一个将文件重命名为文件夹的AppleScript,但它使用2个文件夹名称(文件夹+子文件夹),我只需要子文件夹前缀。

    set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
    set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
    repeat with aFile in myFiles
        tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
    end repeat  
    

    (MacBook Pro Late 2013,OS X Yosemite 10.10.1)

1 个答案:

答案 0 :(得分:1)

这可以实现你想要的。你应该可以从这里调整。没有检查可以忽略非图像文件。

property top_folder : alias "Macintosh HD:Users:MyName:Downloads:Images:"
property save_folder : ""

set save_folder to choose folder with prompt "Select the folder to save the images in."
process_folder(top_folder)

on process_folder(this_folder)
    set these_items to list folder this_folder without invisibles
    set container_name to name of (info for this_folder)
    repeat with i from 1 to the count of these_items
        set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
        if folder of (info for this_item) is true then
            process_folder(this_item)
        else
            process_item(this_item, container_name, i)
        end if
    end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item, c, i)
    -- make the integer a 3 digit string
    if i < 10 then
        set i to "00" & i
    else if i < 100 then
        set i to "0" & i
    end if
    -- set a random number
    set r to (random number from 0 to 99999) as string

    tell application "System Events"
        -- get file extension so not overwritten
        set e to name extension of this_item
        set new_name to "" & r & "_" & c & "_" & i & "." & e
        set name of this_item to new_name
        move this_item to save_folder
    end tell
end process_item