the number of a paragraph

Im trying to create a scrip that copies text from my cellphones providers website and parses my usage info and sends it to me. I have the text copied in the script and can find the static text next to my usage but I need to find the paragraph after that one (aparagraph).

Im sure its dead simple but I cant figure it out.

tell application "Safari"
	set thetext to text of document 1
	set countofparagraphs to count paragraphs of thetext
	
	repeat with aParagraph in (paragraphs of thetext)
		set aParagraph to contents of aParagraph -- coerce from reference to text
		if aParagraph begins with "einingar" then
			
						
			exit repeat
		end if
	end repeat
end tell

use

repeat with x from begin to end

end

your code can look like

tell application "Safari"
   set thetext to text of document 1
   repeat with x from 1 to  count  paragraphs of thetext
       if contents of paragraph x of theText begins with "einingar" then
           set aParagraph to paragraph (x + 1)  of thetext
           exit repeat
       end if
   end repeat
end tell

I favor this format to avoid looping and achieve uni-lineness:

tell application "Safari" to (document 1's text's paragraph 1 whose it begins with "Tuesday")'s paragraph after it

:slight_smile:

You should also aim to avoid unnecessary ununderstability and notbeingrelevantness: :slight_smile:

tell application "Safari" to set theParagraph to paragraph after (first paragraph of document 1's text where it begins with "einingar")

Interestingly, the paragraph concept doesn’t work with this thread page. I tried Marc’s script with the misspelt “Im” at the top of the first post, expecting to get either an empty paragraph or “Im sure its dead simple but I cant figure it out.” Instead, I got “Open this Scriplet in your Editor:”. The two paragraphs beginning with “Im” and the gaps after them are returned as one paragraph. It seems the line endings between the paragraphs of the post are character id 8232, which isn’t recognised as a paragraph separator by AppleScript, although it does format as required.

Well here you have the difference between writing and typing. UTF16 code 8232 is supposed to act as a new line but must not be interpreted as a paragraph separator and that is it’s purpose. I’m working at an advertising company and we’re mostly translating text. We use a lot of those special characters to keep the text together for all automated processes that has to be done later. But the main problem is that a lot of text writers don’t use those characters properly nor consistent. We can even see that Macscripter doesn’t use it entirely correct.

Therefore I’ve written my own text splitters in C and use them in Applescript, also to create multi dimensional arrays or records in one single command.

thanks a lot.

i had pretty figured another way to do this but I decided to clean it a bit up and use the solution DJ Bazzie Wazzie suggested.

i didnt quite understand how to use marc anthonys suggestion.

i have everything sorted out and have parsed the info and done some calculations etc and was thinking about texting it to me and its no problem using system events to fill out an online text message but the only thing i cant do is send the message. i assume its something a javascript could do but how do i refer to the button in question?

It’s like you exptected, you can do this with javascript. Fill in input fields can also be done with javascript and no system events needed.

to fill in a field that has an name like the login form on macscripter you http://www.macscripter.net/login.php.

tell application "Safari" to do JavaScript "document.getElementsByName('req_username')[0].value = 'evaporetore'" in document 1

to perfom a submit, also an example to macscripter inlog page, simply do this


tell application "Safari" to do JavaScript "document.getElementById('login').submit()" in document 1

about javascript
getElementsByName returns an array of elements because different elements can have the same name
getElementById returns an element because an id must be unique in a single page.

Thanks

this is the page. www.alterna.is and here is the source code

i can fill out the message area and phone number though only by using the name and not the id which i would rather do based on your advice. however i can do nothing with the submit button.

here is what i have written

tell application "Safari" to do JavaScript "document.getElementsByName('message')[0].value = 'evaporetore'" in document 1
tell application "Safari" to do JavaScript "document.getElementsByName('recipient')[0].value = '1234567'" in document 1

tell application "Safari" to do JavaScript "document.getElementsByName('sms_button').submit()" in document 1

do i need a safari tell command for every javascript action? and is there need for a delay when filling out forms etc using javascript?

First of all you can look here what javascript can do in an html page http://www.w3schools.com/jsref/

Then you’re submitting the button instead of the form where the fields are in. Sorry for my misleading but in this case there is only one single element you want to get so it’s better to use getElementById. My example with getElementsByName was only because on my example page the html elements only had a name and not an id.

Then there is a third and hard one to explain but this is the code in one of the javascript files of the page itself.

We can see that an action is binded to the button after the page is loaded. Also can we see that there is no form submit action at all. It’s an ajax call that posts data so the only thing we can do is doing the same action.
I’m not sure if the following code works but I didn’t get an error and some action did happen but I couldn’t test it

tell application "Safari"
	tell document 1
		do JavaScript "document.getElementById('sms_message_textarea').innerHTML = 'Hello world!'"
		do JavaScript "document.getElementById('sms_tel_textbox').value = '2145632718'"
		do JavaScript "document.getElementById('sms_button').click()"
	end tell
end tell