1
我想複製一個文件並將其移動到其他文件夾並重命名它。
但重複的文件沒有移動到該文件夾,也沒有重命名。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以「walk」和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」並將其移動並重新命名。
但該物品未移動。
重命名後不能移動項目嗎?
謝謝您的回答。 – noprops
謝謝你的回答。你的代碼比我的文件夾更快。但問題依然存在。請閱讀我編輯的評論。 – noprops