{"id":481,"date":"2007-03-23T17:25:40","date_gmt":"2007-03-24T00:25:40","guid":{"rendered":"http:\/\/10.0.1.201\/?p=481"},"modified":"2007-03-23T17:25:40","modified_gmt":"2007-03-24T00:25:40","slug":"applescript-csv-numbering-table-generator","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2007\/03\/23\/applescript-csv-numbering-table-generator\/","title":{"rendered":"AppleScript CSV numbering table generator"},"content":{"rendered":"<p><ins datetime=\"2021-04-27T19:46:44+00:00\">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.<\/ins><br \/>\n<ins datetime=\"2019-12-19T14:30:08+00:00\">[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&#8217;t do it on output (multi copies using &#8220;VDP&#8221; for me).<\/ins><br \/>\nAn AppleScript to generate a numbering table in comma separated value format. Works great with InDesign&#039;s data merge function.<br \/>\n<!--more--><br \/>\nThe 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.<\/p>\n<p>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 <em>piece1<\/em>, <em>piece2<\/em>, etc.), but can be easily modified to do more by adding to the &#8220;columns_list&#8221;.<\/p>\n<p>For usage of InDesign&#039;s data merge functions, search for &quot;data merge&quot; in InDesign&#039;s help.<\/p>\n<p><code>-- http:\/\/strawhousepig.net\/<\/p>\n<p>on my_trim(text_to_trim)<br \/>\n\tset the character_count to the number of characters of the text_to_trim<br \/>\n\tset the new_text to (characters 1 thru (the character_count - 1) of the text_to_trim) as string<br \/>\n\treturn new_text<br \/>\nend my_trim<\/p>\n<p>set columns_n to text returned of (display dialog \"How many up will pieces be printed?\" default answer \"1\" with title \"N-up\")<br \/>\nset columns_text to \"\"<br \/>\nif columns_n is not false then<br \/>\n\trepeat with n from 1 to columns_n<br \/>\n\t\tset columns_text to columns_text & \"n\" & n & \",\"<br \/>\n\tend repeat<br \/>\n\tset columns_text to my_trim(columns_text)<br \/>\nelse<br \/>\n\treturn<br \/>\nend if<\/p>\n<p>on zero_pad(the_input, the_zeroes)<br \/>\n\tset the_input to the_input as string<br \/>\n\tset n to count of characters in the_input<br \/>\n\tif n < the_zeroes then\n\t\trepeat until n = the_zeroes\n\t\t\tset the_input to \"0\" &#038; the_input\n\t\t\tset n to n + 1\n\t\tend repeat\n\tend if\n\treturn the_input\nend zero_pad\n\non my_sort(the_list, the_columns, the_zeroes)\n\tset return_this to \"\"\n\tset the_columns to the_columns as integer\n\tset row_jump to (round ((count of the_list) \/ the_columns) rounding up) as integer\n\tset row_jump_n to row_jump\n\tif item 1 of the_list is greater than last item of the_list then\n\t\tset row_jump_n to 0 - row_jump\n\tend if\n\trepeat with i from 1 to row_jump\n\t\tset first_cell to item i of the_list as integer\n\t\tset this_row to \"\" as text\n\t\tif the_zeroes is not \"0\" then\n\t\t\trepeat with c from 1 to the_columns\n\t\t\t\tset this_row to this_row &#038; zero_pad(first_cell, the_zeroes) &#038; \",\" as text\n\t\t\t\tset first_cell to first_cell + row_jump_n\n\t\t\tend repeat\n\t\telse\n\t\t\trepeat with c from 1 to the_columns\n\t\t\t\tset this_row to this_row &#038; first_cell &#038; \",\" as text\n\t\t\t\tset first_cell to first_cell + row_jump_n\n\t\t\tend repeat\n\t\tend if\n\t\tset return_this to return_this &#038; my_trim(this_row) &#038; return\n\tend repeat\n\treturn return_this\nend my_sort\n\nset my_start to text returned of (display dialog \"Enter the starting number\" default answer \"1\")\nif my_start is not \"\" then\n\tset my_end to my_start + (text returned of (display dialog \"How many to number (finished pieces)?\" &#038; return &#038; return &#038; \u00ac\n\t\t\"Note: There will be at least 1 number over to save as future starting reference.\" default answer \"10\"))\n\tset the_zeroes to text returned of (display dialog \"Leading zeroes. Enter count you need leading the end number \" &#038; \u00ac\n\t\t\"(start number will be padded to match).\" &#038; return &#038; return &#038; \u00ac\n\t\t\"'Fill' = pad to match end number. 001-100\" &#038; return &#038; \u00ac\n\t\t\"'0' = no leading zeros. 1-100\" &#038; return &#038; \u00ac\n\t\t\"'1' = 0001-0100\" default answer \"Fill\")\n\tif the_zeroes is in {\"Fill\", \"fill\"} then\n\t\tset the_zeroes to (count of characters of (my_end as text))\n\telse if the_zeroes is \"0\" then\n\t\tset the_zeroes to \"0\"\n\telse\n\t\tset the_zeroes to (count of characters of (my_end as text)) + the_zeroes\n\tend if\n\tset num_list to {}\n\trepeat with i from my_start to my_end\n\t\tset end of num_list to i\n\tend repeat\n\t--\tlog num_list\n\tset file_name to \"numbering_table.csv\"\n\tif button returned of (display dialog \"Varible Data Printing (VDP) may not allow for record reversal. Choosing \\\"Reverse\\\" will reverse the order of the numbers.\" &#038; return &#038; return &#038; \"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\n\t\t--\t\tlog \"Reversing the numbers.\"\n\t\tset num_list to reverse of num_list\n\t\t--\t\tlog num_list\n\t\tset file_name to \"numbering_table_reverse.csv\"\n\tend if\n\tset num_list to my_sort(num_list, columns_n, the_zeroes)\n\tset columns_text to columns_text &#038; return &#038; num_list as text\n\ttell application \"Finder\"\n\t\tset the_file to choose file name with prompt \"Save as\" default name file_name\n\t\t--\t\t(*\n\t\tset newFile to (open for access the_file with write permission)\n\t\tset eof newFile to 0\n\t\twrite columns_text to newFile\n\t\tclose access newFile\n\t\t--*)\n\t\t--\t\tdo shell script \"echo \" &#038; quoted form of columns_text &#038; \" > \" & quoted form of POSIX path of the_file<br \/>\n\tend tell<br \/>\nend if<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,28],"class_list":["post-481","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}