mingsai

Numbers Create Table from TSV File

Feb 22nd, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. property useHeaders : true
  2.  
  3. tell application "Numbers"
  4.     activate
  5.     try
  6.         if not (exists document 1) then error number 1000
  7.         tell document 1
  8.             tell active sheet
  9.                 -- prompt user for the TSV file to read
  10.                 set thisTSVFile to ¬
  11.                     (choose file of type "public.tab-separated-values-text" with prompt ¬
  12.                         "Pick the TSV (tab separated values) file to import:")
  13.                
  14.                 -- read the data
  15.                 set thisTSVData to my readTabSeparatedValuesFile(thisTSVFile)
  16.                
  17.                 -- determine data structure
  18.                 set the dataGroupCount to the count of thisTSVData
  19.                 set the dataSetCount to the count of item 1 of thisTSVData
  20.                
  21.                 -- create read data summary
  22.                 set the infoMessage to ¬
  23.                     "The read TSV data is composed of " & dataGroupCount & ¬
  24.                     " groups, with each group containing " & dataSetCount & " items." & ¬
  25.                     return & return
  26.                
  27.                 -- prompt for desired data layout method
  28.                 set dialogMessage to ¬
  29.                     infoMessage & "Is the tabbed data to be grouped by row or column?"
  30.                 display dialog dialogMessage ¬
  31.                     buttons {"Cancel", "Row", "Column"} default button 1 with icon 1
  32.                 set the groupingMethod to the button returned of the result
  33.                
  34.                 if useHeaders is true then
  35.                     set dataGroupCount to dataGroupCount + 1
  36.                     set dataSetCount to dataSetCount + 1
  37.                     set startingAdjustment to 1
  38.                 else
  39.                     set startingAdjustment to 0
  40.                 end if
  41.                
  42.                 if groupingMethod is "Row" then
  43.                     set thisTable to ¬
  44.                         make new table with properties ¬
  45.                             {row count:dataGroupCount, column count:dataSetCount}
  46.                 else if groupingMethod is "Column" then
  47.                     set thisTable to ¬
  48.                         make new table with properties ¬
  49.                             {row count:dataSetCount, column count:dataGroupCount}
  50.                 end if
  51.                
  52.                 -- import the data
  53.                 if the groupingMethod is "Row" then
  54.                     repeat with i from 1 to the count of thisTSVData
  55.                         set thisDataGroup to item i of thisTSVData
  56.                         tell row (i + startingAdjustment) of thisTable
  57.                             repeat with q from 1 to the count of thisDataGroup
  58.                                 set the value of cell (q + startingAdjustment) to item q of thisDataGroup
  59.                             end repeat
  60.                         end tell
  61.                     end repeat
  62.                 else
  63.                     repeat with i from 1 to the count of thisTSVData
  64.                         set thisDataGroup to item i of thisTSVData
  65.                         tell column (i + startingAdjustment) of thisTable
  66.                             repeat with q from 1 to the count of thisDataGroup
  67.                                 set the value of cell (q + startingAdjustment) to item q of thisDataGroup
  68.                             end repeat
  69.                         end tell
  70.                     end repeat
  71.                 end if
  72.                
  73.             end tell
  74.         end tell
  75.     on error errorMessage number errorNumber
  76.         if errorNumber is 1000 then
  77.             set alertString to "MISSING RESOURCE"
  78.             set errorMessage to "Please create or open a document before running this script."
  79.         else
  80.             set alertString to "EXECUTION ERROR"
  81.         end if
  82.         if errorNumber is not -128 then
  83.             display alert alertString message errorMessage buttons {"Cancel"}
  84.         end if
  85.         error number -128
  86.     end try
  87. end tell
  88.  
  89. on readTabSeparatedValuesFile(thisTSVFile)
  90.     try
  91.         set dataBlob to (every paragraph of (read thisTSVFile))
  92.         set the tableData to {}
  93.         set AppleScript's text item delimiters to tab
  94.         repeat with i from 1 to the count of dataBlob
  95.             set the end of the tableData to ¬
  96.                 (every text item of (item i of dataBlob))
  97.         end repeat
  98.         set AppleScript's text item delimiters to ""
  99.         return tableData
  100.     on error errorMessage number errorNumber
  101.         set AppleScript's text item delimiters to ""
  102.         error errorMessage number errorNumber
  103.     end try
  104. end readTabSeparatedValuesFile
Advertisement
Add Comment
Please, Sign In to add comment