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
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
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.
...
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
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!
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?
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