Jump to content
bb13

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

 

Share this post


Link to post

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”.

Share this post


Link to post
Posted (edited)

Thanks for responding deanishe, I didn't run the debugger! Doh. The location of WGET was the main issue. Thank you, that solved it!

Edited by bb13

Share this post


Link to post

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')"

 

Share this post


Link to post

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!

Share this post


Link to post

The command Vítor posted above is a bash/zsh shell command. It’s calling Chrome via AppleScript, yes, but embedded in a shell command.

 

Just replace the line of your script that fetches and tries to parse the HTML.

Share this post


Link to post

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')"

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...