Hello all,
I’m looking to use applescript to grab a url of a page in safari parse the string and have the finder show and select the current file if it’s on the local machine. A nice little time saver and fairly straightforward? I’ve spent a good couple of hours trying to work it out, even finding documentation seems to be difficult. Every language has some sort of string manipulation capabilities, applescript must have but finding any info seems incredibly difficult… how on earth do you manage?!
I got this far by hacking a few of the examples I came across:
tell application "Safari"
-- Find out if Safari has a window open
if not (exists (front window)) then
activate
set dialogText to "No windows are open in Safari."
display dialog dialogText buttons {"OK"} default button 1
return
else
-- Get the URL of the page currently displayed in Safari's front window
set current_URL to URL of document of front window
end if
end tell
-- watch out for server Aliases... check httpd.conf for details
set current_URL to SaR(current_URL, "simon.local/books", "simon.local/httpdocs/books")
set current_URL to SaR(current_URL, "simon.local/php", "simon.local/dynamic/php")
set current_URL to SaR(current_URL, "http://simon.local", "Users/simon/Sites")
set current_URL to SaR(current_URL, "/", ":")
set current_URL to path to startup disk & current_URL
tell application "Finder"
activate
open folder current_URL of the startup disk
end tell
on SaR(aText, asearch, areplace)
try
set oldAstid to AppleScript's text item delimiters
set AppleScript's text item delimiters to asearch
set a_text to every text item of aText
set AppleScript's text item delimiters to areplace
set a_text to (every text item of a_text) as string
set AppleScript's text item delimiters to oldAstid
return a_text
on error
return ""
end try
end SaR
However when I couldn’t find a way of getting a “last Index of”, and the finder kept telling me that it wasn’t happy with the object I was trying to set it to select, I finally gave up and wrote a javascript bookmarklet. Here’s the code for anyone who might find it useful, it get’s close but doesn’t select the file (just opens the folder the files in), and I can’t control the finder window size and loc in a way I’d be able to do with applescript:
javascript:
/* web domain*/
var wd="simon.local";
/* file directory */
var fd="/Users/simon/Sites";
if(document.domain==wd){
var w_loc=document.URL;
/* check for any aliases - see httpd.conf */
w_loc=w_loc.replace(wd+"/books",wd+"/httpdocs/books");
w_loc=w_loc.replace(wd+"/php",wd+"/dynamic/php");
w_loc=w_loc.replace(wd+"/photos",wd+"/httpdocs/photo");
/* there might be a query string with a / in it so cut everything after the ? */
var q=w_loc.lastIndexOf("?");
if(q>0){
w_loc = w_loc.substring(0,q);
}
var f_loc=w_loc.replace("http://"+wd,"file://"+fd);
/* finally remove the doc so only a directory is left */
var trim_point=f_loc.lastIndexOf("/");
f_loc = f_loc.substring(0,trim_point+1);
/* send safari there and it'll open the folder with the finder */
window.location=f_loc;
} else {
alert("Sorry this URL isn't the local domain I'm looking for.nnthis domain: "+document.domain+"nmy domain: "+wd);
};
so… what would I like to know?
- Where’s all the documentation on core applescript?
- What’s applescripts equivalent of lastIndexOf() ?
- What object is the finder after when telling it set a selection?
Here’s me hoping it isn’t always this frustrating.
Thanks,
Simon