Jump to content

Sexy Code Snippet Management with Gists


Recommended Posts

Okay so I have this idea that I got from Nettuts+.  It's already a Sublime Text Package.  Here is a Demo of it.

 

1. I basically want to be able to look up my gists the same way I can with willfarrell's Github Workflow using my username and using a personal access token from github.

 

2. I want to be able to filter out all the gists that don't have the   #snippet   tag at the end of them.

 

3. Once I search and find the correct one, I want it to copy it to my clipboard and also paste the gist snippet whatever text editor/IDE/application my cursor is currently in.

 

Could anyone give me any advice on how to start this project? I really really want to learn and get better at making Alfred workflows so I think this would be a great little project!

 

I have some experience in PHP, Javascript/jQuery, HTML/CSS, and some with AppleScript but I wouldn't consider myself a heavy hitter in any just yet but I will learn what I need to.  I just need guidance.

 

Thanks for any advice!!

Link to comment

To start it out in the simplest way, do this:

 

Since you know PHP, use that.

 

Download a copy of David Ferguson's workflows library (see libraries and helpers thread pinned in this forum). Include that.

 

Create a workflow with just a script filter for now. In it, have it be bash, and just have the command be `php snippets.php`. Have no argument selected.

 

Create the snippets.php file in the workflow directory. Include David's workflows.

 

Start a "first run" function that checks to make sure the data directory exists, and, if not, create it. Make sure that the data directory contains a settings file. If not, create the file and open it in a text editor (just use some exec($cmd) functions to do this). It'll be easiest to have the settings file be some json (so you can use json_decode) with just a user: and key: fields.

 

Use those and query github to get a list of all of the gists, and bring those back into an array.

 

Use a foreach statement on each row, make sure that you have use the $key => $value in that statement:

  Do a preg_match on the name of the gist to see if "#snippet" is in the name. If there isn't a match, then unset($gists[$key]); You should then have all of your gists that match the snippet.

 

Use the add_result (or whatever the object method is) from the workflow library to add all of the results to the workflow object constructed (see how David's library works). Then "echo $w->toxml()"; That should provide you with the feedback. Use the raw URL of the gist as the argument.

 

Then, try to run the workflow. If you get a list of gists in Alfred, you're on the right track.

 

Now, go back and change the script filter to have "argument required" in there, and change the command to `php snippets.php {query}`. Now, look into having some fun with $argv processing in the PHP script in order to filter out more results.

 

Create a new action in Alfred that runs a script.

Have it be PHP. Then, have the contents akin to something like:

echo file_get_contents("{query}");

 

Then, add in a copy to clipboard action in Alfred. For the contents, have it be, simply {query}. Then check the box that says "automatically paste to ...".

 

That should be it.

Link to comment

Okay so I've been working on this workflow but I was wondering If I we could do a Google Hangout or Skype or something really briefly.  If not, no worries.  I'm just used to web development with PHP so it's harder diving into this.  Basically with Web Dev, I'm able to output stuff to the screen to see if I'm doing things correctly.  

 

Ie: I say  <?php echo "hello world!" ?> , refresh the page, and then I see hello world.

 

What I was going to check was to test it out, say... <?php echo $w->path(); ?> to see if it would return the path.  But I don't know were to see the output.  Do you see what I'm saying?

 

Also, with the exec() command.  Do I just put per say.. a bash script inside it?

 

ie:

$data_directory = "path/to/data_directory/";

 

# Make sure the data directory exists
// if the directory doesn't exist
if ( !file_exists( $data_directory ) && !is_dir( $data_directory ) )
{
     // Make a new directory
     exec( 'cd path/to/; mkdir data_directory/;' );
     exit;
}

 

To start it out in the simplest way, do this:

 

Since you know PHP, use that.

 

Download a copy of David Ferguson's workflows library (see libraries and helpers thread pinned in this forum). Include that.

 

Create a workflow with just a script filter for now. In it, have it be bash, and just have the command be `php snippets.php`. Have no argument selected.

 

Create the snippets.php file in the workflow directory. Include David's workflows.

 

Start a "first run" function that checks to make sure the data directory exists, and, if not, create it. Make sure that the data directory contains a settings file. If not, create the file and open it in a text editor (just use some exec($cmd) functions to do this). It'll be easiest to have the settings file be some json (so you can use json_decode) with just a user: and key: fields.

 

Use those and query github to get a list of all of the gists, and bring those back into an array.

 

Use a foreach statement on each row, make sure that you have use the $key => $value in that statement:

  Do a preg_match on the name of the gist to see if "#snippet" is in the name. If there isn't a match, then unset($gists[$key]); You should then have all of your gists that match the snippet.

 

Use the add_result (or whatever the object method is) from the workflow library to add all of the results to the workflow object constructed (see how David's library works). Then "echo $w->toxml()"; That should provide you with the feedback. Use the raw URL of the gist as the argument.

 

Then, try to run the workflow. If you get a list of gists in Alfred, you're on the right track.

 

Now, go back and change the script filter to have "argument required" in there, and change the command to `php snippets.php {query}`. Now, look into having some fun with $argv processing in the PHP script in order to filter out more results.

 

Create a new action in Alfred that runs a script.

Have it be PHP. Then, have the contents akin to something like:

echo file_get_contents("{query}");

 

Then, add in a copy to clipboard action in Alfred. For the contents, have it be, simply {query}. Then check the box that says "automatically paste to ...".

 

That should be it.

Edited by AleXander
Link to comment

Send me a PM, and we can setup a time to chat if you need. I'm in NYC, and I'm unscheduled for the next few days.

 

But a few tips:

 

You can see the output two ways: the first is to send it to a notification or large text with the {query} variable. But that's not too great to see the output.

 

The other thing you can do is open a terminal in the workflow folder and just run the script with the php command (php test.php). All the output will flow through the terminal, so you can see what's going on. This is one of the reasons that you see so many workflows with script filters that just invoke a script in the workflow directory.

 

Also, the TerminalFinder workflow makes navigating to the workflow folder in the terminal much nicer.

 

For the first-run, you could always use the native php functions to make the directory: $dir=$w->data; mkdir($dir);

 

If you were to take the bash route, then it's easiest to put everything on just one line (or in a variable) and then pipe it into the exec() function. I.e. $cmd = 'ps aux|grep caffeinate|grep -v grep'; echo exec($cmd);

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...