Suggestion to shortcut to open new tab in Chrome with predefined url with input

Hi, I'm looking for suggestion to this example in Chrome.

  1. Shortcut to open an input field
  2. Type "en" and hit enter
    2.1 If url contains anything between .com and /login (example.com/us/login) then replace us with en from input
    2.3 If url does not contain anything between .com and /login (example.com/login) then add /en in between .com and /login = example.com/en/login
  3. Open https://example.com/en/login in new tab

I'd like it to work like Spotlight. CMD + space > type app name > ENTER to open app.
But with the logic of replacing XX with whatever is being typed in the input field.

Is this even possible?

Since no one answers ... with my limited knowledge, I think it can not be done. You can try Keyboard Maestro. :man_shrugging:

I am pretty sure it can be done with a mix of AppleScript and Javascript

I made it work with apple script. Here is the code if anyone else will do something similar.

set startURL to "https://example.com/"
set endURL to "/login/"

set theCountrycode to ""

repeat while theCountrycode is ""
	set theCountrycode to ¬
		the text returned of ¬
			(display dialog "Enter the countrycode:" default answer ¬
				"" buttons {"Cancel", "OK"} default button 2)
end repeat

tell application "Google Chrome"
	activate
	tell front window to make new tab at after (get active tab) with properties {URL:startURL & theCountrycode & endURL} -- open a new tab after the current tab
end tell
1 Like

Yep I think using this sort of Apple Script is currently the most performant way to achieve this.
The Shortcuts app does not yet have an action to open a url in a new tab, otherwise that would have been my recommendation (it works well with BTT and getting & working with text it is really easy (and it will be the future of automation on macOS)).
If you want to minimize the use of Apple Script and use it only for opening the new tab:

1 Like

Thanks Andreas, that's an awesome solution I think it will always be best to use native solutions.
Changed mine to the one you provided using Shortcuts.