user912475
user912475

Reputation:

applescript: how to automatically choose to save a safari document at a PARTICULAR PATH with system events

I want to make a script that will save safari documents using system events (command+s) and I want the file to always be saved at a particular path. (I could make a habit of always saving safari files at a certain folder but that's not a robust solution) How can I make sure system events saves a document at a certain path?

Upvotes: 1

Views: 1768

Answers (2)

Lri
Lri

Reputation: 27613

Changing the folder of a file dialog:

try
    try -- this would result in an error when there's no clipboard
        set old to the clipboard
    end try
    -- delay 1 -- for testing
    -- activate application "Safari"
    tell application "System Events"
        keystroke "s" using command down
        keystroke "g" using {shift down, command down}
        delay 0.1
        set the clipboard to "~/Movies/"
        delay 0.1
        keystroke "av" using command down
        delay 0.1
        keystroke return
        delay 0.1
    end tell
    set the clipboard to old
end try

Saving to a specific folder with a partially encoded URL as a file name:

tell application "Safari"
    set x to URL of document 1
    set r to do shell script "echo " & quoted form of x & " | sed 's|/$||;s|:|%3A|g;s|/|%2F|g'"
    do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & r & ".html")
end tell

Saving to a specific folder but choosing the file name manually:

tell application "Safari"
    set x to URL of document 1
    set answer to text returned of (display dialog "" default answer ".html")
    do shell script "curl " & x & " > " & quoted form of ((system attribute "HOME") & "/Desktop/" & answer)
end tell

Upvotes: 1

eykanal
eykanal

Reputation: 27017

Directly from this MacRumors forum post:

tell application "Safari"
    activate
end tell

tell application "System Events"
    tell process "Safari"
        click menu item "Copy" of menu "Edit" of menu bar 1
    end tell
end tell

delay 1

set the_filename to (the clipboard) & ".html"
set the_filepath to "Macintosh HD:Users:Roy:Documents:" & the_filename
set the_shellfilepath to "'/Users/Roy/Documents/" & the_filename & ".download'"
set the_shellfilepath2 to "'/Users/Roy/Documents/" & the_filename & "'"

tell application "Safari"
    activate
    save document 1 in the_filepath
end tell

do shell script "mv " & the_shellfilepath & " " & the_shellfilepath2

Set the path (the_shellfilepath) as appropriate.

Upvotes: 1

Related Questions