Select a folder to become your http server.

Many people maybe know that php -S localhost:8000 will start php sever in root of project folder.
What people maybe do not know we could do this in python.

python -m SimpleHTTPServer will do the same thing.

After reading on the web how this could work with AppleScript.

Select a folder there you have html, css and javascript and run the service or applet.

So I decide to build a PDF server and use Mozilla’s PDF.js.
If you are interesting in that you could find information here:
https://mozilla.github.io/pdf.js/getting_started/

This AppleScript only start the http server and open URL
In my case for PDF server I use ‘/web/viewer.html’ but it could be anything for your project.

If you do not like to use python and instead php its easy to change the script the idea is the same.

on run {input, parameters}
	
	set serverPath to POSIX path of input
	set serverPort to do shell script "PORT=8000; while lsof -i :$PORT > /dev/null; do ((PORT++)) ; done ; echo $PORT"
	-- We use terminal so the process is in background.
	tell application "Terminal" to do script "cd '" & serverPath & "' && python -m SimpleHTTPServer " & serverPort
	
	delay 0.5
	
	do shell script "open http://localhost:" & serverPort & "/web/viewer.html"
	return input
end run

Example from the Script Menu

on run
	tell application "Finder" to set theSelection to (selection) as text
	set serverPath to POSIX path of theSelection
	set serverPort to do shell script "PORT=8000; while lsof -i :$PORT > /dev/null; do ((PORT++)) ; done ; echo $PORT"
	-- We use terminal so the process is in background.
	tell application "Terminal"
		if not (exists window 1) then reopen
		do script "cd '" & serverPath & "' && python -m SimpleHTTPServer " & serverPort in window 1
	end tell
	delay 1
	
	do shell script "open http://localhost:" & serverPort & "/web/viewer.html"
end run