Blog Archives

AppleScript: how to extract numbers from a string

Here’s a little handler I wrote in response to a query over on ASC, that will return all the numbers in a string of text. This could be really handy for all sorts of tasks, like extracting data from a text document in order to import the data into a spreadsheet or indexing page numbers from InDesign or Quark, for instance.

Here’s the handler:

on returnNumbersInString(inputString)
	set s to quoted form of inputString
	do shell script "sed s/[a-zA-Z\\']//g <<< " & s
	set dx to the result
	set numlist to {}
	repeat with i from 1 to count of words in dx
		set this_item to word i of dx
			set this_item to this_item as number
			set the end of numlist to this_item
		end try
	end repeat
	return numlist
end returnNumbersInString

To use it , place the handler somewhere in your script (handlers usually go at the beginning or end of your script, but it’s up you; AppleScript doesn’t care where you put them!). Then call it like so:

set theNums to returnNumbersInString(“put your string with some numbers like $45.12, 20%, 12 months, and other assorted data here, or use a variable that points to your text “)

The handler does the work, and sets theNums to a list containing all the numbers in your text. In the example, you’ll see the result as {45.12, 20, 12}.

After that, you’re free to sort them or send them to another app or do whatever your script wants to do with numbers. 

If you want to see this handler in action, take a look at my battery health meter script. 🙂

Office not working on Lion?

If you are having trouble opening any Microsoft Office apps — Word, Excel, Powerpoint — on your new OS X Lion installation the problem is most likely that you have Office for Mac: 2004. One of the main shifts from Snow Leopard to Lion is that the latter does not support what is known as PowerPc apps, and significantly that includes the 2004 edition of MS Office.

If you want to stick with Lion you have three options:
1. Upgrade to Office 2008 or 2011
The simplest solution, but of course it adds to the cost of the Lion upgrade.
2. Download the free Office suite LibreOffice
LibreOffice is not only free, open source software, but it will both import and export to all Office formats. The interface may look a bit dated, but the functionality is just as sophisticated and arguably even more powerful in some respects than Office.
3. Buy Apple’s equivalent software from the App Store
Apple offers ‘Numbers’ (for spreadsheets), ‘Pages’ (for word processing) and ‘Keynote’ (for powerpoint) as their own alternatives to Microsoft’s signature software package. These will all import your old 2004 Office files and export to MS format. Currently these are retailing at $19.99 each in the App store, so if you only use one of the MS programs this could be significantly cheaper than upgrading to the latest version of Office. You’ll also get AutosaveVersions and full screen functionality built in.

If you are loathe to part with your Office for Mac: 2004 for some reason, then the fourth option is to revert to Snow Leopard. This should be possible so long as you didn’t buy a new Mac that came pre-installed with Lion.

Related articles:
running PowerPc apps on Lion
will my old apps work with Lion?
how to turn a Lion into a (Snow) Leopard

featured picture: Office for Mac alternative icons by deleket

%d bloggers like this: