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
		try
			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. 🙂

how to paste with no formatting



If you find it annoying that in Pages and other Apple apps, there’s no easy way to remove rich text formatting when you copy and paste from websites and other rich text sources, this could be just the tip for you.

The video begins with a demonstration and then shows you how to create the shortcut shown. If you don’t see captions at the bottom of the video, be sure to press the ‘CC’ button in the YouTube video controller bar at the bottom of the screen.

The procedure is fairly straightforward:

1. Open Automator.app and create a new service by clicking on the gear/cog.

2. Change the ‘Service receives’ menu to ‘no input’.

3. Type ‘Apples’ in the Action filter bar, and drag a ‘Run AppleScript’ action into the main pane.

4. Clear the default text, and copy/paste this into the window:

set the clipboard to «class ktxt» of ((the clipboard as text) as record)

5. Click the hammer icon to compile the code, and then ‘command-S’ to save it (you don’t need to choose a save location).

6. The service will now appear in your Services menu (eg. Pages > Services > plainTxt).

Whenever you copy some formatted text, just click on the plainTxt item in the Services menu before you paste, and you’ll get plain text instead.

7. Follow the second half of the video if you want to make a global shortcut.

Enjoy! 🙂

how to stop help windows always being on top

HelpViewer


Ever called up a HelpViewer window in an app only to find that you have to close the window in order to get back to the app and start following the instructions? That can be highly annoying if the instructions aren’t easy to remember.

That’s because the HelpViewer.app is by default set to always be on top. If you want to change that, here’s a nice little Terminal trick that will let you manage HelpViewer windows just like a normal window, courtesy of new tipster site defaults-write.com. Here’s what you do:

1. If you have any HelpViewer windows open, close them first.

2. Open Terminal.app (/Applications/Utilities/Terminal.app) and paste this command into the window:

defaults write com.apple.helpviewer DevMode -bool true

and press ‘return’ on your keyboard.

That’s it! From now on, you’ll be able to switch focus between the app and the HelpViewer window so that you can work with whichever one you need on top.

Tip: If you want to reverse the behaviour, use the same command as above but replace ‘true’ at the end with ‘false’.

If you like exploring the various things you can do with defaults preferences, have a rummage around the defaults-write.com site, a nice little addition to our bookmarks list! 🙂

<span>%d</span> bloggers like this: