Skip to content

Command clicking URL fails to open or pass URL to Semantic History script

Hello, this is an ancillary bug relating to: #7635 (closed)

  • iTerm2 version: Build 3.3.0beta2
  • OS version: 10.12.6
  • Attach com.googlecode.iterm2.plist here (drag-drop from finder into this window)
  • Attach a debug log, if possible. Instructions at https://iterm2.com/debuglog debuglog.txt
  • Are you reporting a performance issue, excessive CPU usage, or a hang? No
  • Are you reporting a crash? No
  • Are you reporting excessive memory usage? No

Detailed steps to reproduce the problem:

  1. From command line: echo http://google.com
  2. Command Click http://google.com

What happened: No URL is passed to the Semantic History command as parameter $2 / \1

What should have happened: URL should be opened or passed to Semantic History command as positional parameter $2 / \1

Additional Details:

Semantic History => Run Command: /Users/jcuzella/bin/iterm_open_with \5 \1 \2

I'm using a modified iterm_open_with Semantic History script to detect the URL vs filename:num:col patterns and handle them appropriately on Command + Click. I have added some debug output echo commands to log what the script sees as passed positional parameters (also a good tongue twister 😝 ):

#!/bin/sh
# iterm_open_with - open a URL, file from CWD, full path, or path with linenumber in default app or Sublime Text if text file
#                   For usage with iTerm2:
#                   In iTerm's Preferences > Profiles > Default > Advanced > Semantic History,
#                   choose "Run command..." and enter "/your/path/to/iterm_open_with \5 \1 \2".
# Usage: iterm_open_with $(pwd) filename [linenumber]
# $(pwd) = current working directory (either use `pwd` or $PWD)
# filename = filename to open
# lineno = line number
pwd=$1
file=$2


echo "Semantic History Script Called:" >> /tmp/iterm_open_with.log
echo "$0 $1 $2 $3" >> /tmp/iterm_open_with.log
regex='https?://([a-z0-9A-Z]+(:[a-zA-Z0-9]+)?@)?[-a-z0-9A-Z\-]+(\.[-a-z0-9A-Z\-]+)*((:[0-9]+)?)(/[a-zA-Z0-9;:/\.\-_+%~?&@=#\(\)]*)?'
perl -e "if ( \"$file\" =~ m|$regex|) { exit 0 } else { exit 1 }"
if [ $? -ne 0 ]; then
  echo "NOT A URL: $2" >> /tmp/iterm_open_with.log
  # if it's not a url, try splitting by ':'
  arr=($(echo $2 | tr ':' "\n"))
  file=${arr[0]}
  lineno=${arr[1]:-$3}
  colno=${arr[2]:-${3##*:}}
  [ -e "$file" ] || file=${pwd}/${file}
fi

perl -e "if ( \"$file\" =~ m|$regex|) { exit 0 } else { exit 1 }"
[ $? -eq 0 ] && echo "URL DETECTED: $2" >> /tmp/iterm_open_with.log


file "$file" | grep -q "text"
if [ $? -ne 0 ]; then
  /usr/bin/open $file
else
  /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ${file}${lineno:+:${lineno}}${colno:+:${colno}}
fi

Script Log Result:

$ cat /tmp/iterm_open_with.log
Semantic History Script Called:
/Users/jcuzella/bin/iterm_open_with /private/tmp
NOT A URL:
Edited by James Cuzella