2017-05-01 9 views
0

私はフォルダを自動的に作成し、最初の番号に基づいてファイルを並べ替えるフォルダを作成しようとしています。私が並べ替えが必要なファイルは、2月4 2.3 U#03(3).mrdのような名前の同様の形式です。私の意図は、いくつかのAppleScriptを書いて、数字(2.3)に基づいてフォルダを作成してから、そのフォルダに(2.3)を含むすべてのファイルを置き、他のすべてのファイルと同じことをすることでした。AppleScriptを使用して最初の番号に基づいて新しいフォルダにファイルを並べ替え

私は今、私は最初の数に基づいて、新しいフォルダを作成し、内の一致するファイルを配置する必要があり

set text item delimiters to {" "} 
tell application "Finder" 
    set aList to every file in folder "Re-namer" 
    repeat with i from 1 to number of items in aList 
    set aFile to (item i of aList) 
    try 
     set fileName to name of aFile 
     set firstPart to text item 1 of fileName 
     set secondPart to text item 2 of fileName 
     set thirdPart to text item 3 of fileName 
     set fourthPart to text item 4 of fileName 
     set fifthPart to text item 5 of fileName 
     set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart 
    set name of aFile to newName 
    end try 
end repeat 
end tell 

、動作しているようです彼らの数に基づいてファイルを並べ替えビットを作った。私は、あまりにも、このためのスクリプトを作ってみました(私は前にコード化されていないと私はやってないアイデアを持っていたことがありません心に留めておく)と当然それはうまくいきませんでした:(

tell application "Finder" 
open folder "Re-namer" 
set loc to folder "Re-namer" 
set aList to every file in loc 
repeat with i from 1 to number of items in aList 
    set aFile to (item i of aList) 
    if not (exists folder named "text item 1" in loc) then 
     make new folder in loc with properties {name:"text item 1"} 
    else 
     move aFile in folder "text item 1" 
    end if 
end repeat 
end tell 

私はいくつかを見つけました私はまだそれを働かせることはできません。誰かがこの質問を助けるためのアイデアやリソースがあれば私はそれを大きく評価するだろう。

+0

あなたのアプローチは初心者にとっては問題ありません。オープンフォルダの "リネーマー"と言うのではなく、そのフォルダへのパスを提供する必要があります。私はそれに言及していませんでした。 –

答えて

0

あなたは正しいスクリプトに非常に近いです。しかし、名前を変更するためのものとファイルを移動するものの2つのループを行うのではなく、同時に両方を行うループを1つだけ行うほうが簡単です。

また、重要なことは、ループ内で名前を変更すると、ファイルへの参照が変更されたことを緩和し、名前の変更後に移動しようとすると失敗することです。フォルダと新しい名前が分かっているので、その "新しい"ファイルを参照することはできます。

set MyRenamer to choose folder "Select the folder" 
set TPath to MyRenamer as string 
set text item delimiters to {" "} 
tell application "Finder" 
    set aList to every file in MyRenamer 
     repeat with F in aList 
     set fileName to name of F 
     set firstPart to text item 1 of fileName -- like "Feb" 
     set secondPart to text item 2 of fileName -- like "4" 
     set thirdPart to text item 3 of fileName -- like "2.3" 
     set fourthPart to text item 4 of fileName -- like "U#03" 
     set fifthPart to text item 5 of fileName -- like "(3).mrd" 
     set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart 
     set name of F to newName 
     if (not (exists folder thirdPart in MyRenamer)) then 
      make new folder in MyRenamer with properties {name:thirdPart} 
     end if 
     move (TPath & newName) as alias to folder (TPath & thirdPart & ":") -- rebuild the new file path as alias and move it. 
    end repeat 
end tell 

テスト済みです。

+0

それは非常に有用です、ありがとうございます! – Matt

関連する問題