open Safari URL path in BBEdit

update: this post has been solved, please see the last entry for the solution.

Hi guys, first time poster here.

I’m trying to open URLs that correspond with a volume on my computer. The URL would be something like http://www.site.com/about/grants/menu.php and my volume is /Volumes/Web/about/grants/menu.php

So I’m trying to write an applescript that would open the menu.php in BBEdit from Safari. Any ideas? I’m not really sure where to start besides this:


tell application "Safari"
	set this_URL to the URL of document 1
	--set this_URL_path to some magic with regexp to get just path name and convert "/" to ":"
end tell

tell application "BBEdit"
	activate
	open {file "Web:this_URL_path"} with LF translation
end tell

Hi,

welcome :slight_smile:

Something like this

tell application "Safari" to set this_URL to the URL of document 1

set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL as text
set text item delimiters to ":"
set this_URL to this_URL as text
set text item delimiters to TID

tell application "BBEdit"
	activate
	open ("Web:" & this_URL as alias) with LF translation
end tell

Thanks StefanK!

How can I perform a regex search to grab the bit after the domain/ … with something like ^http://[a-zA-Z0-9.]*(/)

Also, if there’s no file in the URL (in the case of index.html or index.php) I’d like to add it. Can applescript verify that index.html or index.php exists?

I could imagine the logic being:
if this_URL doesn’t contain .html or .php then
check for index.html
else check for index.php
else return error

Sorry for being so lame, I’m just staring to grasp the elusive syntax as I see these kinds of scripts.

Thanks for the help.

grep or awk isn’t necessary


...
set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL as text
set bitAftertheDomain to text item 1 of this_URL -- bitAftertheDomain contains the part until next slash after the domain
set text item delimiters to ":"
...

Of course, here a suggestion:

tell application "Safari" to set this_URL to the URL of document 1

set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL as text
set bitAftertheDomain to text item 1 of this_URL
set text item delimiters to ":"
set this_URL to this_URL as text
set text item delimiters to TID

if this_URL does not end with "index.php" or this_URL does not end with "index.html" then
	-- (do something like)
	-- set this_URL to this_URL & "index.html"
end if

tell application "BBEdit"
	activate
	open ("Web:" & this_URL as alias) with LF translation
end tell

OK, I see how you used text item delimiters and text items 4 thru -1 to eliminate the need for regex; why the last usage of this:

set text item delimiters to TID

Is that supposed to replace ‘/’ with ‘:’? I can’t get that to work as expected. My url still contains ‘/’ instead of ‘:’

For those following along at home I’m going to use this to verify the file exists:


tell application "Finder"
	if (item this_URL) exists then
		return true
	else
		return false
	end if
end tell

text item delimiters can be set from everywhere. To avoid unexpected behavior you should reset it to
the last value before having changed it

yes, AppleScript can only handle paths separated by colons.
But it should work, I’ve tested it

you can do that without involving the Finder:

try
	this_URL as alias
	return true
on error
	return false
end try

Not sure what I’m doing wrong…


tell application "Safari" to set this_URL to the URL of document 1
set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL as text
set text item delimiters to ":"
return this_URL

This returns:
“yards/index.html”
not
“yards:index.html”
Is my syntax off? Applescript is so confusing for my tiny brain!

you missed two lines, but I did also a mistake :wink:

tell application "Safari" to set this_URL to the URL of document 1

set {TID, text item delimiters} to {text item delimiters, "/"} -- save old text item delimiters and set it to slash
set this_URL to text items 4 thru -1 of this_URL -- cuts off http://thedomain/ and puts every item between the slashes into a list
set text item delimiters to ":" -- change text item delimiter to colon
set this_URL to this_URL as text -- replace the "/" with ":"
set text item delimiters to TID -- restore old text item delimiters
return this_URL

Awesome, I’m finally getting it, and I got a rough version working:


tell application "Safari" to set this_URL to the URL of document 1

set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL
set text item delimiters to ":"
set this_URL to this_URL as text
set text item delimiters to TID

if this_URL does not end with ".php" or this_URL does not end with ".html" then
	try
		"Web:" & this_URL & "index.html" as alias
		set this_URL to this_URL & "index.html"
	on error
		set ishtml to false
	end try
	
	if ishtml is false then
		try
			"Web:" & this_URL & "index.php" as alias
			set this_URL to this_URL & "index.php"
		end try
	end if
	
	
end if


tell application "BBEdit"
	activate
	open ("Web:" & this_URL as alias) with LF translation
end tell

Thanks very much for all the help. If you’re interested in helping further, I’d appreciate some comments. Could this be simplified any further regarding the messy try blocks?

Thanks again Stefan.

in this case I would prefer to check the files with the Finder

tell application "Safari" to set this_URL to the URL of document 1

set {TID, text item delimiters} to {text item delimiters, "/"}
set this_URL to text items 4 thru -1 of this_URL
set text item delimiters to ":"
set this_URL to this_URL as text
set text item delimiters to TID

if this_URL does not end with ".php" or this_URL does not end with ".html" then
	tell application "Finder"
		if exists alias ("Web:" & this_URL & "index.html") then
			set this_URL to this_URL & "index.html"
		else if exists alias ("Web:" & this_URL & "index.php") then
			set this_URL to this_URL & "index.php"
		end if
	end tell
end if

tell application "BBEdit"
	activate
	open ("Web:" & this_URL as alias) with LF translation
end tell

One note to your version:
ishtml is never set to true, so you would get an error message,
if it’s not false