applescript:如何自动选择使用系统事件在特定路径中保存safari文档

时间:2011-10-05 07:54:52

标签: applescript

我想创建一个脚本,使用系统事件(命令+ s)保存safari文档,我希望文件始终保存在特定路径中。 (我可以养成在某个文件夹中保存safari文件的习惯,但这不是一个强大的解决方案)如何确保系统事件将文档保存在某个路径?

2 个答案:

答案 0 :(得分:1)

直接来自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

根据需要设置路径(the_shellfilepath)。

答案 1 :(得分:1)

更改文件对话框的文件夹:

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

使用部分编码的URL作为文件名保存到特定文件夹:

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

保存到特定文件夹,但手动选择文件名:

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