Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Importing space delimited iPad text files into number

Lots of iPad app collect data as space delimited text files ( where is tab key on iPad) how can I import these files into numbers for analysis and plotting?

Posted on Apr 15, 2012 5:51 PM

Reply
Question marked as Best reply

Posted on Apr 15, 2012 6:07 PM

Provided there are no spaces in the data itself, you could:

  • import the data into a text processor or word processor application.
  • use find/replace to replace the spaces with tabs.
  • copy the data.
  • paste into a Numbers table.


Regards,

Barry

19 replies

Apr 17, 2012 11:09 PM in response to VerusEx

I would be surprised to see an iOS version of AppleScript.


When a script must be designed, the important task is not coding, it's to fully analyse the problem.

I often describe that this way : « Think before coding ».

Alas it seems that some users feel that this formula is impolite 😕


Here are the first documents to study:


https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/Ap pleScriptX/AppleScriptX.html#//apple_ref/doc/uid/10000156i


https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/Ap pleScriptLangGuide/introduction/ASLR_intro.html#//apple_ref/doc/uid/TP40000983


https://developer.apple.com/library/mac/#releasenotes/AppleScript/RN-AppleScript /Introduction/Introduction.html


Yvan KOENIG (VALLAURIS, France) mercredi 18 avril 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.3

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

Apr 20, 2012 2:59 AM in response to VerusEx

Hello


The structure of the files created by Numbers on the iPad is really odd so the given script's interest is very limited.


Here is an other one which split datas structured in fixed withs columns (values padded by space characters)


--{code}

--[SCRIPT split-fixedWidths-datas]

(*

Copy the datas source in the clipboard.

Run the script.

The extracted datas will be inserted in a new Numbers document


Sample set of datas:


aa bbbb ccc dd ee

a2 bbbbbbbb ddef edf

a3 b3 erty

b4 c4 d4 e4


Yvan KOENIG (VALLAURIS, France)

2012/04/17

*)

--=====

(*

Properties useful to fasten the script

*)

property lesValeurs : {}

property listeTemporaire : {}


--=====


on run

local nblignes, r, uneListe, uneLigne, uneValeur, c, caraC, nbColonnes

local lesLargeurs, nouvelleLigne, ligneBidon, largeurC, vieilleLigne, aux, k


(*Extract the original datas from the clipboard

*)

set my lesValeurs to paragraphs of (the clipboard as text)


(*

Remove possible extraneous empty paragraphs at end of the datas set

*)

repeat while item -1 of my lesValeurs = ""

set my lesValeurs to items 1 thru -2 of my lesValeurs

end repeat


(*

Apply a first split process.

Every group of characters ending with a space is treated as a single value.

This structure will me modified later if required.

*)

set nblignes to count my lesValeurs

set my listeTemporaire to {}

repeat with r from 1 to nblignes

set uneListe to characters of item r of my lesValeurs

set {uneLigne, uneValeur} to {{}, {}}

repeat with c from 1 to count uneListe

set caraC to item c of uneListe

copy caraC to end of uneValeur

if caraC is space then

if (c = (count uneListe)) or item (c + 1) of uneListe is not space then

copy uneValeur as text to end of uneLigne

set uneValeur to {}

end if -- (c=…

end if -- caraC…

end repeat -- c

copy uneLigne to end of my listeTemporaire

end repeat -- r


(*

Scan listeTemporaire to get the count of columns

*)

set nbColonnes to 0

repeat with r from 1 to nblignes

if (countitemr of my listeTemporaire) > nbColonnes then set nbColonnes to (countitemr of my listeTemporaire)

end repeat


(*

Scan items of (items of listeTemporaire) to build a list of columns widths

CAUTION : assumes that there is at least one line with no values made of spaces.

*)

set lesLargeurs to {}

repeat with r from 1 to nblignes

if (countitemr of my listeTemporaire) = nbColonnes then

repeat with c from 1 to nbColonnes

copy (count item c of item r of my listeTemporaire) to end of lesLargeurs

end repeat

exit repeat

end if

end repeat


(*

Split some values if needed

*)

repeat with r from 1 to nblignes

if (countitemr of my listeTemporaire) < nbColonnes then

set {nouvelleLigne, ligneBidon, c} to {{}, {}, 1}

repeat

try

set largeurC to itemc of lesLargeurs

set vieilleLigne to itemr of my listeTemporaire

if (count item c of vieilleLigne) = largeurC then

copy item c of vieilleLigne to end of nouvelleLigne

copy item c of vieilleLigne to end of ligneBidon

else

set aux to text 1 thru (largeurC) of item c of vieilleLigne

copy aux to end of nouvelleLigne

copy aux to end of ligneBidon

copy ligneBidon to ligneBidon2

copy text (1 + (largeurC)) thru -1 of item c of vieilleLigne to end of ligneBidon2

repeat with k from (c + 1) to count of vieilleLigne

copy item k of vieilleLigne to end of ligneBidon2

end repeat -- k

set itemr of my listeTemporaire to ligneBidon2

end if

end try

if c = nbColonnes then exit repeat

set c to c + 1

end repeat -- c

set itemr of my listeTemporaire to nouvelleLigne

end if

end repeat --r


(*

Build the final data set

*)

repeat with r from 1 to nblignes

set itemr of my listeTemporaire to my recolle(itemr of my listeTemporaire, tab)

end repeat -- r


(*

Save the deciphered datas in a temporary text file

*)

set leFichier to (path to temporary items as text) & "azertyuiop.txt"

my writeTo(leFichier, my supprime(my recolle(my listeTemporaire, return), space), text, false)


(*

Open the temporary file in Numbers

*)

tell application "Numbers" to open leFichier


(*

Clear properties so they will not be stored in the script

*)

set {my listeTemporaire, my lesValeurs} to {{}, {}}

end run


--=====


on recolle(l, d)

local oTIDs, t

set oTIDs to AppleScript's text item delimiters

set AppleScript's text item delimiters to d

set t to "" & l

set AppleScript's text item delimiters to oTIDs

return t

end recolle


--=====

(*

removes every occurences of d in text t

*)

on supprime(t, d)

local oTIDs, l

set oTIDs to AppleScript's text item delimiters

set AppleScript's text item delimiters to d

set l to text items of t

set AppleScript's text item delimiters to ""

set t to l as text

set AppleScript's text item delimiters to oTIDs

return t

end supprime


--=====

(*

Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861

*)

on writeTo(targetFile, theData, dataType, apendData)


-- targetFile is the path to the file you want to write


-- theData is the data you want in the file.


-- dataType is the data type of theData and it can be text, list, record etc.


-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it

try

set targetFile to targetFile as text

set openFile to open for accessfiletargetFile with write permission

if not apendData then set eof of openFile to 0


writetheDatatoopenFilestarting ateofasdataType


close accessopenFile

return true

on error

try


close accessfiletargetFile

end try

return false

end try

end writeTo


--=====

--[/SCRIPT]

--{code}


Yvan KOENIG (VALLAURIS, France) vendredi 20 avril 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.3

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

Importing space delimited iPad text files into number

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.