{"id":28,"date":"2014-09-04T15:38:32","date_gmt":"2014-09-04T15:38:32","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=28"},"modified":"2014-09-04T15:41:05","modified_gmt":"2014-09-04T15:41:05","slug":"from-filemaker-to-sql-with-applescript","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=28","title":{"rendered":"From FileMaker to SQL with Applescript"},"content":{"rendered":"<p>From FileMaker Pro to MSSQL or MYSQL using Applescript<\/p>\n<pre style=\"font:inherit;\">\r\n<code>\r\nset tablestoMake to \"\"\r\nset dbName to \"\"\r\n\r\nset mysql to true\r\nset mssql to false\r\nset sqlInsert to false\r\nset mssqlClasses to false\r\nset truncate to false\r\nset useDB to false\r\n\r\nset namespace to \"\" ---USED TO OUTPUT A BASIC C SHARP CLASS\r\n\r\ntell application \"FileMaker Pro\"\r\n    set output to \"\"\r\n    set myTables to the name of every table of database dbName\r\n    repeat with x from 1 to the count of every table of database dbName\r\n        --try\r\n        set myTable to item x of myTables\r\n        if (tablestoMake contains myTable or tablestoMake is \"\") then\r\n            set myTable to name of table myTable\r\n            set myDbTable to (dbName as string) & \"_\" & name of table myTable of database dbName\r\n            set myfields to the name of every field of table myTable of database dbName as list\r\n\r\n            set output to output & \"CREATE TABLE \" & myTable & return & tab & tab & \"(\" & return\r\n\r\n            if (mysql = true) then\r\n                repeat with i from 1 to the count of myfields\r\n                    set myfield to item i of myfields as string\r\n                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string\r\n                    if myfield is equal to \"id\" and myFieldType is equal to \"real\" then\r\n                        set output to output & tab & tab & tab & \"`\" & myfield & \"`\" & \" MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY\"\r\n                    else if myFieldType = \"real\" then\r\n                        set output to output & tab & tab & tab & \"`\" & myfield & \"`\" & \" INT\"\r\n                    else if myFieldType = \"date\" then\r\n                        set output to output & tab & tab & tab & \"`\" & myfield & \"`\" & \" VARCHAR (256)\"\r\n                    else if myFieldType = \"string\" then\r\n                        set output to output & \"   \" & tab & tab & tab & \"`\" & myfield & \"`\" & \" VARCHAR (2256)\"\r\n                    end if\r\n                    if (i < the (count of myfields)) then\r\n                        set output to output &#038; \",\" &#038; return\r\n                    else if (i = the (count of myfields)) then\r\n                        set output to output &#038; return\r\n                    end if\r\n                    set i to i + 1\r\n                end repeat\r\n                set output to output &#038; tab &#038; tab &#038; tab &#038; \") \" &#038; return &#038; tab &#038; tab &#038; tab &#038; \"ENGINE=MyISAM;\" &#038; return &#038; return\r\n            end if\r\n            if (mssql = true) then\r\n                repeat with i from 1 to the count of myfields\r\n                    set myfield to item i of myfields as string\r\n                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string\r\n                    if myfield is equal to \"id\" and myFieldType is equal to \"real\" then\r\n                        set output to output &#038; tab &#038; tab &#038; tab &#038; \"id INT NOT NULL PRIMARY KEY\"\r\n                    else if myFieldType = \"real\" then\r\n                        set output to output &#038; tab &#038; tab &#038; tab &#038; \"[\" &#038; myfield &#038; \"] INT\"\r\n                    else if myFieldType = \"date\" then\r\n                        set output to output &#038; tab &#038; tab &#038; tab &#038; \"[\" &#038; myfield &#038; \"] datetime\"\r\n                    else if myFieldType = \"string\" then\r\n                        set output to output &#038; \"   \" &#038; tab &#038; tab &#038; tab &#038; \"[\" &#038; myfield &#038; \"] NVARCHAR(2256)\"\r\n                    end if\r\n                    if (i < the (count of myfields)) then\r\n                        set output to output &#038; \", \" &#038; return\r\n                    else if (i = the (count of myfields)) then\r\n                        set output to output &#038; return\r\n                    end if\r\n                    set i to i + 1\r\n                end repeat\r\n                set output to output &#038; return &#038; tab &#038; tab &#038; \");\" &#038; return &#038; return\r\n            end if\r\n           \r\n            if (mssqlClasses = true) then\r\n                set output to \"using System.Web;\" &#038; return &#038; return\r\n                set output to output &#038; \"namespace \" &#038; namespace &#038; \".NS\" &#038; myTable &#038; return\r\n                set output to output &#038; \"{\" &#038; return\r\n                set output to output &#038; tab &#038; \"public class \" &#038; myTable &#038; return\r\n                set output to output &#038; tab &#038; \"{\" &#038; return\r\n               \r\n                repeat with i from 1 to the count of myfields\r\n                    set myfield to item i of myfields as string\r\n                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string\r\n                    if myfield is equal to \"id\" and myFieldType is equal to \"real\" then\r\n                        set output to output &#038; \"   \" &#038; tab &#038; tab &#038; tab &#038; \"public int \" &#038; myfield &#038; \"{ get; set; }\"\r\n                    else if myFieldType = \"real\" then\r\n                        set output to output &#038; \"   \" &#038; tab &#038; tab &#038; tab &#038; \"public int \" &#038; myfield &#038; \"{ get; set; }\"\r\n                    else if myFieldType = \"date\" then\r\n                        set output to output &#038; \"   \" &#038; tab &#038; tab &#038; tab &#038; \"public date \" &#038; myfield &#038; \"{ get; set; }\"\r\n                    else if myFieldType = \"string\" then\r\n                        set output to output &#038; \"   \" &#038; tab &#038; tab &#038; tab &#038; \"public string \" &#038; myfield &#038; \"{ get; set; }\"\r\n                    end if\r\n                    if (i < the (count of myfields)) then\r\n                        set output to output &#038; return\r\n                    else if (i = the (count of myfields)) then\r\n                        set output to output &#038; return\r\n                    end if\r\n                    set i to i + 1\r\n                end repeat\r\n                set output to output &#038; return &#038; tab &#038; \"}\" &#038; return &#038; return\r\n                set output to output &#038; return &#038; \"}\" &#038; return &#038; return\r\n                my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)\r\n            end if\r\n            if (sqlInsert = true) then\r\n                set output to \"INSERT INTO \" &#038; myTable &#038; \"(\"\r\n                repeat with i from 1 to the count of myfields\r\n                    if (i < the (count of myfields)) then\r\n                        set output to output &#038; (item i of myfields as string) &#038; \", \"\r\n                    else if (i = the (count of myfields)) then\r\n                        set output to output &#038; (item i of myfields as string) &#038; \") values (\"\r\n                    end if\r\n                    set i to i + 1\r\n                end repeat\r\n                set i to 1\r\n                repeat with i from 1 to the count of myfields\r\n                    set myfield to item i of myfields as string\r\n                    set myFieldType to default type of field (item i of myfields) of table myTable of database dbName as string\r\n                    if myFieldType = \"real\" then\r\n                        set output to output &#038; \" int \"\r\n                    else if myFieldType = \"string\" or myFieldType = \"date\" then\r\n                        set output to output &#038; \"string \"\r\n                    end if\r\n                    if (i < the (count of myfields)) then\r\n                        set output to output &#038; \", \"\r\n                    end if\r\n                    if (i = the (count of myfields)) then\r\n                        set output to output &#038; \");\" &#038; return\r\n                    end if\r\n                end repeat\r\n                my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)\r\n            end if\r\n        end if\r\n        --end try\r\n        set x to x + 1\r\n    end repeat\r\n    if (mssql = true or mysql = true) then\r\n        my saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)\r\n    end if\r\n    display dialog \"DONE\"\r\nend tell\r\n\r\non saveDoc(output, dbName, myTable, mssqlClasses, sqlInsert)\r\n    set the clipboard to output\r\n    set d to do shell script \"date '+%Y_%m_%d'\"\r\n    tell application \"TextWrangler\"\r\n        make new document\r\n        paste of document of window 1\r\n        if (mssqlClasses = false and sqlInsert = false) then\r\n            save document of window 1 to dbName &#038; \"_sections_headers_\" &#038; d &#038; \"_big.sql\"\r\n        else if (mssqlClasses = true) then\r\n            save document of window 1 to myTable &#038; \".cs\"\r\n        else if (sqlInsert = true) then\r\n            save document of window 1 to myTable &#038; \".sql\"\r\n        end if\r\n        --close document of window 1\r\n    end tell\r\nend saveDoc\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>From FileMaker Pro to MSSQL or MYSQL using Applescript set tablestoMake to &#8220;&#8221; set dbName to &#8220;&#8221; 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 &#8220;&#8221; &#8212;USED TO OUTPUT A BASIC C SHARP CLASS tell application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=28"}],"version-history":[{"count":4,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}