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! 🙂

problems with wifi, Safari and Mountain Lion



If you’ve upgraded to Mountain Lion and traded swift wifi and fast Safari for a flakey internet connection and sluggish browser, you are not alone. Widespread reports of problems with wifi, Safari and Mountain Lion have been mounting ever since July 25th. However, unlike the lengthy debacle with similar wifi problems experienced after the Lion upgrade last year, a lot of users are finding their problems can be solved by using one or more of the tips below.

1. Create a new location and renew the DHCP lease
OS X Daily have a step-by-step procedure here

http://osxdaily.com/2012/08/02/fix-os-x-mountain-lion-wireless-connection-problems/

that is proving hugely successful. Don’t forget to follow their second step about changing the MTU value while you’re at it.

2. Reset default system preferences
If that didn’t do it for you, it’s time to roll up your sleeves and enter Terminal! Open the Terminal.app from

/Applications/Utilities/Terminal.app

and paste this command

rm ~/Library/Preferences/com.apple.systempreferences.plist

then press ‘return’.

You will need to restart your mac to see if this has had any beneficial effect, so do that now.

3. Do PRAM & SMC resets
If you’re still suffering problems, it’s time for a couple of system resets. To do the SMC reset you will need to see what kind of mac you have, as the procedure is different for some models. Take a look here and follow the instructions for your model.

Before powering up after the SMC reset, also take the trouble to do a PRAM reset. To do that:

i. Ensure the machine is powered off.

ii. Locate the following keys on your keyboard in preparation for Step 4:

‘command’ – ‘option’ – ‘P’ – ‘R’

iii. Press the ‘power on’ button.

iv. Immediately – and before the grey screen appears – hold down ‘command-option-P-R’ all together.

v. Keep them held down until you’ve heard the start-up chime twice. After you release them you should hear it again, and hopefully your Mac will boot up without wifi/Safari issues.



4. Check Wifi connection
And if that doesn’t work? Time to check your wifi connection. See how strong your signal-to-noise ratio is. You need something in the order of 25 or higher. To find out whether you signal is strong enough, hold down the ‘option’ key and click on the wifi icon in the menu bar. Choose Open Wifi Diagnostics from the menu.

When you see the welcome screen, ignore the ‘Continue’ button and instead press ‘command-N’ on your keyboard.

Click wifi scan in the task bar and scroll to the right where you will see two numbers, ‘signal’ and ‘noise’. Ignore the minus ‘-‘ signs, and subtract the signal number from the ‘noise’ number. Anything over 25 is a good enough signal, below that and the signal is too weak for a reliable connection. Over 40 is excellent (in the example below, you can see the SNR is 34, a pretty good signal for a home router located on the next floor).

If your SNR is lower than 25 you need to either move the computer closer to the router or find a better connection. If the ‘Noise’ shows a very low figure (equals more noise), you can try changing the channel on your router. Look at the other routers in the list and if they are using the same channel as yours, switch your router to something else between 1 and 11.

5. Reinstall OS X
If all else fails…some users are reporting that simply reinstalling OS X is solving the problem for them. Reinstalling doesn’t touch your Apps or user data, but its always wise to make sure you have a backup before undertaking such an operation.

To reinstall, restart the computer while also holding down ‘command-R’ on the keyboard. From the Utilities window that opens up, choose ‘Reinstall OS X’.

Still having problems? Let us know in the comments below.

🙂

featured picture: Internet by ~vagraine

%d bloggers like this: