Jump to content

[Solved] Jump to Home of Website.


Recommended Posts

Background

I often find myself landing deep within some website structure, e.g. http://www.thedomain.com/subpage/anothersubpage/imhere.

I'd now like to press a keyboard shortcut to jump up to http://www.thedomain.com. 

Let's call it an ease-of-life feature.

 

Problem

I've tried for several hours to get this workflow going, but have realised that my programming skills are too weak/non-existent.

 

Steps (according to my n00b mind)

  1. Get URL from active browser tab (this is the part that's running, thanks to input from this forum. It's using an Applescript)
  2. Analyse the URL and chop off everything after the third "/". (Using RegEx? I've seen similar things done with Ruby, but I know little to nothing about both topics)
  3. Replace current tab URL with "home URL", reload browser. (Not there yet...)

 

Questions

  • If feels wrong to use an Applescript and then send the output into a Ruby script for further processing. Is it possible to solve my problem using one language?
  • I've got horrible problems getting how variables get set up in a script and how to send them on in the workflow. Especially with different languages. Is there a good place to learn about this?

 

Any input to this problem is highly appreciated. Thanks!

Link to comment

Use this in a Run Script with /usr/bin/osascript (JS) as the language:

const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]
const frontmost_app = Application(frontmost_app_name)

const domain_regex = /(https?:\/\/[^/]*).*/

if (['Google Chrome','Google Chrome Canary','Chromium','Opera','Vivaldi'].indexOf(frontmost_app_name) > -1) {
  current_tab = frontmost_app.windows[0].activeTab
  current_tab.url = current_tab.url().replace(domain_regex, '$1')
} else if (['Safari','Safari Technology Preview','Webkit'].indexOf(frontmost_app_name) > -1) {
  current_tab = frontmost_app.documents[0]
  current_tab.url = current_tab.url().replace(domain_regex, '$1')
} else {
  throw new Error('You need a supported browser as your frontmost app')
}

 

22 minutes ago, eightohnine said:

I've got horrible problems getting how variables get set up in a script and how to send them on in the workflow. Especially with different languages. Is there a good place to learn about this?

 

This questions is too broad. To answer this, it’d be useful if you had a concrete question of something  you’re trying to do but don’t understand why it doesn’t work. That probably deserves a new post.

Link to comment

Wow. Just wow. That's exactly what I was looking for! Thanks for your swift and clean answer!

(Mind you, I still have to dig into your code to actually "get" how it works...)

 

13 minutes ago, vitor said:

This questions is too broad. To answer this, it’d be useful if you had a concrete question of something  you’re trying to do but don’t understand why it doesn’t work. That probably deserves a new post.

 

Yeah, I see what you mean after reading the question again... I'll rethink what *exactly* I want to know and make a post accordingly.

 

Thanks a ton!

Link to comment
  • vitor changed the title to [Solved] Jump to Home of Website.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...