trying to use a number with leading zero's and failing...

Dear all,

I have a need to pass a variable number to another app.

I set the variable to look like this
set the mrt to 0012345678
when i save the script, applescript removes the leading zero’s and I get
set the mrt to 12345678

if i use
set the mrt to 205312345678
i get
set the mrt to 2.0531234567E+10

I have never seen this behaviour before. Whats going on?

BTW . The other app needs to receive the entire number, with the leading zeros.
I have tried sending the number as text and the receiving program does not recognise it.

Thanks for reading this:(

Hi.

I get 2.05312345678E+11. :slight_smile: What system are you using? 205312345678 is larger than the maximum positive integer that AppleScript can handle, so it’s compiled as a real: 205312345678.0. Very large and very small reals are presented on screen in “E” notation, but it makes no difference to the numeric value.

A number’s just a number value. Leading zeros only appear in text. Maybe your other app’s expecting a different text encoding.

Nigel, thanks for your reply.

The client app is quite dumb and simply generates a barcode. Whatever numeric value it sees it turns into a code. It’s a pure mapping operation the value of the number is ignored. This digit = that combination of bars. It’s a numeric only code so any text causes an error.

It sees a zero and generates a specific set of bars, then then does the same each digit and concatenates all the sets and saves the code as an image. It does no analysis of the number or calc or anything…

The service that will read the code needs a fixed length field and the leading zeros mean something so I have to have them.

Take on board what you said about encoding. If I just type the digits into the barcode app , 005312345678, it will accept them and work. If I type the same digits into text edit , then manually copy and then paste them into the barcode app it works. At the moment it is AS refusal to allow numeric leading zeros is the problem.

I wonder if a JS sub might do it. I will keep trying.

Thanks again,

Max

Nigel,

I am trying something truly crude here and telling system events to cut and paste from text edit, because the target app WILL accept the number when copied and pasted from textEdit.

It seems I can use applescript to produce a string of 005312345678 as a string. Then i can paste that into text edit. Then i select it in textEdit and paste it into the target app.

That works, but I will have to do better than that!

It gets me out of the hole today, but i need to improve on that!

Thanks, Max

Nigel, me again.

I had not noticed, but the barcode app supplier last update gves me the option of using an imported .CSV file.

I would think that makes all possible.

Max

Yes. Text copied to the clipboard from TextEdit gets stored there in a variety of encodings from which your other app can take its pick. AppleScript uses UTF-16 internally. But even writing AppleScript text directly to the clipboard produces (on my Mojave Systems) versions in ‘Unicode text’ (UTF-16BE), ‘string’ (MacRoman), UTF-8 and «class ut16» (which is UTF-16LE on Intel machines):

set the clipboard to "005312345678"
the clipboard as record
--> {Unicode text:"005312345678", string:"005312345678", scrap styles:«data styl01000000000010000E00030000000C00000000000000», «class utf8»:"005312345678", «class ut16»:"005312345678"}

I’d have thought one of these would have suited your other app. :confused: Copying plain text from TextEdit produces the same result without the scrap styles. Copying RTF text from TextEdit, I get the AppleScript result plus RTF data and ‘uniform styles’. How have you been transferring data from the script to the app (apart from apparently passing a real instead of text)?

I believe that CSV files generally have UTF-8 endcoding, although they needn’t necessarily do so, so it’s potentially the same probem with them.

Nigel,

I am a bit committed elsewhere so I am behind on this.

but

I can send my numbers as a string of text for each code to the .csv file. As in the script you sent me years back, i can keep appending to the end of the CSV to make my list of code payloads.

The barcode app has two modes of input. A single code at a time dialogue box that I just type into.
In a loop;
I was using AS to paste into this, one code at a time,
then export to file.
I end up with folder full of files.

This morning i discovered that it ALSO has a CSV importer built in. This happily reads a .csv which was constructed by AS. I have already been able to manage the import controls from AS.
So I construct my list via AS and then tell the barcode app to import and then generate the CSV.

Clearly, the barcode app uses a different method of validating an import to that of the single dialog box.

I’ll write and error trap my script for this then publish it back ASAP.

Thanks

Max

Skimming this thread, it sounds like you wish to take a number and obtain a text representation of it, padded with leading zeroes as needed.

I saw “005312345678” in one of your posts, so I’ve used this as a guide and am assuming you require a 12-digit long result.[format]to stringFromNumber:(N as number)
local N
return N as miles as string
end stringFromNumber:

to addLeadingZeroes to N
local N

    set twelveZeroes to "000000000000"
    if N's class ≠ text then set N to stringFromNumber_(N)
    if the length of N ≥ 12 then return N

    return text -1 thru -12 of (twelveZeroes & N)

end addLeadingZeroes[/format] With those handlers inserted into a script, hopefully, you can process a number like so:

set the clipboard to (addLeadingZeroes to 5312345678)
return the clipboard