PDA

View Full Version : Applescript Text Extract - Help




Ethereal11
Apr 9, 2012, 02:34 PM
So, I'm getting into Applescripts and have come to a road block in trying to extract a string of numbers from a variable. My variables read like so..

"blah blah blah XXX-XX-XXXXXX-XX-X yadda yadda"

How can I go about setting XXX-XX-XXXXXX-XX-X as a new variable with the other verbiage floating around it? I need to maintain the hyphens in the resulting variable.

TIA!!



kryten2
Apr 9, 2012, 09:14 PM
Perhaps not the best or most elegant solution but it works.

set the_String to "blah blah blah 123-45-678925-78-9 yadda yadda"
try
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {" "} -- declare new delimiters
-- do script steps here
set these_items to the text items of the_String
-- the result of the these_items is a list:
-- {"blah", "blah", "blah", "123-45-678925-78-9", "yadda", "yadda"}
set AppleScript's text item delimiters to oldDelims -- restore them
on error
set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
-- we take the 4th item in the list
--the result of the_new_String will be : "123-45-678925-78-9"
set the_new_String to (item 4 of these_items)