Page 1 of 1
Find "/"
Posted: Fri Mar 13, 2009 7:24 pm
by gyroscope
Hi, if I have a web address, for example:
forums.runrev.com/phpBB2/index.php
I would want to split it up as follows:
forums.runrev.com
/phpBB2
/index.php
My code is:
Code: Select all
on mouseUp
do fld "A" as appleScript
put result() into tRes
put tRes after field "B"
delete char 1 of field "B"
delete char -1 of field "B"
if char 1 to 7 of line 1 of field "B" is "http://" then
delete char 1 to 7 of line 1 of field "B"
end if
find "/" in line 1 of field "B"
put return after char "/" in line 1 of field "B"
end mouseUp
But it doesn't find "/" and doesn't put a return where needed.
If anyone could help with the following questions please that'll be great:
• How do I amend the above script to make it work as I want?
• How do I find a second instance of a character? (When I'm able to get this:
forums.runrev.com
/phpBB2/index.php
I'd need to find the second "/" to put the cr before it).
Any help appreciated, thanks.
Posted: Fri Mar 13, 2009 9:44 pm
by morbug
How about setting the itemdelimiter to "/" and then use the third, fourth and fifth item to get what you want?
Edit: Like this...
Code: Select all
set the itemdelimiter to "/"
IF char 1 to 7 of field "B" is "http://" THEN
put item 3 of field "B" & return into tText
put "/" & item 4 of field "B" & return after tText
put "/" & item 5 of field "B" & return after tText
put tText into field "B"
END IF
Posted: Fri Mar 13, 2009 10:00 pm
by gyroscope
Thanks for help there morbug but I can't get that to work. I think maybe that setting the itemDelimeter might not be useful here? I think that's for adding to split up sentences rather that finding a particular character which happens to be one which is commonly used as an item delimeter; if you see what I mean... (also a web address is seen as one word anyway).
But thanks again for the idea.
By the way, it's strange that if you ask it to find anything else in the line other than a slash, it will find it. And strange that I asked it to find "runrev" as an example, and the cursor was in the middle of these letters, not at the end, as expected. Then strange again that if I don't delete the web address and just click the mousebutton again, it'll append the same web address, of course, and THEN will find the first "/".
Curiouser and curiouser, said Alice.

Hmmm...
Posted: Fri Mar 13, 2009 10:11 pm
by morbug
Odd. But I'm pretty new at this so there probably are something that I don't understand. It seems to work pretty well for me if I put an URL in field "B".
I updated the code to be a bit more generic but if the first one didn't work this one probably won't either:
Code: Select all
set the itemdelimiter to "/"
IF char 1 to 7 of field "B" is "http://" THEN
REPEAT WITH x=3 to the number of items in field "B"
IF x = 3 THEN
put item x of field "B" & return into tText
ELSE
put "/" & item 4 of field "B" & return after tText
END IF
END repeat
put tText into field "B"
END IF
Posted: Fri Mar 13, 2009 10:16 pm
by gyroscope
Well, good grief morbug, it worked! (almost, as I'll explain in a minute). I'll have to examine your code in more detail, and revise my understanding of "Itemdelimeter" (never stop learning, me!) Thank you.
It's almost there but I get:
forums.runrev.com
/phpBB2
/phpBB2
i.e as you can see, the third part of the address is a duplicate of the second. Right, off to experiment with your code. Thanks again.

Posted: Fri Mar 13, 2009 10:52 pm
by gyroscope
I've modded your code as I need, morbug, and it works really well, thanks, so for the record it's:
Code: Select all
on mouseUp
do fld "A" as appleScript
put result() into tRes
put tRes into field "B"
delete char 1 to 8 of field "B"
delete char -1 of field "B"
set the itemdelimiter to "/"
-- put the number of items in line 1 of field "B" into tNum
-- answer tNum
put item 1 of field "B" & return into field "C"
repeat with x= 2 to number of items in field "B"
put "/" & item x of field "B" & return after field "C"
end repeat
end mouseUp

Posted: Fri Mar 13, 2009 11:15 pm
by bn
Hi Gyroscope,
one variation of the itemdelimiter
Code: Select all
on mouseUp pMouseBtnNo
put line 1 of field 1 into tLine1
set the itemdelimiter to "/"
put item 1 of tLine1 & cr into tCollect
repeat with i = 2 to the number of items of tLine1
put "/" & item i of tLine1 & cr after tCollect
end repeat
delete last char of tCollect
put tCollect into line 1 of field 1
end mouseUp
I would go for
Code: Select all
on mouseUp pMouseBtnNo
put line 1 of field 1 into tLine1
put length(tLine1) into tSoOften
repeat with i = tSoOften down to 1
if char i of tLine1 = "/" then put return before char i of line 1 of field 1
end repeat
end mouseUp
In your case I like the repeat with x down to y because Rev is very fast with one character stepping and since you change the text (by putting a return into it) with counting from the end to the beginning you dont have problems with the number of characters. I you count up and put an extra character into the text you will have to take that into account.
find is not so good when you want to change the field where you found something. You would find the "/" with 'find chars "/" in field 1" not just 'find "/"'. Look in the documentation of the find command what it finds, the simple find command finds beginnings of words, I suppose that since "/" is in the middle of a word or since it is not considered a word it does not find it.
Offset would be a better alternative but is a little involved with repeated Offsets, since you have to indicate the start of the repeated offset because otherwise it always starts from the beginning.
both the code snippets are tested. They dont take care of the "http://", which obviously is a little in the way, since in your original post you did not have "http://"
regards
bernd
Posted: Fri Mar 13, 2009 11:37 pm
by morbug
gyroscope wrote::
forums.runrev.com
/phpBB2
/phpBB2
Yeah, I forgot to change item 4 to item x but you noticed that already so all is well

. I used itemdelim a LOT when doing a little character creation application for a tabletop roleplaying game. Useful for finding stuff inside paranthesis too. (sp? I'm just a silly nerdish swede

).
Posted: Fri Mar 13, 2009 11:57 pm
by gyroscope
Thank you for the additional info concerning itemDelimeter, Bernd. I've learned that itemDelimeter is more "powerful", and find is less "powerful" than I thought.
( I'm a swede)
For interest morbug, my first wife's mother was Faroese (for those who don't know, the Faroe Islands are "owned" by Sweden).
My only other connection with Sweden is that I go to Ikea a lot!!
