New to Applescript

I’m learing applescript and I need to know if the following is achievable. I have a text file with 1000 lines, some of the lines start with:
'TODO

which is then Suffixed by what to do. For example:

blah, code, blah, blah
'TODO - Is this the best way to do this?
this and
that and the other
'TODO - Remove redundant code from the next query
Dim rsStuff
Set rsStuff = …

So what I want is a script that will take in a given text file and return the full contents of each line that start with 'TODO

It should be concatenated with the line number and the file name. The reason which I guess is fairly obvious is to markup a page of code with relevant tags such as TODO and HACK then have the script identify those tags and produce a list of things still to do or hacks to remove.

I’m struggling to make a decision on whether to add these lines to Omni Outline Pro which I use with the kGTD scripts and Quicksilver or, to Tracks which I think I would have to add manually. The advantage of Omni is the script could send each line to Quicksilver which would then pas it on to Omni through kGTD.

This sounds great but I have rather fallen in love with TRACKS lately as my GTD and task manager.

So I guess my two questions are can it be done either way (Omni or Tracks) and, is this something someone could quickly throw together for me to take a look at. I think I could get a lot of useful techniques out of seeing this code in action rather than trying to hack something together by trial an error over the next week.

Thanks from a veteran PC user that finally saw the light
Melvin

Hi Melvin,

assuming that your text file contains only plain text
the following script parses the text file and creates a new one with the
extracted lines in it (Format e.g. text_004: 'TODO…)

set textFile to choose file
-- separate file name and extension
set {name:Nm, name extension:Ex} to info for textFile
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
-- read text file
set theText to paragraphs of (read textFile)
set TODOlines to ""
-- calculate number of leading zeros
set counter to count theText
set leadZeros to count (counter as string)
set LZ to ""
repeat leadZeros times
	set LZ to LZ & "0"
end repeat
-- parse text
set x to 1
repeat with i in theText
	if i contains "TODO" then set TODOlines to TODOlines & Nm & "_" & text -leadZeros thru -1 of (LZ & (x as string)) & ": " & i & return
	set x to x + 1
end repeat

-- write result to disk
set ff to open for access file (((path to desktop) as Unicode text) & Nm & "_TODOlines.txt") with write permission
write TODOlines to ff
close access ff

Unfortunately I have no idea about Omni Outliner or Tracks

Thank you so much Stefan,

I think with what I learn from this code, my knowledge of the workings of app A, appB and appC, throw in a dash of learning from a few of the excellent online Applescript tutorials and I may be able to integrate with at least one of them.

I’ll keep you posted.

BTW I’ll take criticism for not reading the posting guidelines from any moderator that hand on heart can say she/he has read every posting guideline for every site they’ve ever posted on.

OMG, what have I started now. Let she/he who is without sin cast the first digital stone, to my PM though please.

Edited on the advice of Mikey-San with tongue firmly in cheek and no offense taken.

Is this some kind of advertisement or search engine gaming in the disguise of a forum post?

Why would you ask that? If you look at the apps/websites I’ve mentioned they have nothing in common except the fact I use them all, think they are great resources and realized after my first post that I was being a little too cryptic mentioning GTD, Tracks, Quicksilver.

Tracks and Quicksilver are both freeware, no profit in that. 43 folders is a great site to help with organization on a conceptual as well as practical level, Merlin Mann who runs the site does a lot of free Webcasts for Leo Laporte’s stable of Free webcasts, webcasts which are very informative and very Mac biased. It’s through posts like mine I discover other related technologies, sites, etc. and thought I would pay it forward because i can’t contribute any Applescript support at the moment, due to the fact I only know how to make a beep and have the default voice say “Hello World” :slight_smile:

The only commercial app mentioned is by another company whose work I like and, the scripts for GTD that integrate with it are freeware too.

One caveat, not sure actually if everything is freeware, there may be some donation ware in there too but I don’t think by mentioning them I can be accused of spamming or keyword loading to help out people I have absolutely nothing to do with.

Being a long time Digg (whoops, there I go again) user I understand the need to keep noise to a minimum, this wasn’t noise. I’ve already used the script today at work to pull out all of my TODO tasks from an ASP page I’m working on, impressed the hell out of my boss too, until I tell him I didn’t write it that is. Then I will have to explain what I was doing for the last 3 hours.

I usually don’t read the posting guidelines to sites like this because they are generally all the same. No spam, be polite, no flame wars, all the usual common sense stuff. If I’ve done something wrong by all means let me know, I’ll know better next time then.

Want me to go edit my previous post, sure I can do that but I feel it would make it a lot less useful and you never know, it may just introduce someone to something new and useful. I’m a software junkie, I bet I’m not the only one reading this forum.

Melvin

I would ask because your post looks a lot like SEO gaming. You’re brand new, and there’s a lot of information in it that’s useful for gaming search engines, while little of it is relevant to the thread. Make sense?

Don’t get bent out of shape. Perhaps I’ve seen it on other forums and don’t want it happening here. Since I don’t know you, there’s no reason for me not to at least pose the question. Nothing personal.

To be honest, I think you should trim your post, merely for the reason that there’s a great deal of extraneous information that people will have to sift through to see if there’s something relevant to your AppleScript question.

I’m sure the moderators love this. I don’t see any reason not to read the guidelines. Who knows what useful information you may find that you did not expect.

This is what I’d rather use for parsing your text file for “TODO” lines:

set textFile to POSIX path of (choose file)
set filteredOutput to do shell script "cat " & (quoted form of (textFile)) & " | grep 'TODO' | tee " & (quoted form of (do shell script "dirname " & (quoted form of (textFile))) & "/TODO_" & (do shell script "basename " & (quoted form of (textFile))))

Obviously my script is way too difficult for a newbie to understand.
.but it is fast, reliable, compact and last but not least it’s shell
Four reasons why I’d use it rather than a vanilla applescript.

Btw: it creates a file with the filtered output and saves the output into the variable filteredOutput at the same time (if wanted).

StefanK: Anyway, nice vanilla script ;):smiley:

Obviously it’s a bit daft, then, posting it to help someone who’s trying to learn AppleScript. :wink:

The first two, at least, are worthy considerations. However, on my machine, Stefan’s script is faster (and a version I wrote this morning but decided not to post is probably faster still) and yours reliably doesn’t work. The text in the file it produces is an exact copy “ not an edit “ of the original.

Yours would be even more compact if you’d used ‘quoted form of’ once while defining ‘textFile’ instead of using ‘quoted form of textFile’ three times in the following line. It would also be faster if you could find some way to combine the three shell script strings into one ‘do shell script’ call. I’m afraid I can’t help you with that, nor with getting it to work properly in the first place… :wink:

Edit: I’ve found the reason for the script not working properly: it relies on the text file having Unix line endings. Vanilla AppleScript doesn’t have that limitation. It can handle Unix, DOS, or Mac OS line ends without needing special arrangements. :slight_smile:

I haven’t been following this, but it looks like you could have done that with a ‘sed’ one liner.