Last active
June 21, 2016 22:36
-
-
Save 1ec5/e0d414b1a2ec3eff098086d3691a4129 to your computer and use it in GitHub Desktop.
Open Selection in GitHub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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