Skip to content

Instantly share code, notes, and snippets.

@sci-phi
Forked from chockenberry/README
Last active April 6, 2022 19:14
Show Gist options
  • Save sci-phi/2fdd7165b30c83fc5de39c9a978db993 to your computer and use it in GitHub Desktop.
Save sci-phi/2fdd7165b30c83fc5de39c9a978db993 to your computer and use it in GitHub Desktop.
A simple AppleScript to start up a web server in the folder where the script is located.
1) Add this code to a new file in Script Editor
2) Export the script as "Application" and make sure it's code signed
3) Put "Startup.app" in the root folder of the test website (e.g. where index.html is located)
4) Make sure that "Startup.app" has Full Disk Access in System Preferences > Security & Privacy > Privacy
set portNum to 8888
set serverUrl to "http://localhost:" & portNum
tell application "Finder" to set basePath to (POSIX path of (parent of (path to me) as string))
do shell script "cd " & basePath & "; /usr/bin/ruby -run -ehttpd . -p" & portNum & " &> /dev/null & echo $!"
set pid to the result
delay 1
do shell script "/usr/bin/open " & serverUrl & " &> /dev/null &"
display dialog "Click OK when done testing in " & basePath buttons {"OK"} with title "Serving " & serverUrl
do shell script "kill " & pid
@sci-phi
Copy link
Author

sci-phi commented Apr 6, 2022

I added a title to the dialog displayed to the user - in case they closed the browser window and wanted to get it back without restarting the service.
As part of that I also made the port number configurable (in code) to avoid service conflicts.
Finally, I needed to add a delay on my machine to ensure the service was reliably running before the URL was opened in the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment