## Processing the Environment Variables

The main part of any PopClip extension is to take the variables passed in the environment and make something useful with it.

This section of code will fetch the verse from the King James version if it was set in the preferences (the $qKJV variable) or the command key was pressed while selecting the PopClip action ($keycode is 1048576). It will also request it if both control and command keys are pressed ($keycode is 1310720). Since the$qKJV variable is a string that is 1 or 0, you can check for the first character to be a character 1. That way, it does not have to process to a number. That can save some time!

If the condition is true, then the verses are retrieved using the getBibleVerse() function and appended to the $result string. Here, it is similar. If the Thai King James version was selected in the preferences ($qThaiKJV) or the control key was pressed while selecting the PopClip action ($keycode is 262144), then the Thai King James version will be requested. It will also request it if both control and command keys are pressed ($keycode is 1310720).

If the condition is true, then the verses are retrieved using the getBibleVerse() function and appended to the $result string. Here, the results need to be echoed. But, if the results buffer is empty, then the verse should be echoed. If an empty string is echoed, it effectively deletes what was selected. Since everything is an option, it is possible to get here with the $results variable empty. Good programming practices tells us to always take each possibility into account.

## Usage

Once the extension is loaded into PopClip and the preferences are set, then the extension is easy to use. Just select some text that is a Bible reference.

When the PopClip bar appears, select the white bible and the selection will be changed into the verse. Here, the preferences were set up for both KJV and ThaiKJV versions to be printed.

If the text does not match the regular expression for a Bible reference, then the Bible icon will not be shown.

This helps to save real-estate on the PopClip bar. Whenever you create an extension, think about how to minimize unnecessary appearances of your extension.

## Summary

Now you have a neat little PopClip extension for getting verses from a web site and pasting them in to your application. Even better than that, you now know how to create a preferences panel for your extension, get to that information from your extension, manipulate the selected text, and paste it back into the top most application. You can take this code and expand it or use it as a skeleton for your own PopClip extension. Let everyone know how you use it in the comments!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.