Peter
Peter

Reputation: 11

How to automate downloading multiple PDF files from different locations (AppleScript or JavaScript)

I have to analyse about 30 promotional store folders every week. Nowadays I know where to find the folder HTML by heart, but still I have to rewrite the URL from every location manual, which I'd like to automate.

I only have the ability to use AppleScript, or possibly JavaScript (On my MobileMe Hosting, no PHP eo.)

In the URL there are some variables:

http://store.website.com/[store]/[region]/[date]NL/[store]_[region]_[date]NL.pdf

I'd like to have it work like this:

  1. A from or dialog which ask me to tell

    a. Store: [fill in Store];
    b. Region: [fill in region];
    c. Date: [fill in date].

  2. Download file and save on //specific/path on my machine.

I'm a noob with AppleScript and JavaScript, but I'm unable to use PHP/ SQL, so I hope someone can help me showing some other directions.

Upvotes: 1

Views: 2061

Answers (2)

regulus6633
regulus6633

Reputation: 19030

Anne gave you a good script however I figure you can make this very easy on yourself. I'm assuming that a store is always associated with a region and website, therefore you can enter this information into the script and then you can just choose it from a list instead of typing it in every time.

As such, you need to create a list of records with that information. I entered 4 samples for you in the storeRegionRecord at the top of the script. You also need to enter a folder path in the downloadFolder variable where the files should be downloaded.

Here's how the script works after you enter the information as explained. A dialog pops up where you can choose one or more store/region/website combinations to download. You would choose more than 1 if a date would apply to several of them. Shift-click or Command-click to choose more than 1 in the dialog. Then a second dialog pops up where you enter the particular date. A repeat loop cycles through the store/region/websites you chose and downloads each pdf file to the download folder, naming the downloaded pdf as Anne suggested in her code.

I hope this helps...

property storeRegionRecord : {{store:"store1", region:"region1", website:"http://store1.website.com/"}, {store:"store2", region:"region2", website:"http://store2.website.com/"}, {store:"store3", region:"region3", website:"http://store3.website.com/"}, {store:"store4", region:"region4", website:"http://store4.website.com/"}}
property aDate : ""
property listDelimiter : "*"
set downloadFolder to path to desktop as text


-- get the store/region/website to download. You can choose more than 1.
set chooseList to chooseListForStoreRegionRecord(storeRegionRecord)
choose from list chooseList with title "PDF Downloads" with prompt "Choose one or more..." default items (item 1 of chooseList) OK button name "Select" cancel button name "Quit" with multiple selections allowed
tell result
    if it is false then error number -128 -- cancel
    set selectedItems to items
end tell

-- enter the date
display dialog "Date?" default answer aDate
set aDate to the text returned of the result

-- download the files
set text item delimiters to listDelimiter
repeat with aSelection in selectedItems
    set theVariables to text items of aSelection
    set theStore to item 1 of theVariables
    set theRegion to item 2 of theVariables
    set theWeb to item 3 of theVariables
    if theWeb does not end with "/" then set theWeb to theWeb & "/"

    set link to theWeb & theStore & "/" & theRegion & "/" & aDate & "NL/" & theStore & "_" & theRegion & "_" & aDate & "NL.pdf"

    set destination to downloadFolder & theStore & "_" & theRegion & "_" & aDate & "NL.pdf"

    tell application "URL Access Scripting" to download link to file destination replacing yes
end repeat
set text item delimiters to ""

-- tell me that it's finished
display dialog "The PDF files were downloaded to:" & return & (downloadFolder as text) buttons {"OK"} default button 1 with icon note



(*============= SUBROUTINES ===============*)
on chooseListForStoreRegionRecord(storeRegionRecord)
    set chooseList to {}
    repeat with aRecord in storeRegionRecord
        set end of chooseList to store of aRecord & listDelimiter & region of aRecord & listDelimiter & website of aRecord
    end repeat
    return chooseList
end chooseListForStoreRegionRecord

Upvotes: 1

Anne
Anne

Reputation: 27073

AppleScript (downloads to your desktop):

display dialog "Store?" default answer ""
set the Store_Input to the text returned of the result

display dialog "Region?" default answer ""
set the Region_Input to the text returned of the result

display dialog "Date?" default answer ""
set the Date_Input to the text returned of the result

set link to "http://store.website.com/" & Store_Input & "/" & Region_Input & "/" & Date_Input & "NL/" & Store_Input & "_" & Region_Input & "_" & Date_Input & "NL.pdf"

set the destination to ((path to desktop as string) & Store_Input & "_" & Region_Input & "_" & Date_Input & "NL.pdf")

tell application "URL Access Scripting"
    download link to destination replacing yes
end tell

Upvotes: 1

Related Questions