为访问过的每个网页动态创建目录和文件

时间:2016-08-20 18:59:10

标签: bash applescript

我编写了以下脚本来记录我访问过的网页。运行时,活动选项卡的文本内容将被记录并存储在目录中。我希望能够做的是每次页面更改时都有一个新的目录和文件;是否激活了另一个选项卡或是否在同一选项卡中加载了另一个URL。就像现在一样,脚本将记录所有内容,但它会记录脚本启动时打开的网页目录中的所有内容。因此,如果我在此页面处于活动状态时启动脚本,则我访问的任何其他页面都将记录在与此页面记录的内容正下方相同的文件和目录中。

#!/bin/bash

getChrome() {   
    osascript -e \
    'tell application "Google_Chrome" to tell the active tab of window 1 to execute javascript "document.body.innerText"'
}

url="$(osascript -e 'tell application "Google_Chrome" to set the_URL to the URL of the active tab of window 1')"
site="$(echo $url | cut -d/ -f3)" 

# removes . and replaces with _
if [[ $(echo "$site") =~ '.' ]]; then 
    site="$(echo "${site//./_}")"
fi

ds="$(date "+%m-%d-%y")"
dir="$HOME/Desktop/netlogs/$site/$ds"

if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi

doc="${site}_LOG.txt"
file="${dir}/${doc}"

printf "\nBEGIN\n\n" | tee -a $file
while true
do 
    getChrome | while read lines
    do 
        echo $lines 
        # This if statement doesn't work.
        # Included here to show intent
        if [[ $url != $url ]]; then
            break 1
        fi  
    done 
done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file

在试图弄清楚这样做的方法时,我写了这篇Applescript,以便在我更改标签时获得有关正在发生的事情的一些音频反馈:

tell application "Google_Chrome" to tell window 1
    tell the active tab
        set the_url to the URL
        repeat
            if the URL is not the_url then
                set the_url to the URL
                say "nope"
            else
                say "yep"
            end if
        end repeat
    end tell
end tell

除了非常烦人之外,它确实让我知道网址的更改已被识别。但我无法弄清楚如何采用同样的想法,并在网址更改时使用它创建一个新的目录和文件。我不是必须寻找AppleScript解决方案。事实上,我更喜欢尽可能避免使用AppleScript,因为它通常会带来头痛和最终的愤怒。但是当它工作时它运作良好,我会对任何做到这一点的方法感到满意。

1 个答案:

答案 0 :(得分:1)

功能:

let documentsFolderURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomain: .UserDomainMask).first!

脚本:

#!/bin/bash

getSAFARI() {   
    osascript -e \
    'tell application "Safari" to tell current tab of window 1 to do javascript "document.body.innerText"' 2>&1
}
getCHROME() {   
    osascript -e \
    'tell application "Google_Chrome" to tell active tab of window 1 to execute javascript "document.body.innerText"' 2>&1 
}
SURF() {
    if [[ "$(echo $current_url)" != "$(echo $url)" ]]; then
        echo -e "\nEND\n\n" | tee -a $file 2>&1
        break 1
    elif [[ "$(echo $current_url)" = https://example.com/*/ ]]; then
        regex='[[:alnum:]]'
    else
        regex='[\p{L}]'
    fi
    get$BROWSER | while read lines; do echo $lines | grep -E $regex; done < <(get$BROWSER)
}
SET_VARIABLES() {

    ds="$(date "+%m_%d_%y")"
    site="$(echo $url | cut -d/ -f3)"
    dir="$HOME/Desktop/netlogs/$ds/$site"
    doc="${site}_LOG.txt"
    file="${dir}/${doc}"
    if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi
    echo '-----------------------------' | tee -a $file 2>&1
    date | tee -a $file 2>&1
    echo -ne "$url\n" | tee -a $file 2>&1
    echo '-----------------------------' | tee -a $file 2>&1
    printf "BEGIN\n\n" | tee -a $file 2>&1

}