------------------------------------------------------------------------------------------------
# Created: 2012-09-07 : 03:16
# Modified: 2012-09-07 : 03:57
# Application: Safari & Progressive Downloader
# Purpose: Download GraphicConverter Beta
# Dependencies: None: Only stock OSX components used.
------------------------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------------------------
on goGCBetaPage()
tell application "Safari"
end tell
end goGCBetaPage
------------------------------------------------------------------------------------------------
on progressiveDownload(_referer, _linkList, _destination)
tell application "Progressive Downloader"
activate
repeat with i in _linkList
set _task to make new task
tell _task
set address to i
set referer to _referer
set destination to _destination
end tell
end repeat
if (count of tasks) > 0 then
resume every task
else
error "No Tasks in Progressive Downloader!"
end if
end tell
end progressiveDownload
------------------------------------------------------------------------------------------------
on safari_links(regexStr, tagName, tagType)
set js to "function in_array (array, item) {
for (var i=0; i < array.length; i++) {
if ( array[i] == item ) {
return true;}}
return false;}
var a_tags = document.getElementsByTagName('" & tagName & "');
var href_array = new Array();
var reg = new RegExp(/" & regexStr & "/i);
for (var i=0; i < a_tags.length; i++) {
var href = "" style="font-family: Verdana; color: rgb(0, 0, 0); "> & tagType & ";
if ( reg.test(href)) {
if ( !in_array(href_array, href)) {
href_array.push(href);}}}
href_array;"
try
tell application "Safari" to set linkList to do _javascript_ js in document 1
on error
set linkList to {}
end try
return linkList
end safari_links
------------------------------------------------------------------------------------------------
on safari_url()
tell application "Safari" to return URL of front document
end safari_url
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------
try
goGCBetaPage()
delay 0.2
set pageURL to safari_url()
set regexStr to "gc.*\\.dmg"
set linkList to safari_links(regexStr, "*", "href")
if linkList ≠ {} then
set linkList to item 1 of linkList as list
progressiveDownload(pageURL, linkList, POSIX path of (get path to downloads folder))
end if
on error e number n
set e to "Error: " & e & return & return & "Error Number: " & n
beep
tell me to set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------------------------