Created
February 14, 2012 16:00
-
-
Save bennadel/1827756 to your computer and use it in GitHub Desktop.
Building Executable Scripts For The Mac OSX Command Line With Node.js
This file contains hidden or 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
#! /usr/local/bin/node | |
// ^-- Tell the terminal which interpreter to use for the execution | |
// ^-- of this script. For our purposes, we'll be using the Node.js | |
// ^-- interpretor (which was installed in the local BIN directory | |
// ^-- using HomeBrew. | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Include the system utility library (for output). | |
var util = require( "util" ); | |
// Include the library for spinning up child processes. We'll need | |
// this to execute the "copy to clipboard" command. | |
var childProcess = require( "child_process" ); | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Build up a list of loving things to say to your loved one. Each | |
// phrase can be tagged for use in command-line filtering. | |
var phrases = []; | |
// Create some tags to be used for filtering. | |
phrases.GENERAL = 1; | |
phrases.SAPPY = 2; | |
phrases.NAUGHTY = 3; | |
phrases.PLAYFUL = 4; | |
// Populate the phrases. | |
phrases.push({ | |
text: "I love you so much!", | |
tag: phrases.GENERAL | |
}); | |
phrases.push({ | |
text: "Sometimes, I have to pinch myself, I'm so happy with you!", | |
tag: phrases.SAPPY | |
}); | |
phrases.push({ | |
text: "You are the love of my life!", | |
tag: phrases.SAPPY | |
}); | |
phrases.push({ | |
text: "I love you more than a kitten loves milk!", | |
tag: phrases.PLAYFUL | |
}); | |
phrases.push({ | |
text: "Hey shmoopy, I miss you.", // For Seinfeld fans. | |
tag: phrases.PLAYFUL | |
}); | |
phrases.push({ | |
text: "I can't stop thinking about your body!", | |
tag: phrases.NAUGHTY | |
}); | |
phrases.push({ | |
text: "I was just thinking of you and it made me smile :)", | |
tag: phrases.GENERAL | |
}); | |
phrases.push({ | |
text: "I wanna dip you in pudding and eat you for dessert!", | |
tag: phrases.NAUGHTY | |
}); | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Now that we have our phrase library populated, let's see if we | |
// need to filter the list using a command-line argument. By default, | |
// we'll be selecting from all of the phrases. | |
var filterArgument = (process.argv[ 2 ] || ""); | |
var filterTag = null; | |
// Check to see if the use has supplied a filter. | |
switch (filterArgument){ | |
case "": | |
filterTag = null; | |
break; | |
case "--general": | |
filterTag = phrases.GENERAL; | |
break; | |
case "--sappy": | |
filterTag = phrases.SAPPY; | |
break; | |
case "--naughty": | |
filterTag = phrases.NAUGHTY; | |
break; | |
case "--playful": | |
filterTag = phrases.PLAYFUL; | |
break; | |
// If we could not figure out what the command-line argument was, | |
// then something is incorrect. Exit out. | |
default: | |
util.puts( "Filter not recognized." ); | |
util.puts( "Use: --general, --sappy, --naughty, --playful" ); | |
// Exit out of the process (as a failure). | |
process.exit( 1 ); | |
break; | |
} | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// It's time to select the phrase. Since we'll need to select a | |
// random phrase from a pool of phrases, let's factor out the | |
// phrases first, then we'll select them. Start by assuming that | |
// we will not be filtering. | |
var filteredPhrases = phrases; | |
// Check to see if we need to filter. | |
if (filterTag){ | |
// Create a list of filtered phrases for the given tag. | |
filteredPhrases = phrases.filter( | |
function( phrase ){ | |
// Only include the phrase if the filter matches. | |
return( phrase.tag === filterTag ); | |
} | |
); | |
} | |
// Now, let's generate a random index from the array. | |
var targetIndex = ( | |
Math.floor( Math.random() * 1000 ) % filteredPhrases.length | |
); | |
// Get the target phrase. | |
var phrase = filteredPhrases[ targetIndex ]; | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Now that we have the phrase, we need to copy it to the clipboard. | |
// For this, we'll use the [pbcopy] command in a child process: | |
// | |
// $> echo "YOUR_SELECTED_PHRASE" | pbcopy | |
// | |
childProcess.exec( | |
("echo \"" + phrase.text + "\" | pbcopy"), | |
function( error, stdout, stderr ){ | |
// Check to see if the PastBoard copy worked. | |
if (error){ | |
// Something went wrong. | |
util.puts( | |
"Hmm, we could not copy \"" + phrase.text + | |
"\" to the clipboard." | |
); | |
// Output the error. | |
util.puts( "ERROR: " + stderr ); | |
} else { | |
// Woot! It all worked. | |
util.puts( | |
"\"" + phrase.text + | |
"\" has been copied to your clipboard!" | |
); | |
} | |
// Exit out with success!! | |
process.exit( 0 ); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment