Thursday, December 3, 2020

#1 2020-11-08 06:13:20 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

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.

Applescript:

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

Applescript:

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

Last edited by Fredrik71 (2020-11-08 09:09:13 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)