Xcode quick action scripts to jump to GitHub or Bitbucket repository

Stumbled upon this handy little macOS quick action script project in GitHub which support opening the code selected in Xcode in a browser so that the selected code file in GitHub repository is opened and selected lines are highlighted in the browser. Or you could copy the link from Xcode to GitHub hosted project, while from Xcode, and share the link to someone by email or whatever.

Image from the original repo by wojteklu

Neat, wanted to try it out and cloned the project and installed the action scripts. That was easy, just double click on the scripts and they are installed.

I tried this with a project that is actually hosted in Bitbucket.org. Obviously it didn’t work out well. I forked the project, cloned it and copied and opened the action scripts in Automator app, edited the scripts to fit Bitbucket and now I have another pair of scripts which do work also with Bitbucket hosted repositories!

The changes to the scripts were quite easy to implement. Just change .com to .org, and instead of GitHub’s “blob” path, use the Bitbucket’s “src” path for the code files. And instead of addressing code lines the GitHub way (#L54-L58), use the Bitbucket’s way (#lines-33:35). Here’s an example link to a GitHub repo…

https://github.com/anttijuu/PluginDllDemo/blob/0f7b83f7ada5613cb0561a63a8c7d09f95f46144/EasyCryptoReversePlugin/EasyCryptoPlugin.cpp#L54-L58

… and here’s an example of a Bitbucket link (note: these links to BitBucket project ohar2014 are no longer valid since I have chopped the project into separate components in separate repositories):

https://bitbucket.org/anttijuu/ohar2014/src/e6fd26ef3d142d3da0cbe5969e6f037e465cede5/BaseLayer/ConfigurationDataItem.cpp#lines-33:35

The long code in both of the links is the current commit id of the version I’m viewing in Xcode. And here’s the relevant parts of my new shiny Action script source code…

on run {input, parameters}
	tell application "Xcode"
		set activeDocument to document 1 whose name ends with (word -1 of (get name of window 1))
		set activeDocumentPath to path of activeDocument
		set currentLines to selected paragraph range of activeDocument
		set startLine to get item 1 of currentLines
		set endLine to get item 2 of currentLines
	end tell
	
	set repositoryUrl to do shell script "cd $(dirname " & activeDocumentPath & "); git remote -v | grep fetch | awk '{print $2}' | sed 's/git@/https:\\/\\//' | sed 's/org:/org\\//' | sed 's/\\.git//'"
	
	set pathToRepositoryDirLocation to do shell script "cd $(dirname " & activeDocumentPath & "); git rev-parse --show-toplevel "
	
	set activeFilePathInRepository to replace_text(activeDocumentPath, pathToRepositoryDirLocation, "")
	
	set currentCommit to do shell script "cd $(dirname " & activeDocumentPath & "); git rev-parse HEAD"
	
	set bitbucketUrl to repositoryUrl & "/src/" & currentCommit & activeFilePathInRepository & "#lines-" & startLine & ":" & endLine
	
	set the clipboard to bitbucketUrl
	
	return input
end run

…and the script itself installed and visible in the Xcode menu!

Action scripts installed for Bitbucket.

I also made a pull request to the original project, in case he wants to merge my contribution into it.