• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Saving a Safari web page archive or PDF into a new folder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Saving a Safari web page archive or PDF into a new folder


  • Subject: Saving a Safari web page archive or PDF into a new folder
  • From: "Joseph Pollone" <email@hidden>
  • Date: Mon, 27 Feb 2006 09:11:28 -0500

I enabled GUI scripting and would like to save a Safari Web Archive (or a PDF) to a folder I create with input from the script.  With the code below (pilfered from MacScripter and Apple), the AppleScript will ask for a folder name, create a folder by prepending the mm-dd to the input name ( i.e. 02-25 Bananas), but it will save the current Safari page archive to the last folder where an archive was saved.  In my case, the Desktop.

How do I save the archive to the folder I just created?

The format does not need to be a web archive as a PDF will do fine too.  I have seen examples of writing an HTML file to a specific folder, but this will be my last resort as I would like to preserve the fidelity of the page.

Thoughts?



-- SET THE DEFAULT PATH
set the destination_path to ("/Users/user1/Documents/")

-- GET DATE
set current_date to the current date
set the month_index to (the month of current_date as integer)
set the day_index to (the day of the current_date)
set the day_index to my add_leading_zeros(day_index, 1)
set the month_index to my add_leading_zeros(month_index, 1)

display dialog "What's the folder name?" default answer ""
set folder_name_input to text returned of result
set the new_folder_name to month_index & "-" & day_index & " " & folder_name_input

-- REMOVE ILLEGAL CHARACTERS & ADD QUOTES FOR THE NEW FOLDER
if the new_folder_name contains ":" then
    set the new_folder_name to my replace_chars(new_folder_name, ":", "-")
end if
set new_path to destination_path & "'" & new_folder_name & "'"

-- MAKE A NEW FOLDER
tell application "Finder"
    set folderPOSIX to POSIX path of (new_path)
    do shell script ("mkdir " & folderPOSIX)
end tell

-- SAVE SAFARI WEB PAGE ARCHIVE
do_menu("Safari", "File", "Save As")

set new_file_name to new_folder_name
tell application "Safari" to activate
tell application "System Events"
    tell process "Safari"
        tell window 1
            tell sheet 1
                set value of text field 1 to new_file_name
                if the value of checkbox "Hide Extension" is 0 then
                    click checkbox "Hide Extension"
                end if
                click button 1
            end tell
        end tell
    end tell
end tell





-- DONE


on add_leading_zeros(this_number, max_leading_zeros)
    set the threshold_number to (10 ^ max_leading_zeros) as integer
    if this_number is less than the threshold_number then
        set the leading_zeros to ""
        set the digit_count to the length of ((this_number div 1) as string)
        set the character_count to (max_leading_zeros + 1) - digit_count
        repeat character_count times
            set the leading_zeros to (the leading_zeros & "0") as string
        end repeat
        return (leading_zeros & (this_number as text)) as string
    else
        return this_number as text
    end if
end add_leading_zeros


on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

on do_menu(app_name, menu_name, menu_item)
    try
        -- bring the target application to the front
        tell application app_name
            activate
        end tell
        tell application "System Events"
            tell process app_name
                tell menu bar 1
                    tell menu bar item menu_name
                        tell menu menu_name
                            click menu item menu_item
                        end tell
                    end tell
                end tell
            end tell
        end tell
        return true
    on error error_message
        return false
    end try
end do_menu
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: OT: HTML mail and multi-part mime [ not Re: list syntax ]
  • Next by Date: Re: OT: HTML mail and multi-part mime [ not Re: list syntax ]
  • Previous by thread: Re: Referencing "unknowns" with UI scripting
  • Next by thread: Creating MacBinary files: command line or AScript tools?
  • Index(es):
    • Date
    • Thread