I’ve been trying to select text fields and change their value and then click a button using javascript in Safari, but I can’t seem to get the code right.
Can anyone give me some examples of what I need to do?
Thanks.
I’ve been trying to select text fields and change their value and then click a button using javascript in Safari, but I can’t seem to get the code right.
Can anyone give me some examples of what I need to do?
Thanks.
Hit the Search button (above) and enter “do AND JavaScript AND document” (you’ll find several examples using Safari)
cool, that got me what I needed…
I’ve got one more question (for the moment :rolleyes: )
Is there a way that I can set a variable to a particular piece of text on the web page, like I want to copy a number that occurs on the web page (the number changes, so I can’t do something like a find) but it is always in the same spot, and I want my script to be able to preform some math on it.
So how can I select that number and put it in to a variable so I can use it in the script?
Thanks agian
If you ask for the page’s properties formally…
tell application "Safari" to properties of document 1
You’ll see that you can retrieve both the “source” (html) and the “text” (displayed text).
So, you can:
tell application "Safari" to set theText to source of document 1
Now, the variable “theText” contains the following:
<html><body><b>3</b>
And you can get it as follow:
set theNumber to text ((offset of "<b>" in theText) + 3) thru ((offset of "</b>" in theText) - 1) of theText as number
But depending on the complexity of the text to search in, you can use different methods.
I’ve got a handler I use for this since I do this kind of thing a lot. You can send it a list, where the list is {sourceText, textBeforeDesiredText, textAfterDesiredText}. You can also send it a record if you want more control. The default behavior is to find the first occurrence of your search items. There are two samples shown below:
set someText to "Contact:Jeff Robertson" & return & "Phone:856-321-4567" & return & "Fax:856-321-9999"
getTextBetween({someText, ":", return})
--> "Jeff Robertson"
getTextBetween({sourceText:"Jeff is my friend", beforeText:"Jeff", afterText:"friend"})
--> " is my "
on getTextBetween(prefs)
-- version 1.4, Daniel A. Shockley <http://www.danshockley.com>
-- gets the text between specified occurrence of beforeText and afterText in sourceText
-- the default textItemNum should be 2
set defaultPrefs to {textItemNum:2}
if (class of prefs is not list) and (class of prefs is not record) then
error "getTextBetween FAILED: parameter should be a record or list. If it is multiple items, just make it into a list to upgrade to this handler." number 1024
end if
if class of prefs is list then
if (count of prefs) is 4 then
set textItemNum of defaultPrefs to item 4 of prefs
end if
set prefs to {sourceText:item 1 of prefs, beforeText:item 2 of prefs, afterText:item 3 of prefs}
end if
set prefs to prefs & defaultPrefs -- add on default preferences, if needed
set sourceText to sourceText of prefs
set beforeText to beforeText of prefs
set afterText to afterText of prefs
set textItemNum to textItemNum of prefs
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to the beforeText
set the prefixRemoved to text item textItemNum of sourceText
set AppleScript's text item delimiters to afterText
set the finalResult to text item 1 of prefixRemoved
set AppleScript's text item delimiters to oldDelims
on error errMsg number errNum
set AppleScript's text item delimiters to oldDelims
-- tell me to log "Error in getTextBetween() : " & errMsg
set the finalResult to "" -- return nothing if the surrounding text is not found
end try
return finalResult
end getTextBetween