I have a CSV file which on each line is a code.
I then would have a dialog box asking for the code, once the code is entered it then searches the contents of the CSV if it is found I would like it to add a tick, next to that item, if it is not there then an error message and then in both incidences it would then return to the script to search for the next time.
Is this something that could be done?
Many Thanks
Matt
If I understand correctly, your CSV file looks like:
code1
code2
code3
and you want it to look like:
code1, 2
code2, 0
code3, 4
after the script has run, with the “tick” number being the number of times you searched for the particular code. This can be done, and this script should do the trick:
property CSVfile : ""
tell application "TextEdit"
try
open CSVfile
on error
try
set CSVfile to choose file
open CSVfile
on error
return
end try
end try
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return --assumes 1 code per line
set CSVtext to text of front document
set CSVlist to text items of CSVtext
repeat with i from 1 to count of CSVlist
set item i of CSVlist to {item i of CSVlist, 0}
end repeat
repeat
try
display dialog "Enter the code to search for:" default answer ""
set inputCode to text returned of the result
set inList to false
repeat with i in CSVlist
if item 1 of i is inputCode then
set item 2 of i to (item 2 of i) + 1
set inList to true
end if
end repeat
if inList is false then display alert inputCode & " was not found in the list."
on error
set CSVtext to ""
repeat with i from 1 to count of CSVlist
set item i of CSVlist to (item 1 of item i of CSVlist) & ", " & (item 2 of item i of CSVlist)
end repeat
set CSVtext to CSVlist as string
set text of front document to CSVtext
set AppleScript's text item delimiters to astid
--add any closing instructions here
return
end try
end repeat
end tell
This script leaves the file open in TextEdit for you to look at. You can add lines to save and close it if you prefer.
Model: Mac Mini
AppleScript: 2.1.2
Browser: Safari 534.59.8
Operating System: Mac OS X (10.6)