How to open url with html id attribute

Howdy,

I am trying to add some html help to an applescript studio application, and I want to create several “?” buttons in the UI that will bring the user to the relevant paragraph in the html file. I am trying to do this with the html “id” attribute, where a hash and a string are added to the end of the url. For example, pasting this file address in a browser:

         /Users/tm/myfile.html#section1

will open myfile.html and scroll down to the area marked “section1”.

I am having trouble when I try to code this from applescript because the browser is replacing the hash mark with “%23”, and this destroys the address. Here is the applescript that I’m working with:

 
set pathtohome to path to home folder as string
set idString to "section1"
set theUrl to (POSIX path of pathtohome) & "myfile.html#" & idString
tell application "Safari"
	open location theUrl
end tell

I have also tried converting theUrl to unicode text, and the browser still replaces the hash with %23. Any help you can provide would be greatly appreciated!

Tasha

Model: macbook core duo
AppleScript: Xcode v 3.0
Browser: Firefox 3.0.10
Operating System: Mac OS X (10.5)

Tasha:

This may not be the proper way to go about things but your script as amended below works for both Safari and Camino… when ‘open location’ is directed to either in a tell block. (Seems the URL needs to be ‘literally’ completed.) Open location is in Standard Additions, however, needs no ‘tell’ and will target the user’s default browser if used without.

I also took the liberty of altering the placement of the hash, thinking it might be better to keep it with the section name.

Hope this is of use…

Peter B.



set pathtohome to path to home folder as string
set idString to "#section1"
set theUrl to (POSIX path of pathtohome) & "myfile.html" & idString
set theUrl to "file://" & theUrl
--tell application "Safari"
	open location theUrl
--end tell

.or create the URL with System Events


set idString to "#section1"
tell application "System Events" to set theURL to URL of file "myfile.html" of home folder & idString
open location theURL

Thank You, Stefan.

Good call… as usual. I was aware of the file URL method, but I’m not thinking that clearly today.

Peter B.


Thanks Folks! That solution worked nicely!

Best,
Tasha