I’m happy to break it down for you.
Step one: We need to get the path to the file, but the system will give us what’s called an alias path. Alias paths look like this:
It’s not what we call “POSIX-compliant”, so the shell can’t use it. AppleScript, however, is kind enough to provide us a facility to convert Mac OS aliases to POSIX paths:
set pathAsPOSIX to POSIX path of pathAsAlias
That will turn the above alias-style path into this:
We can do the conversion in the same line with the “choose file with prompt” command:
set pathAsPOSIX to POSIX path of (choose file with prompt "Your ad here.")
Step two: The file isn’t a regular plain text or rich text file, so we need to yank out the plain text strings within. We already know the value we want is stored as a plain text string inside the file, so we use a shell program called strings to dump a list of the text strings inside the file. E.g.,
In your case, the strings commandwill dump the same info TextEdit shows you. Now we’re ready to look for the text you want to extract. For this, we turn to awk.
Step the third: We need to locate lines that contain “getspotfunction setscreen”. We could use another shell program called grep, but awk can perform both the search and the value extraction we’re going to do later, so we’ll keep it down to just using strings and awk.
So now, we search:
Oh, but that doesn’t work, because we want to search the text that the strings command gave us. For this, we use what we call “output redirection”. We’re going to take the output of the strings command and pipe it to awk. Awk will read the output of strings and use that instead of a file. E.g.,
Data comes out of strings and goes into awk. Easy peasy.
But now we need to return what awk finds, so we tell awk to print it:
The output of that entire command might look like this:
We’re rockin’ out so far. We just need to . . .
Step four: Extract the two values at the beginning of the lines of text returned by awk. By default, awk’s field delimiter is the space character. This is analagous to AppleScript’s text item delimiters. Since the default is a space, and our fields are space-delimited anyway, we just have to reference them that way:
Since I’m already telling awk to print text anyway and return it to AppleScript, I may as well label and format them for my dialog inside the awk command:
(There should be three spaces between the value of the first field and the label of the second, just in case the forums truncate the runs of spaces.)
Step five: Almost done. Now we just need to bring it into AppleScript.
set shellOutput to (do shell script "/usr/bin/strings" & space & quoted form of theFileAsPOSIX & "| /usr/bin/awk '/getspotfunction setscreen/{print \"Frequency: \"$1\" Angle: \"$2}'"
Note that we need to do THREE IMPORTANT THINGS (IN ALL CAPS HERE BECAUSE THEY’RE IMPORTANT) when we turn this into an AppleScript shell call:
- We always reference shell programs /by/full/path/names. See this post for an explanation:
http://bbs.applescript.net/viewtopic.php?pid=52253#p52253
- Spaces in disk, folder, and file names have to be handled correctly in the shell. The way we commonly do this in AppleScript is to use “quoted form of” with our path name variable:
do shell script "/bin/rm -r" & space & quoted form of theFile"
In the shell, it looks like this:
- Double quotes tell AppleScript where the shell script command starts and ends, so we have to escape them (protect/translate them) properly:
do shell script "/bin/echo "bash rules!""
That won’t compile, but this will:
do shell script "/bin/echo \"bash rules!\""
Step six: Insert into the display alert command, which you should be familiar with (or be able to pick through) at this point.
And there you have it.