How to Approach

I’m trying to figure out a way to create barcode file in Indesign. My problem is that the barcode font that we use makes 2 characters from one key stroke. (example: 00 is #, 01 is $, 02 is % and so on…) It would be nice to use an alpha numeric barcode font, but the machines that read the barcode needs the one that we are current using. So I’m thinking I need to make a database of some sort for applescript to determine what to use. So if my number is 0102 it will return $% or something like that.
Any ideas?

Thanks

I found I can use a line like this to do the job…


repeat
			try
				set mytext to search document for "01" replacing with "$"
				set mytext to search document for "02" replacing with "%"
				if mytext = {} then
					exit repeat
				end if
			on error
				exit repeat
			end try
		end repeat

I have 99 numbers to do. Is there a way to shorten this?

Thanks

Would something like this work?

property replacementList : {"$", "%"}

count replacementList
repeat with i from 1 to result
	try
		get text -2 thru -1 of ("0" & i)
		--set myText to search document for result replacing with (item i of replacementList)
		
		if mytext = {} then exit repeat
	on error
		exit repeat
	end try
end repeat

One way to do this would be using a couple of lists, fishheadtw.

As mentioned previously, I don’t use Indesign - so I’ll have to use pseudo-code to indicate the kind of thing I mean. (Since the values of the replace_list property below are also guesses, you may need to adjust them as necessary.)

[code]property search_list : {“00”, “01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, “10”, “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”, “25”, “26”, “27”, “28”, “29”, “30”, “31”, “32”, “33”, “34”, “35”, “36”, “37”, “38”, “39”, “40”, “41”, “42”, “43”, “44”, “45”, “46”, “47”, “48”, “49”, “50”, “51”, “52”, “53”, “54”, “55”, “56”, “57”, “58”, “59”, “60”, “61”, “62”, “63”, “64”, “65”, “66”, “67”, “68”, “69”, “70”, “71”, “72”, “73”, “74”, “75”, “76”, “77”, “78”, “79”, “80”, “81”, “82”, “83”, “84”, “85”, “86”, “87”, “88”, “89”, “90”, “91”, “92”, “93”, “94”, “95”, “96”, “97”, “98”, “99”}
property replace_list : {“#”, “$”, “%”, “&”, “'”, “(”, “)”, "", “+”, “,”, “-”, “.”, “/”, “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “:”, “;”, “<”, “=”, “>”, “?”, “@”, “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “[”, “\”, “]”, “^”, “_”, “`”, “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”, “{”, “|”, “}”, “~”, “”, “Ä”, “Ã…”, “Ç”, “É”, “Ñ”, “Ö”, “Ü”} ( modify as necessary *)

(* pseudo-code )
repeat with i from 1 to count search_list
search document for (my search_list’s item i) replacing with (my replace_list’s item i)
end repeat
(
/pseudo-code *)[/code]
However, it looks like there might be a pattern to the sequence of barcode characters. If that’s extendable throughout the range, you could simply try something like this:

to get_barcode for t
    set b to ""
    repeat with i from 1 to count t by 2
        set b to b & (ASCII character (35 + (t's text i thru (i + 1))))
    end repeat
    b
end get_barcode

get_barcode for "000102"
--> "#$%"

Bruce and Kia,

Thanks for the advice I have it working on one level. as long as the sequence is in order they change the right numbers, (like 00010203…) But as soon as I break the order it stops the sequence and gives me the text (like 010206).

Any ides?


property search_list : {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"}
property replace_list : {«data utxt0023», «data utxt0024», «data utxt0025», «data utxt0026», «data utxt0027», «data utxt0028», «data utxt0029», «data utxt002A», «data utxt002B», «data utxt002C», «data utxt002D», «data utxt002E», «data utxt002F», «data utxt0030», «data utxt0031», «data utxt0032», «data utxt0033», «data utxt0034», «data utxt0035», «data utxt0036», «data utxt0037», «data utxt0038», «data utxt0039», «data utxt003A», «data utxt003B», «data utxt003C», «data utxt003D», «data utxt003E», «data utxt003F», «data utxt0040», «data utxt0041», «data utxt0042», «data utxt0043», «data utxt0044», «data utxt0045», «data utxt0046», «data utxt0047», «data utxt0048», «data utxt0049», «data utxt004A», «data utxt004B», «data utxt004C», «data utxt004D», «data utxt004E», «data utxt004F», «data utxt0050», «data utxt0051», «data utxt0052», «data utxt0053», «data utxt0054», «data utxt0055», «data utxt0056», «data utxt0057», «data utxt0058», «data utxt0059», «data utxt005A», «data utxt005B», «data utxt005C», «data utxt005D», «data utxt005E», «data utxt005F», «data utxt0060», «data utxt0061», «data utxt0062», «data utxt0063», «data utxt0064», «data utxt0065», «data utxt0066», «data utxt0067», «data utxt0068», «data utxt0069», «data utxt006A», «data utxt006B», «data utxt006C», «data utxt006D», «data utxt006E», «data utxt006F», «data utxt0070», «data utxt0071», «data utxt0072», «data utxt0073», «data utxt0074», «data utxt0075», «data utxt0076», «data utxt0077», «data utxt0078», «data utxt0079», «data utxt007A», «data utxt007B», «data utxt007C», «data utxt007D», «data utxt007E», «data utxt00C4», «data utxt00C5», «data utxt00C7», «data utxt00C9», «data utxt00D1», «data utxt00D6», «data utxt00DC», «data utxt00E1»}

tell application "Adobe InDesign CS2"
	activate
	set FormDoc to make document
	tell document preferences of FormDoc
		set page width to "8.5 in"
		set page height to "11 in"
	end tell
	tell FormDoc
		try
			set LayerName to layer "Image layer" of FormDoc
		on error
			set LayerName to make layer with properties {visible:true, locked:false, name:"NEW"}
			delete layer 2
		end try
		set NewSpotColor to make color with properties {space:CMYK, color value:{0, 0, 0, 100}, name:"00"}
		delete color "C=100 M=0 Y=0 K=0"
		delete color "C=0 M=0 Y=100 K=0"
		delete color "C=0 M=100 Y=0 K=0"
		delete color "C=15 M=100 Y=100 K=0"
		delete color "C=75 M=5 Y=100 K=0"
		delete color "C=100 M=90 Y=10 K=0"
		
		set WinFormFrame to make text frame
		set the geometric bounds of WinFormFrame to {"10.5 in", "0 in", "11.1947 in", "2.5 in"}
		set contents of WinFormFrame to "00" & "01" & "03"
		set vertical justification of text frame preferences to bottom align
		set justification of characters of WinFormFrame to left align
		set applied font of characters of WinFormFrame to "Int Hr Lin P 48 Dl"
		set font style of characters of WinFormFrame to "Regular"
		set point size of characters of WinFormFrame to 48
		set fill color of characters of WinFormFrame to "00"
		set overprint fill of characters of WinFormFrame to true
		set vertical justification of text frame preferences of WinFormFrame to top align
	end tell
	repeat with i from 1 to count search_list
		try
			set mytext to search for (my search_list's item i) replacing with (my replace_list's item i)
			if mytext = {} then
				exit repeat
			end if
		on error
			exit repeat
		end try
	end repeat
end tell
end


One or two questions if I may, fishheadtw - before we go too much further down this road.

b[/b] I’m intrigued by what InDesign returns in response to the search for x replacing with y command. It looks as though this returns a list of some description - but what does the list (assigned in your script to the variable mytext) normally contain - and under what circumstances would it be empty? (I believe the statement if mytext = {} then exit repeat might be causing a premature exit.)

b[/b] How many bar codes appear in a document? Just one - or more?

b[/b] At what point is the initial text entered in the document? I’m assuming this is done by the set contents of WinFormFrame to “00” & “01” & “03” statement. Is this correct?

b[/b] Is there any reason why the converted text could not be entered directly (rather than by entering numeric characters first, and then converting them with the search & replace routine)?

I’m pretty sure that, if possible, pre-conversion would be a much more efficient approach, since it should involve iterating through only a few (3?) characters - rather than the 100 contained in the lists. To illustrate what I’m driving at, here’s a variation of the get_barcode handler that I posted earlier:

property replace_list : characters of ((«data utxt0023002400250026002700280029002A002B002C002D002E002F0030003100320033003400350036003700380039003A003B003C003D003E003F0040004100420043004400450046004700480049004A004B004C004D004E004F00500051005200530054» as Unicode text) & «data utxt00550056005700580059005A005B005C005D005E005F0060006100620063006400650066006700680069006A006B006C006D006E006F0070007100720073007400750076007700780079007A007B007C007D007E00C400C500C700C900D100D600DC00E1»)

to get_barcode for t
    set b to ""
    repeat with i from 1 to count t by 2
        set b to b & my replace_list's item (1 + (t's text i thru (i + 1)))
    end repeat
    b
end get_barcode

set current_barcode to get_barcode for "010206"
--> "$%)"

The handler could be called before the application tell statement - and then, if my assumption in b[/b] is correct, this…

set contents of WinFormFrame to "00" & "01" & "03"

… might be replaced by something like:

set contents of WinFormFrame to current_barcode

Please forgive the probing - I can’t test this lot for myself. :wink:

Kia,
What you are seeing is a small portion of a much larger script. The user will enter in numbers per job that will string together what will make the barcode. So in my original script.


set contents of WinFormFrame to "00" & "01" & "03"

Will be replace with something like…


set contents of WinFormFrame to bar_code

bar_code can be a combo of many numbers. So I think I need a property list for the script to look through and replace with.

You were right about the if mytext = {} then exit repeat
I took that out and it works fine.
Now I’m dealing with the fact that it coverts the number wrong. I’m thinking I need something to tell it to look at only 2 numbers at a time, it seems to look at it as a whole and makes some wierd conversions.

Thanks for the help
It is great to have people jump in and help a guy out.

Ok New issue, my search a replace works fine, but as it changes it keeps changing the number till it is incorrect. Example lets say my number is 5123920235 I need to replace only two digits at a time (51,23,92,02,35) then stop. The number can either be 8 digits or 10 depending on user input. Right now it will change two digits then change them again base on the new number.

Any ideas

Thanks

Grab the numbers to replace and step through them in groups of 2. Since you are creating the bar code from user input (right?) then you could do this before you set the contents of the text box.

I still believe that the best way forward is to convert an entire barcode and then enter the converted code, too. (The handler that I suggested above should be able to convert a barcode of pretty much any length for this purpose.)

Kai, Thanks again it took me a little time to figure it out, but you were right
on the money with your suggestion. I works great as far as I can see. I just had to figure out where
to put the code. My script is huge it builds a total of 15 indesign docs based of four questions.
Thanks again for your help.

I hope to be able to help people out in the future as I learn more.

Thanks for the feedback, fishheadtw. Delighted to hear that we got there in the end. :slight_smile:

Kai,
Question the covert before fuction of the get_barcode works great, till I add a repeat fuction into my script. It gives me an error doesn’t understand the get_barcode fuction. If i put the line outside the Indesign part it works, but I need it to change numbers based on user input string. I hope I’m explaining this properly.
Heres the code…


tell application "Adobe InDesign CS2"
		activate
		set FormDoc to make document
		tell document preferences of FormDoc
			set {page height, page width} to {"11 in", "8.5"}
		end tell
		tell FormDoc
			repeat with anItem in theList
				set inset_number to anItem as string
				
				count characters of inset_number
				if length of inset_number is 1 then
					set inset_code to text -2 thru -1 of ("00" & inset_number)
				end if
				if length of inset_number is 2 then
					set inset_code to inset_number
				end if
				if length of inset_number is 3 then
					set inset_code to text -4 thru -1 of ("0000" & inset_number)
				end if
				set the_code to product_number & win_size & inset_code
				set current_barcode to get_barcode for the_code
				
				try
					set LayerName to layer "Image layer" of FormDoc
				on error
					set LayerName to make layer with properties {visible:true, locked:false, name:inset_number}
				end try
				set SpotColor to text -2 thru -1 of ("00" & inset_number)
				set NewSpotColor to make color with properties {space:CMYK, color value:{0, 0, 0, 100}, name:SpotColor}
				set properties of NewSpotColor to {model:spot}
				
				set the_frame to make text frame
				set the geometric bounds of the_frame to {"10.3059 in", "0 in", "11 in", "2.5 in"}
				set contents of the_frame to start_code & current_barcode & end_code
				set vertical justification of text frame preferences to bottom align
				set justification of characters of the_frame to left align
				set applied font of characters of the_frame to "Int Hr Lin P 48 Dl"
				set font style of characters of the_frame to "Regular"
				set point size of characters of the_frame to 48
				set fill color of characters of the_frame to NewSpotColor
				set overprint fill of characters of the_frame to true
				set vertical justification of text frame preferences of the_frame to top align
			end repeat
			delete color "C=100 M=0 Y=0 K=0"
			delete color "C=0 M=0 Y=100 K=0"
			delete color "C=0 M=100 Y=0 K=0"
			delete color "C=15 M=100 Y=100 K=0"
			delete color "C=75 M=5 Y=100 K=0"
			delete color "C=100 M=90 Y=10 K=0"
			delete last layer
			set AppleScript's text item delimiters to tid
			
			set save_name to "BC NO " & inset_save as string
			set dest_path to ImpoFolder as string
			set FileName to dest_path & save_name
			save to FileName
		end tell
		close active document
	end tell


I’m guessing it can’t reference back to the property form inside the tell Indesign part of the script.

Thanks

Yeah - that’s about the size of it, fishheadtw.

InDesign’s problem is that it doesn’t know what the expression get_barcode for the_code means. Since it refers to a script handler, we need (when in an application tell block) to refer the statement back to the script - which can be done simply like this:

set current_barcode to my (get_barcode for the_code)

Thanks Kai,
I did figure it out over the weekend, while sitting on a plane
working out this script. Thanks again.