Applescript Text Extract - Help

Discussion in 'Mac Programming' started by Ethereal11, Apr 9, 2012.

  1. Ethereal11 macrumors newbie

    Jan 19, 2009
    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.

  2. kryten2 macrumors 6502a

    Mar 17, 2012
    Try this

    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"
    	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)

Share This Page