alfred-safari-history icon indicating copy to clipboard operation
alfred-safari-history copied to clipboard

AppleScript approach

Open nemeth-it opened this issue 4 years ago • 0 comments

I would recommend an Apple Script (/usr/bin/osascript) approach, instead of an bash/zsh one.

The following is a quick and dirty solution, it:

  1. Focuses Safari
  2. Opens URL in new Tab
  3. Sends key-stroke to Safari for next tab.
tell application "Safari"
	activate --focus Safari
	set the URL of (make new tab in window 1) to "{query}" --opens url in new tab
	
	-- send next tab key-stroke (works only once if current site not focuses input field)
	tell application "System Events"
		tell process "Safari" to key code 124 using {shift down, command down}
	end tell
end tell

But this solution isn't perfect, because if the current page focuses an input or text-field the key-strokes going lost in these fields instead taking an effect. I think we have to find out how to read the URLs of the open tabs to find the correct tab-index and target this one then directly with Apple Script.

Why this approach?

Because the Bash/ZSH variant opens a new windows instead a new tab. In every other browser it would be easy to open Safari via command line thanks to parameters which targeting a new tab. But for Safari I doesn't found a command-line solution yet, so I tried to work around that issue via AppleScript.

nemeth-it avatar Jan 25 '21 19:01 nemeth-it