使用 Applescript 将 Safari 网页上选定的 URL 捕获到文本文件

问题描述 投票:0回答:1

使用 SAFARI 作为网络浏览器,我想将我用鼠标选择的 URL 存储到文本文件中。我认为这可以通过上下文菜单(单击鼠标右键)和“服务”功能来实现。

有人可以帮助我吗?

我尝试使用 AUTOMATOR 工具而不使用 Applescript 来做到这一点。但预定义工具的任何组合都不起作用。

url applescript text-files
1个回答
0
投票

这是一个简单的任务。您可以使用以下 5 个 Javascript 命令中的任何一个。当然,你必须先用鼠标右键选择链接。

tell application "Safari"
    set selectedText to (do JavaScript "''+window.getSelection();" in front document) -- (Two single quotes before the plus sign.)
    -- or:
    set selectedText to (do JavaScript "window.getSelection()+'';" in front document)
    -- or:
    set selectedText to (do JavaScript "\"\"+window.getSelection();" in front document)
    -- or:
    set selectedText to (do JavaScript "window.getSelection()+\"\";" in front document)
    -- or
    set selectedText to do JavaScript "var selObj = window.getSelection();
        var selStr = (selObj).getRangeAt(0);
        unescape(selStr);" in document 1
end tell


write_to_file(selectedText, (path to desktop as text) & "URLs.txt", true)


on write_to_file(|data|, target, append)
    try
        set open_target_file to open for access file target with write permission
        if append is false then set eof of open_target_file to 0
        write |data| to open_target_file starting at eof
        close access open_target_file
        return true
    on error
        try
            close access file target
        end try
        return false
    end try
end write_to_file
© www.soinside.com 2019 - 2024. All rights reserved.