AppleScript CSV numbering table generator

An AppleScript to generate a numbering table in comma separated value format. Works great with InDesign's data merge function.

The script will now prompt for leading zeroes. Hopefully the dialog instructions are clear enough. The default is to pad the start number to match the end number.

This started as a simple 1-up script for numbering tickets using a laser printer. This version will handle up to a 5-up layout (labeled piece1, piece2, etc.), but can be easily modified to do more by adding to the “columns_list”.

For usage of InDesign's data merge functions, search for "data merge" in InDesign's help.

set columns_list to {"1", "2", "3", "4", "5"}

on my_trim(text_to_trim)
  set the character_count to the number of characters of the text_to_trim
  set the new_text to (characters 1 thru (the character_count - 1) of the text_to_trim) as string
  return new_text
end my_trim

set columns_n to choose from list columns_list ¬
  with prompt "How many up will pieces be printed?" default items {"1"} with title "N-up"
set columns_text to ""
if columns_n is not false then
  repeat with n from 1 to columns_n
    set columns_text to columns_text & "piece" & n & ","
  end repeat
  set columns_text to my_trim(columns_text)
end if

on zero_pad(the_input, the_zeroes)
  set the_input to the_input as string
  set n to count of characters in the_input
  if n < the_zeroes then
    repeat until n = the_zeroes
      set the_input to "0" & the_input
      set n to n + 1
    end repeat
  end if
  return the_input
end zero_pad

on my_sort(the_list, the_columns, the_zeroes)
  set return_this to ""
  set the_columns to the_columns as integer
  set row_jump to (round ((count of the_list) / the_columns) rounding up) as integer
  set row_jump_n to row_jump
  repeat with i from 1 to row_jump
    set first_cell to item i of the_list as integer
    set this_row to "" as text
    if the_zeroes is not "0" then
      repeat with c from 1 to the_columns
        set this_row to this_row & zero_pad(first_cell, the_zeroes) & "," as text
        set first_cell to first_cell + row_jump
      end repeat
      repeat with c from 1 to the_columns
        set this_row to this_row & first_cell & "," as text
        set first_cell to first_cell + row_jump
      end repeat
    end if
    set return_this to return_this & my_trim(this_row) & return
  end repeat
  return return_this
end my_sort

set my_start to text returned of (display dialog ¬
  "Enter the starting number" default answer "1")
if my_start is not "" then
  set my_end to text returned of (display dialog ¬
    "Enter the ending number" default answer "10")
  set the_zeroes to text returned of (display dialog ¬
    "Leading zeroes. Enter count you need leading the end number " & ¬
    "(start number will be padded to match)." & return & return & ¬
    "'Fill' = fill to match end number. 001-100" & return & ¬
    "'0' = no leading zeroes. 1-100" default answer "Fill")
  if the_zeroes is in {"Fill", "fill"} then
    set the_zeroes to count of characters in my_end
  else if the_zeroes is "0" then
    set the_zeroes to "0"
    set the_zeroes to (count of characters in my_end) + the_zeroes
  end if
  set num_list to ""
  repeat with i from my_start to my_end
    set num_list to num_list & i as list
  end repeat
  set num_list to my_sort(num_list, columns_n, the_zeroes)
  set columns_text to columns_text & return & num_list as text
  tell application "Finder"
    set the_file to choose file name with prompt "Save as" default name "Number_table.csv"
    set newFile to (open for access the_file with write permission)
    set eof newFile to 0
    write columns_text to newFile
    close access newFile
  end tell
end if

Leave a Reply

Your email address will not be published. Required fields are marked *