I have a question, I have a huge text file in which each word is on a new line.
How can I write an Applescript to chose the (x) word as text?
Hi,
Maybe code as follows gets you started:
on run
set chosenfile to my askforfile()
if chosenfile is missing value then
return
end if
set paras to paragraphs of (read chosenfile)
set num to askfornumber()
if num is not missing value then
if num > (length of paras) or num < 0 then
display dialog "Sorry, word number " & num & " does not exist!" buttons {"OK"} default button 1 with icon stop
else
display dialog "Word " & (num as text) & ":" & return & (item num of paras) buttons {"OK"} default button 1 with icon note
end if
end if
end run
on askforfile()
try
set chosenfile to choose file with prompt "Please choose a text file:" without multiple selections allowed
on error
return missing value
end try
end askforfile
on askfornumber()
try
display dialog "Please enter a number:" default answer "" buttons {"Cancel", "OK"} default button 2 with icon note
set dlgresult to result
set userinput to text returned of dlgresult
try
set useriput to userinput as number
return userinput
on error
my askfornumber()
end try
on error
return missing value
end try
end askfornumber
Best regards from Berlin,
Martin
Ok the script works but when I modify it to chose the items automatically it stops at reading the file
I wanted it to read item 1 on first repeat, item 2 on second, ect so I made this modification
on run
set dlgresult to "0"
set chosenfile to my askforfile()
if chosenfile is missing value then
return
end if
set paras to paragraphs of (read chosenfile)
set num to askfornumber()
if num is not missing value then
if num > (length of paras) or num < 0 then
display dialog "Sorry, word number " & num & " does not exist!" buttons {"OK"} default button 1 with icon stop
else
display dialog "Word " & (num as text) & ":" & return & (item num of paras) buttons {"OK"} default button 1 with icon note
end if
end if
end run
on askforfile()
try
set chosenfile to choose file with prompt "Please choose a text file:" without multiple selections allowed
on error
return missing value
end try
end askforfile
on askfornumber()
try
set userinput to dlgresult
try
set useriput to userinput as number
return userinput
on error
my askfornumber()
end try
on error
return missing value
end try
end askfornumber
I was going to add a counter but it stopped working so I just stopped editing till I got a real result.
But with huge text files I was wondering how huge it really is because when exceeding 80,000 lines you should consider AWK. AWK can find a line within very short time even if you exceed a GB of data (or millions of lines).
do shell script "awk 'NR==123456{print;exit}' /path/to/file" --123456 has to be replaced with the actual line number
Reading a file the first loop en then read the file a second loop can be expensive, depending how far you’ll go. If you want to loop through the whole list I should look into something like this.
set theLines to every paragraph of read file theFile
repeat with theLine in theLines
--do you stuff here
end
This way the file is parsed and put into the memory once instead of reading and parsing the same file over and over again.