Applescript:我无法移动重复的文件

时间:2014-05-24 00:26:47

标签: applescript


我想复制一个文件并将其移动到其他文件夹并重命名。
但是重复的文件没有移动到文件夹而没有重命名。

这是我的代码。
" useskill"是文件夹。

set copyItem to duplicate i
set name of copyItem to "temp.png"
move copyItem to useskill
set name of copyItem to "0001.png"

我不知道应该在哪里修复,因为我是Applescript的新手。

我把我写的所有代码都放了。

on adding folder items to this_folder after receiving added_items
    tell application "Finder"
        -- create folders if not exist
        if not (exists folder "asleep" of this_folder) then
            make new folder at this_folder with properties {name:"asleep"}
        end if
        set asleep to folder "asleep" of this_folder

        if not (exists folder "attack" of this_folder) then
            set attack to make new folder at this_folder with properties {name:"attack"}
            make new folder at attack with properties {name:"north"}
            make new folder at attack with properties {name:"northeast"}
            make new folder at attack with properties {name:"east"}
            make new folder at attack with properties {name:"southeast"}
            make new folder at attack with properties {name:"south"}
            make new folder at attack with properties {name:"southwest"}
            make new folder at attack with properties {name:"west"}
            make new folder at attack with properties {name:"northwest"}
        end if
        set attack to folder "attack" of this_folder

        if not (exists folder "useskill" of this_folder) then
            set useskill to make new folder at this_folder with properties {name:"useskill"}
        end if
        set useskill to folder "useskill" of this_folder

        if not (exists folder "walk" of this_folder) then
            set walk to make new folder at this_folder with properties {name:"walk"}
            make new folder at walk with properties {name:"north"}
            make new folder at walk with properties {name:"northeast"}
            make new folder at walk with properties {name:"east"}
            make new folder at walk with properties {name:"southeast"}
            make new folder at walk with properties {name:"south"}
            make new folder at walk with properties {name:"southwest"}
            make new folder at walk with properties {name:"west"}
            make new folder at walk with properties {name:"northwest"}
        end if
        set walk to folder "walk" of this_folder

        -- add items
        repeat with i in added_items
            set itemName to name of i
            --display dialog itemName
            if itemName ends with ".png" then
                if itemName starts with "asleep" then
                    --asleep
                    set name of i to "0001.png"
                    move i to asleep
                else if itemName starts with "attack" then
                    --attack
                    set oldDel to AppleScript's text item delimiters
                    set AppleScript's text item delimiters to "."
                    set myList to text items of itemName
                    set frame to item 2 of myList as integer
                    set dirNum to item 3 of myList as integer
                    set dirStr to my dirStr(dirNum)
                    --display dialog dirStr
                    move i to folder dirStr of attack
                    set name of i to ("000" & frame & ".png")
                    set AppleScript's text item delimiters to oldDel
                else if itemName starts with "walk" then
                    --walk
                    set oldDel to AppleScript's text item delimiters
                    set AppleScript's text item delimiters to "."
                    set myList to text items of itemName
                    set frame to item 2 of myList as integer
                    set dirNum to item 3 of myList as integer
                    set dirStr to my dirStr(dirNum)
                    --display dialog dirStr
                    move i to folder dirStr of walk
                    set name of i to ("000" & frame & ".png")
                    if frame = 1 then
                        set copyItem to duplicate i
                        set name of copyItem to "temp.png"
                        move copyItem to useskill
                        if dirNum = 0 then
                            set name of copyItem to "0001.png"
                            set copyItem2 to duplicate copyItem to useskill
                            set name of copyItem2 to "0009.png"
                        else if dirNum = 40 then
                            set name of copyItem to "0002.png"
                        else if dirNum = 80 then
                            set name of copyItem to "0003.png"
                        else if dirNum = 130 then
                            set name of copyItem to "0004.png"
                        else if dirNum = 180 then
                            set name of copyItem to "0005.png"
                        else if dirNum = 230 then
                            set name of copyItem to "0006.png"
                        else if dirNum = 280 then
                            set name of copyItem to "0007.png"
                        else if dirNum = 320 then
                            set name of copyItem to "0008.png"
                        end if
                    else if frame = 2 then
                        set copyItem to duplicate i
                        set name of copyItem to "0006.png"
                    else if frame = 3 then
                        set copyItem to duplicate i
                        set name of copyItem to "0005.png"
                    end if
                    set AppleScript's text item delimiters to oldDel
                end if
            end if
        end repeat
    end tell
end adding folder items to

