Jump to content

Shell Script - Extract Title and URL from Chrome Tab


Recommended Posts

Hi, I am trying to run a shell script from within alfred that will copy the title and URL of current Chrome page and insert it into a text file/log. I can get this script to work in terminal but not from Alfred. Any help is appreciated.

 

#!/bin/bash

#========================================================================================
#
# Description: Extracts browser Window Title and saves in log file
#
# Usage      : ./gettitle.sh "URL"
#
# NOTE        : Enclose URL in double quotes
#
#========================================================================================

# Log file : Change as needed by providing full path of the log file
log="/Users/NOTMYREALNAME/Dropbox/text/dailylog19.taskpaper"

# Extract day from date (Example: Sunday)
day=$(date +%A)
# Date iin MM/DD/YYYY format
datemdy=$(date +%m/%d/%Y)
# Prepare date tag
today=$(echo "$day $datemdy:")
#Date tag
datetag=$(date +@\(%Y-%m-%d\))

url="$1"

#----------------------------------------------------------------------------------------

# Extract the tab title (there maybe more title tags so just grabbing the first one)
title=$(wget -q -O - "$url" | grep -io "<title.*>*</title>" | sed -e 's/<[^>]*>//g' | head -n1)

# Update log file when title is found (Use date tag once per day)
if [ -n "$title" ]
then
    grep -q "$today" "$log"

    if [ "$?" -ne 0 ]
    then
        echo "$today" >> "$log"
        echo "" >> "$log"
    fi

    echo -e "\t$title" >> "$log"
    echo -e "\t$url $datetag" >> "$log"
    echo "" >> "$log"
fi

exit 0

 

Link to comment

What does Alfred’s debugger say? That’s where your error will be.

 

At a guess, I’d say it’s because wget isn’t part of macOS, and is installed in /usr/local/bin.

 

That’s not on your PATH in Alfred, so use “/usr/local/bin/wget” instead of just “wget”.

Link to comment

Bash, tools that don’t come pre-installed (wget), and HTML grepping all make for a finicky solution which is prone to break sooner rather than later.


Chrome has great support for AppleScript. Use that.

title="$(osascript -e 'tell application "Google Chrome" to return title of active tab of front window')"

 

Link to comment

Thank you, vitor, I agree and will give your solution a try! My only issue is that it has to write to a text log file with date, tags etc. I will do some research and try to do the same thing with Applescript. Thanks again for the suggestion and link!

Link to comment

deanishe and vitor, I can see why you two are community heroes! Thank you both. I'm smiling over here, finally got this workflow working the way I wanted it to. vitor, that worked *perfectly* and I just added the URL line from your page. I really appreciate the help from both of you! Thanks again.

title="$(osascript -e 'tell application "Google Chrome" to return title of active tab of front window')"
url="$(osascript -e 'tell application "Google Chrome" to return URL of active tab of front window')"

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...