Use a Zsh Function With AppleScript to Get the URL From Chrome

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

AppleScript is a powerful and strange language that allows you to interact with apps in a wide variety of ways. One feature it enables is the ability to get the URL from Chrome so that you can take the URL and do whatever you want with it.

clone(){
  cd ~/projects
  url=$(osascript -e 'tell application "Google Chrome" to return URL of active tab of front window')

  repo=${url:19}
  name=${repo:t}

  git clone git@github.com:$repo && code $name
}

John Lindquist: [0:00] We want to clone this URL from Chrome from our terminal. The way we get that URL is by using osascript, which can run AppleScript. We need to pass in the -e flag, or else it's going to look for a script on the computer. The statement looks like this, "Tell application Google Chrome to return URL of active tab of front window."

[0:24] When I run that, you'll see that we get our URL back. Let's store that in a variable. We're going to wrap this in parentheses so that it executes inside of there, and set URL equal to that, hit enter. Now if we echo our URL, you'll see we have the URL stored. We're going to want this chunk of text here so that we have the username and the project so that we can append it to get clone statement.

[0:48] We'll break apart that string using this syntax, URL at 19. I'll go ahead and echo that. I counted ahead of time that this is 19 characters. We're just removing that from the rest of this.

[1:01] We'll go ahead and assign repo to that. We'll check if we have our repo, which we do. Then, lastly, once it clones it to my folder, the name of the project is going to be promises-in-rxjs. I'm going to need this bit of text. I'm going to clear this out.

[1:17] Finally, from the repo, we can tell it to extract the tail. This syntax is called parameter expansion. It's going to find that last slash and take everything after it. We'll go ahead and echo that. You can see we have the project name.

[1:31] To put this all together, we will create a function named clone. We'll go into our projects directory. I'll copy and paste our URL command. Remember, that's the full URL. Then our repo will be the characters after the 19th character, so this is getting rid of github.com and https. Then the name of our project is the tail of the repo name, so tail of the repo name.

[1:55] Then we can go ahead and git clone git@github.com and then tack on the repo name. Once that's done, we can tell our IDE -- I'm using Visual Studio Code -- to open the name, since we'll be in our current project and this project will have cloned there. Code can just go ahead and open that.

[2:14] Once I go back to my terminal here, let's clear this out. I'll even go up a directory. I'll make sure that this is the frontmost window here and make sure to source my zshrc. Then, once I type clone, you'll see the whole process run through. It'll pop open code with everything cloned and ready to work on.

egghead
egghead
~ 11 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today