Category Archives: Uncategorized

UI-Router Dynamic States – Dynamically create states – List States

Writing boiler plate code for ui-router in angularjs isn’t required.

The code below dynamically creates states in ui-router the first time ui-router is called.

VIEWS, LIST, CREATE, VIEW, EDIT are constants defined in app.config.js.


$state.get(); // LISTS CURRENT STATES

// CONFIGURE STATES
var statesCtrls = [
{ state:'test', ctrl:"TestsCtrl" },
{ state:'tag', ctrl:"TagsCtrl" },
{ state:'tagType', ctrl:"TagTypesCtrl" }
];

function createState(stateCtrl){
var ctrl = stateCtrl.ctrl;
var state = stateCtrl.state;
var list = {
url: "/" + state ,
templateUrl: VIEWS + state + "-" + LIST + ".html",
controller: ctrl
};

var create = {
url: "/" + state + "/" + CREATE,
templateUrl: VIEWS + state + "-" + CREATE + ".html",
controller: ctrl
};

var view = {
url: "/" + state + "/:id",
templateUrl: VIEWS + state + "-" + VIEW + ".html",
controller: ctrl
};

var edit = {
url: "/" + state + "/" + EDIT + "/:id",
templateUrl: VIEWS + state + "-" + EDIT + ".html",
controller: ctrl
};

$stateProvider.state(state, list);
$stateProvider.state(state + "/" + CREATE, create);
$stateProvider.state(state + "/", view);
$stateProvider.state(state + "/" + EDIT + "/", edit);
}

angular.forEach(statesCtrls, function (stateCtrl) {
createState(stateCtrl);
});

$state.get(); // RELISTS CURRENT STATES

Express Path to Static Files – Bower Components – 404 Error

Don’t get suckered into using the following line to reference static files like I did.

app.set(express.static(__dirname + ‘/bower_components’));

One, it will often result in 404 errors and two it statically references directory separators.

Instead use Path.

Add this in the server file.

path = require(‘path’);

And this line where appropriate.

app.use(express.static(path.join(__dirname, ‘bower_components’)));

Use Sublime 3 from Command Line – Terminal – Mac

To create a short cut or symbolic link from the Sublime executable to the /usr/local/bin/ folder, type the following command.

$ ln -s “/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl” /usr/local/bin/subl

The shortcut name at the end of /usr/local/bin/ can be anything you want. Sublime itself, uses subl, on my machine the shortcut is sublime.

After this is done, issuing the following command from the command line will open the current folder in sublime.

$ sublime .

Get Shutter Actuations from a DSLR Free

I found it very frustrating that Canon did not provide a free app or in camera feature to handle this simple task. In comes an open source project called gPHoto2.

“gPhoto2 is a free, redistributable, ready to use set of digital camera software applications for Unix-like systems, written by a whole team of dedicated volunteers around the world. It supports more than 1800 cameras.”

The installation process is fairly simple if HomeBrew is installed, so make sure you have that installed. HomeBrew will take care of installing gPhoto2’s required dependencies.

http://brew.sh/

Open Terminal and run the following command.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Once HomeBrew is installed, plug in your camera via USB and turn it on.

In Terminal, run the following command. This will take a few minutes.

brew install gphoto2; killall PTPCamera; gphoto2 --get-config /main/status/shuttercounter;

When the process completes, you should see something like the following.

Label: Shutter Counter
Type: TEXT
Current: 874

From SQL to FileMaker with Applescript


-- FIELD NAMES WERE EXPORTED FROM SQL INTO FIRST LINE OF CSV
-- DONT ASK WHY WERE GOING FROM SQL TO FMP
-- THEN AGAIN IF YOU NEED IT, HERE IT IS

set dbName to ""
set tableNames to {""}
set fieldNamesList to {""}

repeat with i from 1 to the count of tableNames
    set tableName to item i of tableNames as string
    set fieldNames to item i of fieldNamesList as list
    createTable(dbName, tableName, fieldNames)
    set i to i + 1
end repeat


