I need applescript to read in that text file and use it to lookup the condition for a subnum given by the user (I already have a display dialog to gather the desired subnum). The subnum lookup should ignore case.
I can’t imagine this being terribly difficult.
I can remove the header row from the text file if it makes things easier. Or maybe there’s a way to subsume the text file into the applescript?
Oh, and while I’m thinking about it, does AppleScript have the equivalent of a SWITCH function, or do I just have to use nested IFs?
Thanks very much.
-- Find file.
set myFile to (choose file)
-- Get text out of file.
set myText to read myFile
-- Make list of second to last line of the file.
set myLines to paragraphs 2 thru -1 of myText
-- Get search text.
set mySearch to text returned of (display dialog "What is the subnum?" default answer "")
-- Set result message in case text not found.
set myResult to "subnum not found"
-- Loop through list of lines.
repeat with myLine in myLines
-- If the search text is found in this line.
if myLine contains mySearch then
-- Set the result to the last word (text from last space to end of line).
set myResult to word -1 of myLine
-- Jump out of the loop.
exit repeat
end if
end repeat
-- Display the result.
display dialog myResult
This example doesn’t deal with various possibilities including the ignoring case, the possibility of the search text being in the result and the result having spaces in it.
to getValue for currKey from currString
set o to offset of currKey in currString
if o is 0 then return "no match"
last word of first paragraph of currString's text o thru end
end getValue
getValue for "f002" from read (choose file)
--> "8"
AppleScript has no built-in switch function as such. Depending on what you’re trying to do, there may be other ways of achieving similar results - but if you’re more comfortable with that syntax, then you could build your own switch handler:
on switch(l)
set c to count l
if c mod 2 is 1 then return "unbalanced arguments"
repeat with i from 1 to c by 2
if l's item i then return l's item (i + 1)
end repeat
"no match"
end switch
set cityName to "Rome"
switch({cityName = "London", "English", cityName = "Rome", "Italian", cityName = "Paris", "French"})
--> "Italian"