bb13 Posted July 30, 2019 Share Posted July 30, 2019 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
deanishe Posted July 30, 2019 Share Posted July 30, 2019 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”. bb13 1 Link to comment
bb13 Posted July 30, 2019 Author Share Posted July 30, 2019 (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 July 30, 2019 by bb13 Link to comment
deanishe Posted July 30, 2019 Share Posted July 30, 2019 👍 Glad it's working. Remember the debugger if you run into another issue! bb13 1 Link to comment
vitor Posted July 31, 2019 Share Posted July 31, 2019 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')" bb13 1 Link to comment
bb13 Posted July 31, 2019 Author Share Posted July 31, 2019 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 Posted July 31, 2019 Share Posted July 31, 2019 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. bb13 1 Link to comment
bb13 Posted July 31, 2019 Author Share Posted July 31, 2019 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')" vitor 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now