Getting Started in AppleScript

Hi everyone,

I’ve only just started working on a mac (let alone applescript) but I’m a programmer by trade. Coding seems simple enough but I have a few questions that if answered will iron out the confusions I have.

  1. Is a software editor required to create a simple applescript, or can this be done using textedit?

  2. How is the file run once created?

  3. I’m guessing it’s possible to do the following, but I do not even know where to begin. Any suggestions you may have would be greatly appreciated:

  • Open a text file on my hard drive
  • Go through the file and pick out the email addresses
  • Write those email addresses to another text file

This is for a customer database that will be moved to another system. I just need a listing of the customer names, and email addresses in a text file so that I can import it into the other system.

Thanks, and looking forward to “meeting” all of you!

You can write code in any place, but usually you will compile the script in order to run it, which is usually done in a script editor: Apple’s “Script Editor”, Satimage’s “Smile” or LateNightSoftware “Script Debugger” (first two are free, first one is in your AppleScript folder inside the Applications folder). You will find some other oportunities if you need it, such as AppleScript Studio (Xcode) or FaceSpan (still in beta for OS X), mostly used for applications with interface.
A script editor can compile code, check the syntax of your scripts, provide indentation, syntax coloring, etc.

From your favorite script editor, you can save compiled scripts, tipically run from a script menu (apple’s script menu, fastscripts, or built-in script menus, such as the ones in BBEdit, Tex-Edit Plus, Entourage, Mail…).
You can also create standalone applications (double-click and run).
If you work in AS-Studio or FaceSpan you can create more complex structures for your app, including icons, windows, etc. (also, usually, standalone apps).

Well, I think this is a question for a separate topic. Most probably, you can do it, even more easilly if you are talking about a plain text file. Also, if you take a look to MacUpdate or Versiontracker, you’ll find specialized email extractors, which may be the faster way for your needs, if you need a quick “done!”…

Welcome! Since you are just starting to get familiar with AppleScript, you might want to take a look at the following resources.

AppleScript Language Guide - Old but still relevant
AppleScript for Absolute Starters (available as downloadable PDF)

Generally speaking, a script editor is the preferred way to create compiled scripts and applications since it offers the opportunity to find syntax errors. It is possible to run code that’s stored in plain text files but it’s a crapshoot if the syntax is bad.

Compiled scripts can be run with:

  • A script editor
  • Script menus (systemwide, application specific)
  • Various utilities such as iKey, iBeeZz, various script schedulers and macro utilities

Scripts saved as applications can be run/launched the same as any other application (Dock, double click its icon, etc.)

Yes, it’s entirely possible, but I don’t have any code to offer for extracting/parsing email addresses.

Isn’t it possible to export the required data from the current system and then import it into the new system? Most databases can do this easily.

I hope this helps. Feel free to ask follow-up questions. :slight_smile:

– Rob

Welcome to Macscripter! I think you will find using a Mac much better than ‘other’ boxes IMHO, more especially since you’re already a programmer. Think of Applescript as a system wide/application wide ‘glue’ that do a lot of cool stuff. After you get your feet wet, you will find all kinds of stuff you can do, right alongside perl, php, sql, terminal, etc.

Not required, but I would use Apple’s script editor right out of the box to get a handle on things. You compile your applescript from here too, so you would want to use it anyway.

As Rob and others have already mentioned, there are many more editors out there too. Once you get a feel for the stock editor, try out the others when you can.

Once you check the syntax of your script, that is setting up Script Editor to compile, which happens at the time you save a file. When you save your script, you will see a drop down menu allowing you to save as a regular script, a compiled app, all of which will work for both MacOS 9 and OSX [well … most of the time anyway :>)].

If you need more help, right here is the place to find out. :lol:

Most definitely can be done with Applescript.

While this can be done with Applescript, I question … what kind of DB is it? - If it’s mySql, then you could just as easily do a export from it instead. If it’s a flatfile Db, what flavor is it?

Welcome, and good luck!

On the compiler issue, one option hinted at, but not directly mentioned, is the use of ‘osascript’ in a shell.

 osascript /path/to/script

will run the specified script. If it’s plain text, osascript will compile the script (via osacompile) and run it.

On the ‘extract email addresses’ question, is the data structured in any way (tab delimited? fixed lengths? etc?), or is if freeform?

If it’s structured, it’s easy to walk the file extracting specific data as you go. If free form, it’s a little more complex, but still achievable. More info from you would help decide.

Wow! what a response! I didn’t expect to have so many responses so quikley. Some forums are MUCH slower.

The database is a filemaker pro database. Does anyone know if it’s possible to just do a simple extract from there? That definately would make the job easier. (I come from an SQL/Oracle, .NET/Java world)

The problem however, is that the email address has been saved in a large text field. (sort of like an “additional info” box) where there is other data as well as the email address. I need to parse out the email address only.

You can easily find example code on this board for extracting data from FileMaker Pro DBs (try the search feature for the BBS). As to the email extraction, here is the code I use (it has been uploaded to Code Exchange but as of the posting of this message, the upload was still pending):
set the_text to “This is some text that includes some email addresses.,,,,,,,,, users should be included.”

set address_list to my extract_addresses(the_text)
return address_list

on extract_addresses(the_text)
	set {the_text, address_list} to {words of the_text, {}}
	repeat with i from 2 to (count of the_text)
			if item i of the_text = "@" then
				set temp_address to ((item (i - 1) of the_text) & (item i of the_text) & (item (i + 1) of the_text)) as string
				if temp_address is not in address_list then
					if (my verify_address(temp_address)) then set end of address_list to temp_address
				end if
			end if
		end try
	end repeat
	return address_list
end extract_addresses

on verify_address(temp_address)
	set address_parts to (text items of temp_address)
	if (count of address_parts) < 2 then return false
	set AppleScript's text item delimiters to "."
	set address_parts to text items of temp_address
	set AppleScript's text item delimiters to ""
	if (count of address_parts) < 2 or (count of item -1 of address_parts) > 4 then return false
	return true
end verify_address


Thank you for your help! How can these extracted emails be written to another file? I’m trying to determine how to create a file and write these values in.

Thanks again,

Take a look to this FAQ: