locating the character ":"

Hi,

seems like a very simple thing to do but I am having problems.
I am working thru time codes to isolate the hh mm ss ff and remove the “:” from the timecode.

However I can’t get applescript to see the “:” character when it comes to it.

As an example I have a script below. I do not get a dialog displaying “i found :” when I run it.

Thoughts?
Thanks in advance


set x to "01:00:01:00"
repeat with i in x
	display dialog i
	if i = ":" then display dialog "i found :"
end repeat

Change your if statement to the following and it will work

if i is in {":"} then display dialog "i found :"
set x to "01:00:01:00"

set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
if number of text items of x = 4 then
	set {hh, mm, ss, ff} to every text item of x
else
	set {hh, mm, ss, ff} to {missing value, missing value, missing value, missing value}
end if
set AppleScript's text item delimiters to oldTID

Thanks to both reply’s

much appreciated !!!
you rock

Hi.

The immediate cause of your problem was that with the ’ . in .’ kind of repeat, the value of the loop variable isn’t an item from the list or text, but a reference to it ” eg. not “:”, but ‘item 3 of “01:00:01:00”’. You can usually use this interchangeably with the item itself, which is why the first ‘display dialog’ displays all the characters. But tests to see if it is the item always return false. You can explicitly resolve the reference using ‘contents of’:

if (contents of i = ":") then display dialog "I found :"

But as a solution for your task, the method DJ Bazzie Wazzie’s given you is better than repeating through every character in the text.

This is another way of doing it:


on split:x sep:y
	set pos to offset of y in x
	if pos = 0 then return x
	{text 1 thru (pos - 1) of x} & (its split:(text (pos + 1) thru -1 of x) sep:y)
end split:sep:

tell it to split:"01:00:01:00" sep:":"

Not that it beats DJ Bazzie Wazzie’s solution, which is what I’d use in practice, but it shows the elegance of recursion (and the handy “offset” command from standard additions). If you want a string instead of a list, replace the braces with parentheses.

PS: I’m using the syntax introduced in AppleScript 2.3 (OS X Mavericks). If you are on an older system, you should use split(x,y) instead.