-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.