如何自动从不同位置下载多个PDF文件(AppleScript或JavaScript)

时间:2011-05-20 08:38:18

标签: javascript applescript

我必须每周分析大约30个促销商店文件夹。现在我知道在哪里可以找到HTML文件夹,但我仍然要从每个位置手册重写URL,我想自动化。

我只能使用AppleScript或JavaScript(在我的MobileMe主机上,没有PHP eo。)

在URL中有一些变量:

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

我想让它像这样工作:

  1. 要求我告诉的来自或对话

    一个。商店:[填写商店];
      湾地区:[填写区域];
      C。日期:[填写日期]。

  2. 下载文件并保存在我的计算机上的//specific/path

  3. 我是AppleScript和JavaScript的菜鸟,但我无法使用PHP / SQL,所以我希望有人可以帮我展示其他方向。

2 个答案:

答案 0 :(得分:1)

AppleScript(下载到桌面):

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

答案 1 :(得分:1)

安妮给了你一个很好的剧本,但我想你可以让自己很容易。我假设商店总是与地区和网站相关联,因此您可以将此信息输入到脚本中,然后您可以从列表中选择它而不是每次都输入它。

因此,您需要使用该信息创建记录列表。我在脚本顶部的 storeRegionRecord 中为您输入了4个样本。您还需要在 downloadFolder 变量中输入应下载文件的文件夹路径。

按照说明输入信息后,脚本的工作原理如下。弹出一个对话框,您可以在其中选择一个或多个商店/地区/网站组合进行下载。如果日期适用于其中几个,您可以选择1个以上。按住Shift键单击或按住Command键单击可在对话框中选择多个。然后会弹出第二个对话框,您可以在其中输入特定日期。重复循环遍历您选择的商店/地区/网站,并将每个pdf文件下载到下载文件夹,并按照安妮在其代码中的建议命名下载的pdf。

我希望这会有所帮助......

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