on dirStr(dirNum)
    if dirNum is 0 then
        return "south"
    else if dirNum is 40 then
        return "southeast"
    else if dirNum is 80 then
        return "east"
    else if dirNum is 130 then
        return "northeast"
    else if dirNum is 180 then
        return "north"
    else if dirNum is 230 then
        return "northwest"
    else if dirNum is 280 then
        return "west"
    else if dirNum is 320 then
        return "southwest"
    end if
end dirStr

on retFileNameWithoutExt(fileNameStr)
    set fLen to length of fileNameStr
    set revText to (reverse of (characters of fileNameStr)) as string
    set anOffset to offset of "." in revText
    set fRes to text 1 thru (fLen - anOffset) of fileNameStr
    return fRes
end retFileNameWithoutExt

任何帮助将不胜感激 谢谢。

修改
@adayzdone感谢您的回答。
但是如果itemName以&#34开头,那么#34;和frame = 1,重复的copyItem替换现有文件,因为它们具有相同的名称" 0001.png"重复时。
所以我重写了一些代码,但是效果不好。

--walk
set {frame, dirNum} to my parseName(itemName)
set dirStr to my dirStr(dirNum)
set name of anItem to ("000" & frame & ".png")

if frame = 1 then
    (*set copyItem to duplicate anItem to ((this_folder as text) & "useskill") with replacing*)
    set copyItem to duplicate anItem
    set name of copyItem to "temp.png"
    move copyItem to ((this_folder as text) & "useskill") with replacing

    if dirNum = 0 then
        set name of copyItem to "0001.png"
        set copyItem2 to duplicate copyItem
        set name of copyItem2 to "0009.png"
    else if dirNum = 40 then ....

我打算将项目重命名为" temp.png"并移动它并重新命名。
但该项目未被移动。
重命名后是否无法移动项目?

1 个答案:

答案 0 :(得分:1)

你可以沿着这些方向尝试......

on adding folder items to this_folder after receiving added_items
    set this_folder_path to (quoted form of (POSIX path of this_folder))
    do shell script "mkdir -p " & this_folder_path & "{'asleep','useskill'}; mkdir -p " & this_folder_path & "{'attack','walk'}/{'north','northeast','east','southeast','south','southwest','west','northwest'};"

    repeat with anItem in added_items
        tell application "Finder"
            if anItem's name extension = "png" then
                set itemName to name of anItem

                if itemName starts with "asleep" then
                    set name of anItem to "0001.png"
                    move anItem to ((this_folder as text) & "asleep") with replacing

                else if itemName starts with "attack" then
                    set {frame, dirNum} to my parseName(itemName)
                    set dirStr to my dirStr(dirNum)
                    set name of anItem to ("000" & frame & ".png")

                    move anItem to ((this_folder as text) & "attack:" & dirStr) with replacing


                else if itemName starts with "walk" then
                    set {frame, dirNum} to my parseName(itemName)
                    set dirStr to my dirStr(dirNum)
                    set name of anItem to ("000" & frame & ".png")

                    if frame = 1 then
                        set copyItem to duplicate anItem to ((this_folder as text) & "useskill") with replacing

                        if dirNum = 0 then
                            set name of copyItem to "0001.png"
                            set copyItem2 to duplicate copyItem
                            set name of copyItem2 to "0009.png"
                        else if dirNum = 40 then
                            set name of copyItem to "0002.png"
                        else if dirNum = 80 then
                            set name of copyItem to "0003.png"
                        else if dirNum = 130 then
                            set name of copyItem to "0004.png"
                        else if dirNum = 180 then
                            set name of copyItem to "0005.png"
                        else if dirNum = 230 then
                            set name of copyItem to "0006.png"
                        else if dirNum = 280 then
                            set name of copyItem to "0007.png"
                        else if dirNum = 320 then
                            set name of copyItem to "0008.png"
                        end if
                    else if frame = 2 then
                        set copyItem to duplicate anItem
                        set name of copyItem to "0006.png"
                    else if frame = 3 then
                        set copyItem to duplicate anItem
                        set name of copyItem to "0005.png"
                    end if

                    move anItem to ((this_folder as text) & "walk:" & dirStr) with replacing
                end if
            end if
        end tell
    end repeat
end adding folder items to

on parseName(nameText)
    set {TID, text item delimiters} to {text item delimiters, "."}
    set f to text item 2 of nameText as integer
    set d to text item 3 of nameText as integer
    set text item delimiters to TID
    return {f, d}
end parseName

on dirStr(dirNum)
    if dirNum is 0 then
        return "south"
    else if dirNum is 40 then
        return "southeast"
    else if dirNum is 80 then
        return "east"
    else if dirNum is 130 then
        return "northeast"
    else if dirNum is 180 then
        return "north"
    else if dirNum is 230 then
        return "northwest"
    else if dirNum is 280 then
        return "west"
    else if dirNum is 320 then
        return "southwest"
    end if
end dirStr