AppleScript CSV numbering table generator

Found a pretty big bug involving leading zeroes that I just had the chance to encounter which caused the script to silently die when run from the AppleScript menuextra, yet run (incorrectly) from Script Editor. Should be good.
[UPDATE] Changed to manually enter number up, prompt for total number of finished pieces instead of ending number, and prompts to reverse order the numbers in case you have a rip that won’t do it on output (multi copies using “VDP” for me).
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.

-- http://strawhousepig.net/

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 text returned of (display dialog "How many up will pieces be printed?" default answer "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 & "n" & n & ","
  end repeat
  set columns_text to my_trim(columns_text)
else
  return
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
  if item 1 of the_list is greater than last item of the_list then
    set row_jump_n to 0 - row_jump
  end if
  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_n
      end repeat
    else
      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_n
      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 my_start + (text returned of (display dialog "How many to number (finished pieces)?" & return & return & ¬
    "Note: There will be at least 1 number over to save as future starting reference." 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' = pad to match end number. 001-100" & return & ¬
    "'0' = no leading zeros. 1-100" & return & ¬
    "'1' = 0001-0100" default answer "Fill")
  if the_zeroes is in {"Fill", "fill"} then
    set the_zeroes to (count of characters of (my_end as text))
  else if the_zeroes is "0" then
    set the_zeroes to "0"
  else
    set the_zeroes to (count of characters of (my_end as text)) + the_zeroes
  end if
  set num_list to {}
  repeat with i from my_start to my_end
    set end of num_list to i
  end repeat
  --  log num_list
  set file_name to "numbering_table.csv"
  if button returned of (display dialog "Varible Data Printing (VDP) may not allow for record reversal. Choosing \"Reverse\" will reverse the order of the numbers." & return & return & "NOTE: Due to rounding there may be erroneous numbers on last sheet when reversed. Check your CSV file to be sure they are acceptable (not too many)." buttons {"Cancel", "Reverse", "Forward"} default button "Forward" with title "Sort Order") is "Reverse" then
    --    log "Reversing the numbers."
    set num_list to reverse of num_list
    --    log num_list
    set file_name to "numbering_table_reverse.csv"
  end if
  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 file_name
    --    (*
    set newFile to (open for access the_file with write permission)
    set eof newFile to 0
    write columns_text to newFile
    close access newFile
    --*)
    --    do shell script "echo " & quoted form of columns_text & " > " & quoted form of POSIX path of the_file
  end tell
end if

Leave a Reply

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