noprops
noprops

Reputation: 351

Applescript : I can't move duplicated file


I want to duplicate a file and move it to other folder and rename it.
But duplicated file was not moved to the folder and not renamed.

Here is my code.
"useskill" is folder.

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

I don't know where to I should fix because I'm new to Applescript.

I put all the code I wrote.

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

Any help would be appreciated.
Thank you.

EDIT
@adayzdone Thank you for your answer.
But if itemName starts with "walk" and frame = 1, duplicated copyItem replace the existing file because they have same name "0001.png" when duplicated.
So I rewrited some of the code, but it did not work well.

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

I intended to rename the item to"temp.png" and move it and rename it again.
But the item was not moved.
Is it impossible to move the item after rename?

Upvotes: 1

Views: 142

Answers (1)

adayzdone
adayzdone

Reputation: 11238

You can try something along these lines...

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

Upvotes: 1

Related Questions