Skip to content

Instantly share code, notes, and snippets.

@james-howard
Last active August 21, 2017 18:18
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 james-howard/6887542a389d93239a43acee2f12e393 to your computer and use it in GitHub Desktop.
Save james-howard/6887542a389d93239a43acee2f12e393 to your computer and use it in GitHub Desktop.
function run() {
xcode = Application('Xcode')
var app = Application.currentApplication()
app.includeStandardAdditions = true;
docs = xcode.sourceDocuments()
for (var i = docs.length-1; i >= 0; i--) {
var doc = docs[i];
var range = doc.selectedParagraphRange();
if (!range) {
continue;
}
var start = range[0];
var end = range[1];
if (start > end) {
var tmp = start;
start = end;
end = tmp;
}
var path = doc.path();
var basename = path.substring(path.lastIndexOf('/')+1);
var gitinfo = app.doShellScript(
`cd \`dirname '${path}'\`\n` +
`git remote -v | head -1\n` +
`git rev-parse --show-toplevel\n` +
`git rev-parse HEAD\n`
).split('\r');
var remote = gitinfo[0];
var gitroot = gitinfo[1];
var branch = gitinfo[2];
var relativePath = path.substring(gitroot.length+1);
var m = remote.match(/.*github.com\/(.*?)\/(.*?)\.git.*/);
if (m) {
var owner = m[1];
var repo = m[2];
var lineAnchor = `L${start}`;
if (end != start) {
lineAnchor += `-L${end}`;
}
var url = `https://github.com/${owner}/${repo}/blob/${branch}/${relativePath}#${lineAnchor}`;
app.openLocation(`ship+github://newissue/${owner}/${repo}?title=${escape(basename+":"+start)}&body=${escape(url)}`);
}
break;
}
}
@james-howard
Copy link
Author

Put at ~/Library/Scripts/Applications/Xcode and use with Script Menu (turn on via Script Editor.app's preferences)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment