Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have many web projects that I develop on the localhost. Normally to get to these one has to navigate from the root directory or bookmark the url.

I want to be able to open an OS directory/folder in the default browser. So for example I right-click a 'project A' folder and choose 'open in browser' and go there directly. I would like to be able to specify the beginning of the url, so I can change file:/// to http://localhost/ or mymac.local/.

I am imagining this could be done with Applescript but I'm not experienced in that. Is there any way to achieve this?

share|improve this question
    
Where is your web directory based? (/Users/username/Sites?) –  Nathan Jul 15 '12 at 0:24
    
I am not using the OSX Sites folder, I have been using MAMP & a folder with a path like /Users/username/-/work –  Machine Elf Jul 15 '12 at 9:10

2 Answers 2

up vote 0 down vote accepted

I think this Applescript saved as a service will do it:
(Set to "Service receives selected files or folders in Finder")

on run {input, parameters}
    set basePath to "users/username/folder/"
    set baseURL to "http://localhost"

    repeat with i in input
        tell application "Safari"
            if not (exists (document 1)) then
                --
                -- Safari is *Not* Opened, Call Open New Window Function, Go To Preferred Page
                -- and Activate Safari to Enable Scripting
                --
                tell application "Safari" to activate
                my open_new_window()
                --
                -- Set Safari Location to Preferred Page
                -- 
                set theFileName to POSIX path of i
                set thePath to text (length of basePath+1) thru -1 of theFileName
                set the URL of document 1 to baseURL & thePath
            else
                --
                -- Safari is Opened, Bring it To Front and Open New Tab w/Preferred Page
                --
                if (exists (URL of document 1)) then
                    my open_new_tab()
                end if
                --
                -- and Activate Safari to Enable Scripting
                --
                tell application "Safari" to activate
                --
                -- Set Safari Location to Preferred Page
                --
                set theFileName to POSIX path of i
                set thePath to text (length of basePath+1) thru -1 of theFileName
                set the URL of document 1 to baseURL & thePath
            end if
        end tell

    end repeat

    return input
end run

--
-- Open New Window Function
--
on open_new_window()
    tell application "Safari" to activate
    tell application "System Events"
        tell process "Safari"
            click menu item "New Window" of menu "File" of menu bar 1
        end tell
    end tell
end open_new_window

--
-- Open New Tab Function
--
on open_new_tab()
    tell application "Safari" to activate
    tell application "System Events"
        tell process "Safari"
            click menu item "New Tab" of menu "File" of menu bar 1
        end tell
    end tell
end open_new_tab

Cobbled together from a few sources!

share|improve this answer

You can add a right click item via Automator. Open Automator and copy this workflow:


When you save it, it will appear in your right click menu under Services:


I'm not completely sure what you mean by changing the URL protocol, but I should be able to modify this to accomplish what you want

share|improve this answer
    
Thanks so close! It's almost working, in Chrome it opens two windows not one, and Safari opens a blank window with an activity panel error saying 'frame load interrupted' - I think it's possibly security related? Can you possibly verify whether it's same for you? –  Machine Elf Jul 15 '12 at 9:08
    
Weird. I forgot to actually test it in Safari. Safari just doesn't load it at all and Chrome does load two pages for me. I didn't notice it. I'll have to mess around with it more... –  daviesgeek Jul 15 '12 at 18:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.