Icon on second line?

Hi @unfnshd,

I promised to get back at the above.
Here is my code for a button that shows the favicon. I added 5 buttons. Each to run every 0,5 seconds and only when google chrome is active.

You need to set the location of the download of the favicon. Mine is: "/Users/robertstiekema/BTTdownloads/"

Hope it helps you in the right direction.

Good luck.

# IMPORTANT
# You need to install chrome-cli in terminal (brew install chrome-cli)
# And to allow apple script to be run in Chrome (use chrome preference panel)

# Set constants
set i to 1 as integer #This is the tab-nr. I added 5 times this script as a button. For each button increase i with 1.
set j to 15 #length of button
set activeBackColor to "128,128,128,255"
set activeFontColor to "255,255,255,255"
set normalBackColor to "0,0,0,255"
set normalFontColor to "255,255,255,255"
set tabIconPwdBase to "/Users/robertstiekema/BTTdownloads/"

tell application "Google Chrome"
	# Get tab name
	set tabName to (get title of tab i of (first window whose index is 1))
	# Get icon
	set tabIcon to execute of tab i of (first window whose index is 1) javascript "document.head.querySelector('link[rel~=icon]').href;"
	# Get active tab
	set activeTabIndex to active tab index of (first window whose index is 1) as integer
end tell

set stringLength to length of tabName
if stringLength < j then
	set tabName to tabName as string
else
	set tabName to characters 1 thru (j - 3) of tabName as string
	set tabName to tabName & "..."
end if
set titleString to "\"text\":\"" & tabName & "\""

# Get Icon
#display dialog tabIcon
#set tabId to do shell script "/usr/local/bin/chrome-cli list tabs | sed -n \"" & i & "p\" | sed \"s/].*//\" | sed \"s/\\[//\""
#set tabIcon2 to do shell script "/usr/local/bin/chrome-cli execute \"document.head.querySelector('link[rel~=icon]').href;\" -t " & tabId
#display dialog tabIcon2
set tabIconLocalName to do shell script "echo " & tabIcon & " | grep -oE '[A-Z,a-z,0-9,.]+' | tr '\\n' '_' | rev | cut -c 2- | rev"
#display dialog tabIconLocalName
set tabIconPwd to tabIconPwdBase & tabIconLocalName
set status to false as boolean
tell application "Finder" to if exists tabIconPwd then set status to true
if status is false then
	#try
	#	#display dialog tabIconPwd
	#	tabIconPwd as alias
	#on error
	# CASE: doesn't exist
	#display dialog "Downloading icon for tab " & i
	do shell script "curl " & tabIcon & " -o " & tabIconPwd
	#set t to (time of (current date))
	#do shell script "touch " & t
end if
#end try

set iconString to "\"icon_path\":\"" & tabIconPwd & "\""

# Set fonts and stuff
if activeTabIndex = i then
	# Case: this tab is active tab
	set fontColor to "\"font_color\":\"" & activeFontColor & "\""
	set backgroundColor to "\"background_color\": \"" & activeBackColor & "\""
	#set tempString to "{\"text\":\"" & tempString & "\", \"background_color\": \"" & activeColor & "\"}"
else
	# Case: this tab is not the active sheet
	set fontColor to "\"font_color\":\"" & normalFontColor & "\""
	set backgroundColor to "\"background_color\": \"" & normalBackColor & "\""
	#set tempString to "{\"text\":\"" & tempString & "\", \"background_color\": \"" & normalColor & "\"}"
end if

return "{" & titleString & "," & backgroundColor & "," & fontColor & "," & iconString & "}"

#return tempString
#return "{\"text\":\"tempString\", \"icon_data\": \"base64_icon_data\", \"icon_path\":\"path_to_new_icon\", \"background_color\": \"255,85,100,255\", \"font_color\": \"100,200,100,255\",  \"font_size\": 10}"

#TODO: naam op basis van websitenaam, dan kun je alleen gebruik maken van 'tell google chrome'. Voor check

## set variables
#set i to 1 as integer #Tab-nr
#
## check if tab exists
#set nrOfTabs to do shell script "/usr/local/bin/chrome-cli list tabs | wc -l"
#if i ≤ nrOfTabs then
#	# continue set variables
#	set j to 14 #Length of button (minus 1)
#	set activeBackColor to "128,128,128,255"
#	set activeFontColor to "255,255,255,255"
#	set normalBackColor to "0,0,0,255"
#	set normalFontColor to "255,255,255,255"
#	set tabIconPwdBase to "/Users/robertstiekema/BTTdownloads/" #tab" & i & ".png"
#	
#	# get tab name from chrome-cli
#	set tabName to do shell script "/usr/local/bin/chrome-cli list tabs | sed -n \"" & i & "p\" | sed \"s/.*] //\""
#	
#	# reshape tab name to fit touchbar
#	set stringLength to length of tabName
#	if stringLength > j then
#		set tabName to (characters 1 thru (j - 3) of tabName as string) & "..."
#	end if
#	set titleString to "\"text\":\"" & tabName & "\""
#	
#	# find active sheet (changed color)
#	set tabId to do shell script "/usr/local/bin/chrome-cli list tabs | sed -n \"" & i & "p\" | sed \"s/].*//\" | sed \"s/\\[//\""
#	set countActiveSheet to do shell script "/usr/local/bin/chrome-cli info | grep -c \"" & tabId & "\" || true"
#	if countActiveSheet = "0" then
#		#Case: this tab is not the active sheet
#		set fontColor to "\"font_color\":\"" & normalFontColor & "\""
#		set backgroundColor to "\"background_color\": \"" & normalBackColor & "\""
#	else
#		#Case: this tab is the active tab
#		set fontColor to "\"font_color\":\"" & activeFontColor & "\""
#		set backgroundColor to "\"background_color\": \"" & activeBackColor & "\""
#	end if
#	
#	# get favicon
#	set tabIcon to do shell script "/usr/local/bin/chrome-cli execute \"document.head.querySelector('link[rel~=icon]').href;\" -t " & tabId
#	set tabIconLocalName to do shell script "echo " & tabIcon & " | grep -oE '[A-Z,a-z,0-9,.]+' | tr '\\n' '_' | rev | cut -c 2- | rev"
#	#display dialog tabIconLocalName
#	
#	set tabIconPwd to tabIconPwdBase & tabIconLocalName
#	
#	# download icon
#	if tabIcon = "" then
#		set iconString to ""
#	else
#		set foundImage to do shell script "ls " & tabIconPwdBase & " | grep " & tabIconLocalName & " || true"
#		if foundImage = "" then
#			#display dialog foundImage
#			do shell script "curl " & tabIcon & " -o " & tabIconPwd
#		end if
#		set iconString to "\"icon_path\":\"" & tabIconPwd & "\""
#	end if
#	
#	# return JSON
#	return "{" & titleString & "," & backgroundColor & "," & fontColor & "," & iconString & "}"
#else
#	return ""
#end if
1 Like