on createTable(dbName, tableName, fieldNames)
    tell application "FileMaker Pro"
        activate
        tell database dbName
            tell table tableName
                tell application "System Events"
                    tell process "FileMaker Pro"
                        keystroke "d" using {command down, shift down}
                        tell window 1
                            --- MAKE TABLE
                            tell tab group 1
                                click radio button 0
                            end tell
                            set the clipboard to tableName as text
                            keystroke "v" using command down
                            delay 1
                            keystroke return
                            --- MAKE FIELDS
                            tell tab group 1
                                click radio button 2
                            end tell
                            repeat with i from 1 to the count of fieldNames
                                set fieldName to item i of fieldNames as string
                                set the clipboard to fieldName as text
                                keystroke "v" using command down
                                delay 1
                                keystroke return
                                set i to i + 1
                            end repeat
                            click button "OK"
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end createTable


From FileMaker to SQL with Applescript

From FileMaker Pro to MSSQL or MYSQL using Applescript


set tablestoMake to ""
set dbName to ""

set mysql to true
set mssql to false
set sqlInsert to false
set mssqlClasses to false
set truncate to false
set useDB to false

set namespace to "" ---USED TO OUTPUT A BASIC C SHARP CLASS

tell application "FileMaker Pro"
    set output to ""
    set myTables to the name of every table of database dbName
    repeat with x from 1 to the count of every table of database dbName
        --try
        set myTable to item x of myTables
        if (tablestoMake contains myTable or tablestoMake is "") then
            set myTable to name of table myTable
            set myDbTable to (dbName as string) & "_" & name of table myTable of database dbName
            set myfields to the name of every field of table myTable of database dbName as list

            set output to output & "CREATE TABLE " & myTable & return & tab & tab & "(" & return

            if (mysql = true) then
                repeat with i from 1 to the count of myfields
                    set myfield to item i of myfields as string
                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string
                    if myfield is equal to "id" and myFieldType is equal to "real" then
                        set output to output & tab & tab & tab & "`" & myfield & "`" & " MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY"
                    else if myFieldType = "real" then
                        set output to output & tab & tab & tab & "`" & myfield & "`" & " INT"
                    else if myFieldType = "date" then
                        set output to output & tab & tab & tab & "`" & myfield & "`" & " VARCHAR (256)"
                    else if myFieldType = "string" then
                        set output to output & "   " & tab & tab & tab & "`" & myfield & "`" & " VARCHAR (2256)"
                    end if
                    if (i < the (count of myfields)) then
                        set output to output & "," & return
                    else if (i = the (count of myfields)) then
                        set output to output & return
                    end if
                    set i to i + 1
                end repeat
                set output to output & tab & tab & tab & ") " & return & tab & tab & tab & "ENGINE=MyISAM;" & return & return
            end if
            if (mssql = true) then
                repeat with i from 1 to the count of myfields
                    set myfield to item i of myfields as string
                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string
                    if myfield is equal to "id" and myFieldType is equal to "real" then
                        set output to output & tab & tab & tab & "id INT NOT NULL PRIMARY KEY"
                    else if myFieldType = "real" then
                        set output to output & tab & tab & tab & "[" & myfield & "] INT"
                    else if myFieldType = "date" then
                        set output to output & tab & tab & tab & "[" & myfield & "] datetime"
                    else if myFieldType = "string" then
                        set output to output & "   " & tab & tab & tab & "[" & myfield & "] NVARCHAR(2256)"
                    end if
                    if (i < the (count of myfields)) then
                        set output to output & ", " & return
                    else if (i = the (count of myfields)) then
                        set output to output & return
                    end if
                    set i to i + 1
                end repeat
                set output to output & return & tab & tab & ");" & return & return
            end if
           
            if (mssqlClasses = true) then
                set output to "using System.Web;" & return & return
                set output to output & "namespace " & namespace & ".NS" & myTable & return
                set output to output & "{" & return
                set output to output & tab & "public class " & myTable & return
                set output to output & tab & "{" & return
               
                repeat with i from 1 to the count of myfields
                    set myfield to item i of myfields as string
                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string
                    if myfield is equal to "id" and myFieldType is equal to "real" then
                        set output to output & "   " & tab & tab & tab & "public int " & myfield & "{ get; set; }"
                    else if myFieldType = "real" then
                        set output to output & "   " & tab & tab & tab & "public int " & myfield & "{ get; set; }"
                    else if myFieldType = "date" then
                        set output to output & "   " & tab & tab & tab & "public date " & myfield & "{ get; set; }"
                    else if myFieldType = "string" then
                        set output to output & "   " & tab & tab & tab & "public string " & myfield & "{ get; set; }"
                    end if
                    if (i < the (count of myfields)) then
                        set output to output & return
                    else if (i = the (count of myfields)) then
                        set output to output & return
                    end if
                    set i to i + 1
                end repeat
                set output to output & return & tab & "}" & return & return
                set output to output & return & "}" & return & return
                my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)
            end if
            if (sqlInsert = true) then
                set output to "INSERT INTO " & myTable & "("
                repeat with i from 1 to the count of myfields
                    if (i < the (count of myfields)) then
                        set output to output & (item i of myfields as string) & ", "
                    else if (i = the (count of myfields)) then
                        set output to output & (item i of myfields as string) & ") values ("
                    end if
                    set i to i + 1
                end repeat
                set i to 1
                repeat with i from 1 to the count of myfields
                    set myfield to item i of myfields as string
                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string
                    if myFieldType = "real" then
                        set output to output & " int "
                    else if myFieldType = "string" or myFieldType = "date" then
                        set output to output & "string "
                    end if
                    if (i < the (count of myfields)) then
                        set output to output & ", "
                    end if
                    if (i = the (count of myfields)) then
                        set output to output & ");" & return
                    end if
                end repeat
                my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)
            end if
        end if
        --end try
        set x to x + 1
    end repeat
    if (mssql = true or mysql = true) then
        my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)
    end if
    display dialog "DONE"
