2017-12-12 16 views
0

私の例の.CSVファイルでは、次の値を持つ列Aがあります:2,2,5,5,5。 B列には、ダウンロードが必要な対応ファイルへのハイパーリンクがあります。http://example.com/2A.pdfhttp://example.com/2B.pdfhttp://example.com/5A.pdf、、http://example.com/5BC.pdfです。 (一意でない列Aの値に基づいて)DLフォルダーを作成し、対応する行のDLを適切なフォルダーに適用するAppleScriptを作成するのが難しい。どんな助けでも大歓迎です! TIA。.CSV値に基づいてweblinkから作成されたフォルダに作成されたフォルダにファイルをダウンロードするためのAppleScript

+0

これまでに試したことのコードサンプルをご提供ください。そうすれば、私たちはより効果的にあなたを助けることができます。 – Ivonet

答えて

0

あなたの説明から、私は、CSVファイルには、次のようなものになりますと仮定しています:

2, http://example.com/2A.pdf 
2, http://example.com/2B.pdf 
5, http://example.com/5A.pdf 
5, http://example.com/5B.pdf 
5, http://example.com/5BC.pdf 

とファイル2A.pdfがというディレクトリに終わるべきであるが。

これを正しく解釈したと仮定すると、これを実現するAppleScriptがあります(/Path/To/Save/Folder/を、これらの新しいファイルとフォルダがダウンロードされるフォルダへのフルパスで置き換えます)。 CSVファイルのフルパスに/Path/To/File.csv):

-- Note: The ending/is essential in BaseDir 
    set BaseDir to POSIX file "/Path/To/Save/Folder/" 
    set rows to every paragraph of (read "/Path/To/File.csv") 

    set AppleScript's text item delimiters to {"/"} 

    set [TopDir, BaseFolder] to [text items 1 thru -3, text item -2] of ¬ 
     POSIX path of BaseDir 


    -- Create initial base folder if it doesn't exist 
    tell application "Finder" to if not (exists BaseDir) then ¬ 
     make new folder in (TopDir as text as POSIX file) ¬ 
     with properties {name:BaseFolder} 

    -- This will prevent EOF-related errors 
    -- Thanks to @user3439894 for highlighting the need for this 
    set rows to reverse of rows 
    if the number of words of the first item in rows is 0 ¬ 
     then set rows to the rest of rows 

    repeat with row in rows 

     set [N, |url|] to [first word, text items -2 thru -1] of row 

     -- Create folder into which file will be downloaded 
     tell application "Finder" to if not (exists [BaseDir, N] as text) then ¬ 
      make new folder in BaseDir with properties {name:N} 

     -- This is the shell command that downloads the file 
     -- e.g. "cd '/Users/CK/Desktop/example.com/5'; curl example.com/5BC.pdf \ 
     --  remote-name --silent --show-error" 
     set command to ("cd " & ¬ 
      quoted form of POSIX path of ([BaseDir, N] as text) & ¬ 
      "; curl " & |url| as text) & ¬ 
      " --remote-name --silent --show-error" 

     -- Run the shell command 
     do shell script command 

    end repeat 

    -- Open folder in Finder upon completion of downloads 
    tell application "Finder" 
     activate 
     open BaseDir 
    end tell 

私は、これは状況全体に働くだろう、一般的な使用のために意図ない堅牢なスクリプトであることを強調する必要がありますが、むしろに合わせたスクリプト説明されたケースに明確に対応する。あなたのCSVファイルが私の想定されているフォーマットと少しでも違う場合、エラーが発生する可能性があります。

+1

投稿する前にコードをテストしましたか?それは素晴らしいスクリプトですし、もしうまくいけば+1します。主な問題は、 'do shell script command'が実行されたときに' command'に '/'がありません。最後に_newline_( '0A')がある場合、二次的な問題は失敗しています。これはかなり正常であり、ファイルの行をループするときに常にテストする必要があります。 (また、ファイルの行内で空行で失敗することもあります)。また、AppleScriptのテキスト項目区切り文字を変更してスクリプトのポイントでリセットすると、その変更はもはや適用されないと一般に認められています。 – user3439894

+0

@ user3439894不足している「/」を強調表示していただきありがとうございます。 * Script Editor *のコードを* Safari *にコピーし、ダミー宣言*/Path/To/Save/Folder/*にテストフォルダを代入すると、末尾の "/"を忘れてしまったため、このエラーがスローされました。潜在的なEOF問題に関しては、これは私がまだこれの初心者であるという兆候の1つです。あなたが言わなかったら私には起こりませんでした。私はこれをキャッチするためにいくつかの行を追加しました。もう一度感謝します。最後に、私は意図的にTIDをリセットしないように選択します。これは今のようにこれを維持します。これはすべてのスクリプトで共通です。 – CJK

関連する問題