Extract address fields


I am looking to break a clipboards text into four sections. The clipboard will always have Street address, City, State, Zip an example is:

11401 NW 12th St
Miami, FL 33172

I would like it to separate like this:

Street Address: 11401 NW 12th St
City: Miami
State: FL
Zip: 33172

I am super new and have no idea how make this work with regex.....
Thanks in advance!

Rather than use regex directly, you could use an AppleScript that employs NSDataDetector (which I believe uses regexes under the hood anyway) to extract the address.

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on extractAddressFrom:theString
	set dataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeAddress) |error|:(missing value)
	set addresses to dataDetector's matchesInString:theString options:0 range:{location:0, |length|:length of theString}
	return (addresses's valueForKeyPath:"components") as record
end extractAddressFrom:

on makeKMDictionary:theRecord withName:theName
	set dict to current application's NSDictionary's dictionaryWithDictionary:theRecord
	set allKeys to dict's allKeys()
	tell application "Keyboard Maestro Engine"
		set newDict to make new dictionary with properties {name:theName}
		repeat with theKey in allKeys
			set theValue to (dict's valueForKey:theKey) as text
			set kmKey to (theKey as text)
			tell newDict to make new dictionary key with properties {name:kmKey, value:theValue}
		end repeat
	end tell
end makeKMDictionary:withName:

on run
	tell application "Keyboard Maestro Engine"
		set addressBlock to getvariable "AddressBlock"
	end tell
	--	set addressBlock to "11401 NW 12th St
	--Miami, FL 33172"
	set addressFields to my extractAddressFrom:addressBlock
	my makeKMDictionary:addressFields withName:"AddressFields"
end run

Put this script into an Execute an AppleScript action and it will return a KM dictionary containing the different address fields.

Scrape Address.kmmacros (4.6 KB)

1 Like

Hi @Yisbel_Carrasco,

If you'd prefer an approach that doesn't involve AppleScript then here's one based entirely on native KM actions. Just make sure you've copied the address into the clipboard before running the macro.

Download Macro(s): Test Address Extraction.kmmacros (7.4 KB)


Keyboard Maestro Export

  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

By the way, it will also work with multiple address line pairs in the clipboard.