2014-05-24 131 views
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」並將其移動並重新命名。
但該物品未移動。
重命名後不能移動項目嗎?

回答

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 
+0

謝謝您的回答。 – noprops

+0

謝謝你的回答。你的代碼比我的文件夾更快。但問題依然存在。請閱讀我編輯的評論。 – noprops