macOS批量存储Safari浏览器网址

2024-01-21
#Unix #Automation

1. 代码一

功能:运行 Apple Script 代码,实现把 Safari 浏览器的所有网址,存储到桌面的 MyURLs.txt 文件。

set myURLs to {}
tell application "Safari" to set myURLs to the URL of every tab of every window
set text item delimiters to linefeed
set myURLs to myURLs as text
set text item delimiters to {}
if not myURLs is equal to "" then
    do shell script "echo " & myURLs's quoted form & "> $HOME/Desktop/MyURLs.txt"
end if

2. 代码二

功能:运行 Apple Script 代码,实现把 Safari 浏览器的所有网址,存储到桌面的 MyURLs.txt 文件。

-- 获取 Safari 正在浏览的网页地址
tell application "Safari"
    set safariURLs to URL of every tab of window 1
end tell

-- 将网页地址写入文件
set filePath to (path to desktop as text) & "MyURLs.txt"
set fileHandle to open for access file filePath with write permission
set eof of fileHandle to 0 -- 清空文件内容

repeat with thisURL in safariURLs
    write thisURL & linefeed to fileHandle starting at eof
end repeat

close access fileHandle

3. 代码三

功能:运行 Apple Script 代码,实现把 Safari 浏览器的所有网址,存储到 TextEdit 编辑器的新页面。

-- Set up the initial document
-- set output to ("List of open tabs in Safari" & linefeed as string) & "=======================" & linefeed as string
set output to linefeed as string

tell application "Safari"
    
    -- Count the number of Windows
    set numWindows to number of windows
    
    
    repeat with w from 1 to numWindows
        
        --Count the number of Tabs
        set numTabs to number of tabs in window w
        
        repeat with t from 1 to numTabs
            
            -- Set the Tab Name and URL values
            set tabName to name of tab t of window w
            set tabURL to URL of tab t of window w
            
            -- Create the Tab link and write the code               
            -- set output to output & tabURL & "    " & tabName & linefeed as string
            set output to output & tabURL & "    " & linefeed as string
        end repeat
    end repeat
end tell

-- Write the entire document to TextEdit

tell application "TextEdit"
    activate
    make new document
    set the text of the front document to output
end tell

4. 参考