end tell

on saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)
    set the clipboard to output
    set d to do shell script "date '+%Y_%m_%d'"
    tell application "TextWrangler"
        make new document
        paste of document of window 1
        if (mssqlClasses = false and sqlInsert = false) then
            save document of window 1 to dbName & "_sections_headers_" & d & "_big.sql"
        else if (mssqlClasses = true) then
            save document of window 1 to myTable & ".cs"
        else if (sqlInsert = true) then
            save document of window 1 to myTable & ".sql"
        end if
        --close document of window 1
    end tell
end saveDoc

Project Configuration files

At the root of any good app is a set of good configuration files. This is the path configuration file from the minimalistic PHP framework I created for my freelance work. It is one of the first files loaded and is used to set the primary paths that are used throughout the code base.

<?php
// UNCOMMENT FOR DEVELOPMENT
//error_reporting(E_ALL);

// PATH DEFINITIONS
// TELL PHP THE DIRECTOR SEPARATOR FOR WINDOWS == \ UNIX == /
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

// UNCOMMENT THE LINE BELOW TO GET THE CORRECT WORKING DIRECTORY PATH
//echo getcwd();

//PATH TO THE SITE ROOT
defined('SITE_ROOT') ? null : define('SITE_ROOT',str_replace("system", "", getcwd()));

//PATH TO SYSTEM FILES
defined('SYSTEM_PATH') ? null : define('SYSTEM_PATH', SITE_ROOT.DS.'system'.DS);
defined('SYSTEM_INCLUDES_PATH') ? null : define('SYSTEM_INCLUDES_PATH', SITE_ROOT.DS.'system'.DS.'includes'.DS);
defined('SYSTEM_CLASSES_PATH') ? null : define('SYSTEM_CLASSES_PATH', SITE_ROOT.DS.'system'.DS.'classes'.DS);

// PATH TO APP FILES
defined('APP_PATH') ? null : define('APP_PATH', SITE_ROOT.DS.'app'.DS);
defined('APP_IMAGES_PATH') ? null : define('APP_IMAGES_PATH', SITE_ROOT.DS.'app'.DS.'images'.DS);
defined('INCLUDES_PATH') ? null : define('INCLUDES_PATH', SITE_ROOT.DS.'app'.DS.'includes'.DS);
defined('CLASSES_PATH') ? null : define('CLASSES_PATH', SITE_ROOT.DS.'app'.DS.'classes'.DS);
defined('CONTROLLERS_PATH') ? null : define('CONTROLLERS_PATH', SITE_ROOT.DS.'app'.DS.'controllers'.DS);
defined('MODELS_PATH') ? null : define('MODELS_PATH', SITE_ROOT.DS.'app'.DS.'models'.DS);
defined('VIEWS_PATH') ? null : define('VIEWS_PATH', SITE_ROOT.DS.'app'.DS.'views'.DS);
defined('JS_PATH') ? null : define('JS_PATH', SITE_ROOT.DS.DS.'app'.'js'.DS);
defined('CSS_PATH') ? null : define('CSS_PATH', SITE_ROOT.DS.'app'.DS.'css'.DS);

?>