I use these subroutines as part of larger scripts that load images, zipped files and zipped folders from various web sites. These subroutines employ a logging script that is loaded separately and included here separately.
Here is a example of the code I include in the main program to access these two subroutine files:
property safari_subroutines : null
property log_it : null
set shared_folder_path to path to shared documents as string
set safari_subroutines to load script file (shared_folder_path & "AppleScript - General Purpose:Safari Utilities:Common Subroutines.scpt")
set log_it to load script file (shared_folder_path & "AppleScript - General Purpose:Tracking Utilities:Log It.scpt")
set log_it of safari_subroutines to log_it
set log_file_path of log_it to "BasementHD:Users:programming:Documents:Load Logs:my log.txt"
tell log_it to add_to_log(return & return & return & "Start Load " & (current date))
Here is the Common Subroutines file
property log_it : "null"
on save_image_using_image_URL(file_name, save_folder, image_URL)
-- file_name must have an extension for this to work
if my open_image(image_URL) then
set saved_image to my save_image_with_comments_from_current_window(file_name, save_folder, "")
else
tell log_it to add_to_log(">>>> Image not found for: " & image_URL)
set saved_image to false
end if
my close_safari_window()
return saved_image
end save_image_using_image_URL
on save_image_with_comments_from_current_window(file_name, save_folder, file_comments)
set AppleScript's text item delimiters to ":"
set temp_list to text items of file_name
set AppleScript's text item delimiters to "/"
set file_name to temp_list as string
set AppleScript's text item delimiters to ""
tell application "System Events" to set exists_flag to exists file (file_name) in folder save_folder
if exists_flag then
tell log_it to add_to_log(" >>>> Duplicate not saved: " & save_folder & file_name)
set saved_image to false
else
with timeout of 5 * minutes seconds
tell application "Safari" to save document 1 in save_folder & file_name
end timeout
set saved_image to true
if (length of file_comments) > 750 then set file_comments to text 1 thru 750 of file_comments
tell application "Finder" to set comment of file (save_folder & file_name) to file_comments
end if
return saved_image
end save_image_with_comments_from_current_window
on save_zip_folder_if_new(folder_name, target_folder, type_name, zip_URL, folder_name_list, download_folder)
set AppleScript's text item delimiters to ":"
set temp_list to text items of folder_name
set AppleScript's text item delimiters to "/"
set folder_name to temp_list as string
set AppleScript's text item delimiters to ""
tell application "Finder"
set folder_exists to exists folder folder_name in folder target_folder
if folder_exists then
tell log_it to add_to_log("Exists " & type_name & ": " & zip_URL)
log "Exists " & type_name & ": " & zip_URL
else
tell log_it to add_to_log("New " & type_name & ": " & zip_URL)
log "New " & type_name & ": " & zip_URL
tell application "Safari" to open location zip_URL
set delay_count to 0
set folder_loaded to false
set log_missing_file to true
repeat while not folder_loaded
repeat with loaded_name in folder_name_list
if (exists folder loaded_name in folder download_folder) then
set folder_loaded to true
exit repeat
end if
end repeat
if delay_count > 300 then
if log_missing_file then
log (item 1 of folder_name_list)
set log_missing_file to false
end if
display alert "Long Wait" message "Looking in " & download_folder & " for " & (item 1 of folder_name_list) as warning buttons {"OK"} giving up after 30
delay 30
set delay_count to delay_count + 60
else
if delay_count > 120 then beep
delay 5
set delay_count to delay_count + 5
end if
end repeat
my close_safari_window()
my eliminate_sub_folders(download_folder & loaded_name)
set this_folder to folder (download_folder & loaded_name)
set comment of this_folder to zip_URL
set name of this_folder to folder_name
if download_folder ≠target_folder then
move folder (download_folder & folder_name) to folder target_folder
end if
end if
end tell
return not folder_exists
end save_zip_folder_if_new
on save_zip_file_if_new(file_name, target_folder, zip_URL, file_name_list, download_folder)
set AppleScript's text item delimiters to ":"
set temp_list to text items of file_name
set AppleScript's text item delimiters to "/"
set file_name to temp_list as string
set AppleScript's text item delimiters to ""
tell application "Finder"
set file_exists to exists file file_name in folder target_folder
if file_exists then
tell log_it to add_to_log("Exists: " & zip_URL)
else
tell log_it to add_to_log("New: " & zip_URL)
log "New: " & zip_URL
tell application "Safari" to open location zip_URL
set delay_count to 0
set file_loaded to false
set log_missing_file to true
repeat while not file_loaded
repeat with loaded_name in file_name_list
if (exists file loaded_name in folder download_folder) then
set file_loaded to true
exit repeat
end if
end repeat
if delay_count > 600 then
if log_missing_file then
log (item 1 of file_name_list)
set log_missing_file to false
end if
display alert "Long Wait" message "Looking in " & download_folder & " for " & (item 1 of file_name_list) as warning buttons {"OK"} giving up after 30
delay 30
set delay_count to delay_count + 60
else
if delay_count > 180 then beep
delay 30
set delay_count to delay_count + 30
end if
end repeat
my close_safari_window()
set this_file to file (download_folder & loaded_name)
set comment of this_file to zip_URL
set name of this_file to file_name
if download_folder ≠target_folder then
move file (download_folder & file_name) to folder target_folder
end if
end if
end tell
return not file_exists
end save_zip_file_if_new
on open_URL(URL_to_open)
tell application "Safari"
open location URL_to_open
set time_out to my wait_for_open_to_finish()
if time_out then
tell log_it to add_to_log(">>>> Load timeout for: " & URL_to_open)
close document 1
delay 60
open location URL_to_open
set time_out to my wait_for_open_to_finish()
end if
if time_out then
set valid_url to false
tell log_it to add_to_log(">>>> second time out")
else
set valid_url to true
end if
end tell
return valid_url
end open_URL
on wait_for_open_to_finish()
set delay_count to 0
tell application "Safari"
set load_state to (do JavaScript "document.readyState" in document 1)
repeat
try
set current_URL to (URL of document 1) & ""
exit repeat
on error number -2763
delay 2
end try
end repeat
set load_state to (do JavaScript "document.readyState" in document 1)
repeat while (((load_state ≠"complete") and (load_state ≠"loaded")) or (current_URL = "")) and (delay_count < 90)
delay 1
set delay_count to delay_count + 1
set current_URL to (URL of document 1) & ""
set load_state to (do JavaScript "document.readyState" in document 1)
end repeat
end tell
return (delay_count ≥ 90)
end wait_for_open_to_finish
on eliminate_sub_folders(new_folder)
tell application "Finder"
set internal_folder_count to (count of (every folder of folder new_folder))
with timeout of 10 * minutes seconds
if internal_folder_count > 0 then
if internal_folder_count = 1 then
move (every file of entire contents of folder new_folder) to folder new_folder
delete (folder 1 of folder new_folder)
else
tell log_it to add_to_log(">>>> Zip contains multiple nested folders : " & zip_URL)
end if
end if
end timeout
end tell
end eliminate_sub_folders
on close_safari_window()
tell application "Safari" to close document 1
end close_safari_window
Finally, here is the Log It file
property log_file_path : (path to desktop as string) & "Default Log"
on add_to_log(the_message)
set file_id to open for access file log_file_path with write permission
write the_message & return to file_id starting at eof
close access file_id
end add_to_log