Skip to content

Instantly share code, notes, and snippets.

@simon360
Last active March 18, 2018 19:32
Show Gist options
  • Save simon360/12037dcbe59dae3565cdc91006a6dbf6 to your computer and use it in GitHub Desktop.
Save simon360/12037dcbe59dae3565cdc91006a6dbf6 to your computer and use it in GitHub Desktop.
Open the specified files in Atom, and if any are in a git repository, open that repository in the sidebar. If it's not in a git repository, open the containing folder in the sidebar.
#!/bin/bash
# Open the specified files in Atom, and if any are in a git repository, open
# that repository in the sidebar. If it's not in a git repository, open the
# containing folder in the sidebar.
# TODO: currently, will only open the project for the first file; further files
# will be opened in the same window, even if their repository differs.
# Can load into Automator on macOS, and add it as a service to the Finder.
# 1. Open Automator.
# 2. Create a Service.
# 3. Set "Service receives selected" to "files or folders".
# 4. Set "in" to "Finder".
# 5. Add a "Run shell script" action.
# 6. Set "Pass input" to "as arguments".
# 7. Paste this script in the editor.
# 8. Save.
# The script will appear under "Services" when you right click a file in the
# Finder.
# CONFIGURATION
# Whether to check for a repo/containing folder.
open_repo_as_project=1;
# Where the `atom` command is located
atom_command=/Applications/Atom.app/Contents/Resources/app/atom.sh
# // END CONFIGURATION
if [ $# == 0 ]; then
echo "Usage: open-in-atom <file1> [<file2> ...]";
exit 1;
fi;
for to_open in $@
do
# Change to the directory of the file, so we can read the git repo
cd `dirname $to_open`;
# Create an empty array
declare -a items_to_open;
# Check config
if [ $open_repo_as_project == 1 ]; then
# Check if we're in a git repository
if git status &>/dev/null; then
# If we are, add the git repository to the `atom` arguments
items_to_open+=(`git rev-parse --show-toplevel`);
else
items_to_open+=(`dirname $to_open`)
fi;
fi;
# Add the passed argument to this script to the arguments we send to `atom`
items_to_open+=($to_open);
done;
# Do the thing
$atom_command ${items_to_open[@]};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment