Skip to content

Instantly share code, notes, and snippets.

@1ec5
Last active June 21, 2016 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1ec5/e0d414b1a2ec3eff098086d3691a4129 to your computer and use it in GitHub Desktop.
Save 1ec5/e0d414b1a2ec3eff098086d3691a4129 to your computer and use it in GitHub Desktop.
Open Selection in GitHub
-- http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
tell application "Xcode"
set theLineNumbers to the last source document's selected paragraph range
set thePath to the last source document's path
set theRoot to do shell script "dirname '" & thePath & "'"
set theRepoURL to do shell script "cd '" & theRoot & "'; git remote get-url origin"
set theRepoRelativePath to do shell script "cd '" & theRoot & "'; cd $(git rev-parse --show-toplevel); git ls-files '" & thePath & "'"
set theHash to do shell script "cd '" & theRoot & "'; git rev-parse HEAD"
set theRepoURL to my replace_chars(theRepoURL, "git@github.com:", "https://github.com/")
set AppleScript's text item delimiters to ".git"
set theBlobURL to the front text item of theRepoURL & "/blob/" & theHash & "/" & theRepoRelativePath
set theAnchor to "L" & the first item in theLineNumbers
if (count of theLineNumbers) > 1 and the first item in theLineNumbers ≠ the last item in theLineNumbers then
set theAnchor to theAnchor & "-" & the last item in theLineNumbers
end if
tell application "System Events" to open location theBlobURL & "#" & theAnchor
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment