Applescript: How to download linked files from Google Chrome page

applescriptautomatorgoogle-chrome

I am needing to find a way to download linked files on a webpage in Google Chrome.

I am the graphic artist at the company I work for and I have a "project brief" type form that other employees fill out when they need to request design work. In the form they can upload files that relate to the project (such as sketches, or text files). I have an Automator app that sets up the project from creating a folder system for the project to downloading the form as a pdf to downloading all attached files. However it only works in Safari and I use Chrome. So far I've gotten everything moved over and working in Chrome EXCEPT for the file downloads.

Can someone help me figure out how to do this Chrome?

Here is the "download files" script that works in Safari:
(ignore the '\' in Applescript\'s. Without them I was getting weird syntax highlighting.)

-- DOWNLOAD ATTACHED FILES
tell application "Safari"
activate

set num_links to (do JavaScript "document.links.length" in document 1)
repeat with i from 0 to num_links - 1
    tell application "Safari" to set this_link to do JavaScript "document.links[" & i & "].href" in document 1
    set {ASTID, AppleScript\'s text item delimiters} to {AppleScript\'s text item delimiters, "/"}
    set fName to last text item of this_link
    set AppleScript\'s text item delimiters to ASTID
    try
        do shell script "curl -L -o " & quoted form of (destinationFolder & fName) & space & this_link
    end try
end repeat

end tell

When I replace both instances of "Safari" with "Google Chrome" and try to run it I get a Syntax Error message that says: Expected “,” but found identifier. and has the JavaScript in the set num_links line highlighted.

Best Answer

I got this to compile correctly on latest OSX

-- DOWNLOAD ATTACHED FILES
tell application "Google Chrome"
    activate
    set num_links to execute front window's active tab javascript "document.links.length"
    repeat with i from 0 to num_links - 1
        tell application "Google Chrome" to set this_link to execute front window's active tab javascript "document.links[" & i & "].href"
        set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
        set fName to last text item of this_link
        set AppleScript's text item delimiters to ASTID
        try
            do shell script "curl -L -o " & quoted form of (destinationFolder & fName) & space & this_link
        end try
    end repeat
end tell